[Chapter 18] 18.9 Renaming, Copying, or Comparing a Set of Files
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:39:05Z

#

| Chapter 18 Linking, Renaming, and Copying Files |

## 18.9 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` (1.18), 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` (8.17).) To copy, change `mv` to `cp`. For safety, use `mv -i` or `cp -i` if your versions have the -i options (21.11).

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 (28.1):

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

- JP

18.8 Showing the Actual Filenames for Symbolic Links |  | 18.10 There's More than One Way to Do It