Friday, April 20, 2007

Change the Suffix

If you want to change the suffix of multiple files, you can't do:

% mv *.abc *.def

However the following shell script can be used to do the required opperation:


Change all *.abc file to *.def the following shell script would work:

for f in *.abc; do
mv $f `basename $f .abc`.def

How it works:

for f in *.abc; do

Set up a look for all files ending in .abc, and each time around setup $f as the filename

mv $f `basename $`.def

`basename $f .abc` takes the filename in $f and removes any trailing occurences of .abc, we then append .def to the result and the resulting command becomes "mv file.def"


Ends the "for" loop above.

Under "csh" or "tcsh" a similar thing could be done with:

foreach f in ( *.abc )
mv $f `basename $f .abc`.def

No comments: