home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 18.8 Showing the Actual Filenames for Symbolic Links Chapter 18
Linking, Renaming, and Copying Files
Next: 18.10 There's More than One Way to Do It
 

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


Previous: 18.8 Showing the Actual Filenames for Symbolic Links UNIX Power Tools Next: 18.10 There's More than One Way to Do It
18.8 Showing the Actual Filenames for Symbolic Links Book Index 18.10 There's More than One Way to Do It

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System