0

I want to remove the extensions and prefixes from these files:

awstats.www.test1.com.conf
awstats.www.test2.com.conf
awstats.www.test3.com.conf

I need to remove awstats. and .conf, but leave files like www.test1.com unchanged. How can I do this?

  • Unless there is some other complication, this looks like it would just be a matter of typing in three separate mv commands. – Kusalananda Dec 18 '19 at 09:09
  • @Kusalananda i need to get keep this file in original location and run the command,i have used basename command but it only remove .conf part. – Sudesh Lakmal Pathirana Dec 18 '19 at 09:11
  • @Kusalananda my requirement is list the files exept awstats. and .conf part.also run these values in side the for loop in shell – Sudesh Lakmal Pathirana Dec 18 '19 at 09:16
  • 1
    Are you aware of the rename command? – FelixJN Dec 18 '19 at 09:17
  • @Fiximan i have tried that also, i cant get that remove both sides,this is my command --> rename -- .conf '' *.conf – Sudesh Lakmal Pathirana Dec 18 '19 at 09:20
  • @Fiximan it only remove .conf part according to my knowledge. – Sudesh Lakmal Pathirana Dec 18 '19 at 09:21
  • Again, what is stopping you from just doing mv awstats.www.test1.com.conf www.test1.com? I'm trying to figure out what your issue is. – Kusalananda Dec 18 '19 at 09:28
  • 1
    Your rename command looks odd. Please add this to your question in proper formatting. Regexes with two replacements would look like: s/A/B/;s/1/2/ ---- @Kusalananda Pretty sure he is working on more than 3 files. – FelixJN Dec 18 '19 at 09:32
  • @Fiximan i have installed AWstats[stats tool] on my linux shared server. after configuration it should add into crontab this command, 0 2 * * * /usr/bin/perl /usr/share/awstats/wwwroot/cgi-bin/awstats.pl -config=tecadmin.net -update , my shared server have 1000 web site URLs, then its headeche to add those files into crontab. i want to automate this task. – Sudesh Lakmal Pathirana Dec 18 '19 at 09:41
  • 1
    Please put all corrections into the question. Trying to understand you problem will then be easier. – ctrl-alt-delor Dec 18 '19 at 10:23

2 Answers2

2

Assuming that you are in the same directory as your files:

for name in awstats.*.conf; do
    basename "${name#awstats.}" .conf
done

The code inside the loop will first trim the awstats. prefix off from $name using a standard parameter substitution, and then lets basename trim off the .conf suffix.

You could also do it in two steps without calling basename:

for name in awstats.*.conf; do
    newname=${name#awstats.}  # trim off prefix
    newname=${newname%.conf}  # trim off suffix
    printf '%s\n' "newname"
done

No files are renamed by these two loops.

Kusalananda
  • 333,661
  • i want to keep original files if not this service is not working.get the files without extensions and should run pern command in side the shell script. – Sudesh Lakmal Pathirana Dec 18 '19 at 09:54
  • @SudeshLakmalPathirana If you have clarifications to your question, please add these to the actual question, not in comments. – Kusalananda Dec 18 '19 at 09:56
  • can you give me solution only list the files format.not remove using mv command. – Sudesh Lakmal Pathirana Dec 18 '19 at 10:06
  • @SudeshLakmalPathirana I gave you a solution to the issue as stated in your question. Did I miss anything from the question? If you update the question, some of us may update our answers. – Kusalananda Dec 18 '19 at 10:07
  • i have updated the title of my question.if that have any doubts please ask without hesitation.and also im new to this community.sorry for the any trouble :) – Sudesh Lakmal Pathirana Dec 18 '19 at 10:11
  • when using mv command your script working properly.i want to list files without removing original files. – Sudesh Lakmal Pathirana Dec 18 '19 at 10:15
  • @SudeshLakmalPathirana See updated answer. The new code does not rename any files, just prints the modified filenames. When a question say it wants to modify filenames (you wrote "remove the extensions and prefixes from these files"), people will automatically think it's about renaming files. – Kusalananda Dec 18 '19 at 10:18
  • Thank you very much dear.this is what i want.now i want to passed these values into the array and add this command /usr/bin/perl -config=www.test1.com -update. i will give it try. – Sudesh Lakmal Pathirana Dec 18 '19 at 10:24
1

I assume rename (perl-rename on some systems, just rename on others) is simplest here:

rename 's/^awstats\.//;s/\.conf$//' awstats.*.conf

Explanation of the regex:

  • s/^awstats\.// substitute awstats. at beginning of line (filename) with nothing
  • s/\.conf$// substitute .conf at end of line (filename) with nothing

Use the -n option to do a dry-run!

terdon
  • 242,166
FelixJN
  • 13,566