Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 18.10 There's More than One Way to Do It Chapter 18
Linking, Renaming, and Copying Files
Next: 18.12 Renaming a List of Files Interactively
 

18.11 Renaming Files with ren

The CD-ROM contains a command called ren that you can use to rename multiple files. The advantage of ren is that it can be used to rename files in a flexible fashion. For example, I have a set of PostScript files that are named ps.ch01, ps.ch02, and so on. I need these files to follow the usual convention of having the ps extension as a suffix, not a prefix - i.e. ch01.ps, ch02.ps, ch03.ps, etc. I could do this with a simple shell script, but it's much easier to just use ren.

ren
ren recognizes the metacharacters * and ?, and uses each instance of their use in replacement patterns. The first string in the filename that matches a wildcard is taken as argument 1, or #1. The second is taken as #2, and so on. The best way to explain this is to simply show how ren can be used.

% ls
ps.ch01 ps.ch02 ps.ch03 ps.ch04 ps.ch05 ps.ch06 ps.ch07

Use the * wildcard in the search string, and then use #1 where you want that string included in the replacement string. Because * is also a shell wildcard that should be interpreted by ren and not the shell, you need to protect it within quotes (8.14). In the Bourne shell, # is also a comment character and needs to be quoted; an interactive C shell doesn't treat # as a comment but, to be consistent, we show it quoted:

% ren "ps.*" "#1.ps"

If ren completes execution silently, everything worked just fine and the files were renamed. Check by listing the directory again:

% ls
ch01.ps ch02.ps ch03.ps ch04.ps ch05.ps ch06.ps ch07.ps

ren doesn't let you overwrite existing files without warning. Suppose we had another file in the same directory called ch07.ps:

% ls
ch07.ps ps.ch01 ps.ch02 ps.ch03 ps.ch04 ps.ch05 ps.ch06 ps.ch07

Now when we try renaming the files, ren warns you about overwriting the ch07.ps file:

% ren "ps.*" "#1.ps"
ps.ch07 -> ch07.ps ; remove old ch07.ps?

This feature can be suppressed with the -d option, which says to overwrite files without prompting. Related options are -k, which says not to overwrite any files, also without prompting; and -a, which says to abort the entire procedure if any files will be overwritten. Using -a, ren aborts before any files are renamed, so you can start all over again.

ren is also smart enough to detect internal naming conflicts before it actually renames any files. For example, suppose we had both files with both ps. and eps. prefixes that we wanted renamed with .ps suffixes. If there were any conflicts, ren would tell us right away, and none of the files would be renamed:

% ls
README       ps.ch01      ps.ch03      ps.ch05      ps.ch07
eps.ch07     ps.ch02      ps.ch04      ps.ch06
% ren "*ps.*" "#2.ps"
Two or more files would have to be renamed to 'ch07.ps'.
Aborting, no renames done.

ren has the restriction that it can only be used to move files within a single directory. Although this makes it inconvenient for some applications, it also makes it more secure.

To show ren in a more complicated situation, let's take another example. Every week I write a report and then store it in a directory under the name month.day.year. After a while, I realized that because of the default sorting used by ls, the files weren't being listed in chronological order.

% ls
1.13.92         1.27.92         12.23.91        2.3.92
1.20.92         1.6.92          12.30.91

What I needed to do was to rename them year.month.day, and use leading 0s for the first nine months. This can be quickly done with two ren commands:

% ren "?.*.*" "#3.0#1.#2"
% ren "1?.*.9?" "9#3.1#1.#2"
% ls
91.12.23        92.01.13        92.01.27        92.02.3
91.12.30        92.01.20        92.01.6

The first command renames any reports for single-digit months (0-9). In the second command, I'm careful not to match any of the names of files I've already moved.

- LM


Previous: 18.10 There's More than One Way to Do It UNIX Power ToolsNext: 18.12 Renaming a List of Files Interactively
18.10 There's More than One Way to Do It Book Index18.12 Renaming a List of Files Interactively

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