Renaming, Copying, or Comparing a Set of Files

If you have a group of files whose names end with new and you want to rename them to end with old, this won't work:

% mv *.new *.old Wrong !

because the shell can't match *.old (), and because the mv command just doesn't work that way. Here's how to do it:

-d (..\)..\1 
% ls -d *.new | sed "s/.*\)\.new$/mv '&' '\1.old'/" | sh

That outputs a series of mv commands, one per file, and pipes them to a shell. The quotes help make sure that special characters (8.19) aren't touched by the shell - this isn't always needed, but it's a good idea if you aren't sure what files you'll be renaming:

mv 'afile.new' 'afile.old' mv 'bfile.new' 'bfile.old' ...

(To see the commands that will be generated rather than executing them, leave off the | sh or use sh -v ().) To copy, change mv to cp. For safety, use mv -i or cp -i if your versions have the -i options ().

This method works for any UNIX command that takes a pair of filenames. For instance, to compare a set of files in the current directory with the original files in the /usr/local/src directory, use diff ():

% ls -d *.c *.h | sed 's@.*@diff -c & /usr/local/src/&@' | sh

- JP