LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do I rename a lot of files at once? (https://www.linuxquestions.org/questions/linux-general-1/how-do-i-rename-a-lot-of-files-at-once-241812/)

suresheva31 10-12-2004 02:26 PM

How do I rename a lot of files at once?
 
Hey guys,

I want to rename my whole bunch of files has extension of .cfg-sample to .cfg at once

Examples:

checkcommands.cfg-sample escalations.cfg-sample nagios.cfg-sample

to

checkcommands.cfg escalations.cfg nagios.cfg

this is the script that i found from the this website: onlamp.com/pub/a/onlamp/2002/09/05/nagios.html?page=2



for i in *cfg-sample; do mv $i
`echo $i | sed -e s/cfg-sample/cfg/`; done;

it doesn't seems working for me, and i get the following error.

mv: missing file argument
Try `mv --help' for more information.
-bash: cgi.cfg: command not found
mv: missing file argument
Try `mv --help' for more information.
-bash: checkcommands.cfg: command not found


any helps, thanks in advance

suresh

vasudevadas 10-12-2004 02:43 PM

for x in *.cfg-sample
do
mv $x `echo $x | sed s/cfg-sample/cfg/g`
done

I just tried it and it works.

jeickal 10-12-2004 02:45 PM

How about this one (from bash):

- 1st, get all you file names in a tempfile like this:

ls -1 *.cfg-sample > tempfile

Note: the "-1" option to get one file per line

- then run this loop:


while read a
do
mv $a `echo $a|sed -e "s\cfg-sample\cfg\g"`
done < tempfile


Carefull to respect the quote " and `
This will read "tempfile" containing your file names line by line and store each line in the variable "a" each time it loops

then you move $a into itself but replacing "cfg-sample" by cfg thanks to the sed command.
Hope this help

jeickal 10-12-2004 02:48 PM

I see some are typing quicker then me ;)

We used the same trick so must be the right one...

vasudevadas 10-12-2004 02:49 PM

Some around here are just so eager to help. :D

suresheva31 10-12-2004 02:57 PM

your best people. Its works...thanks a lot


All times are GMT -5. The time now is 03:01 PM.