30.9 Transforming File Names in Dired
This section describes Dired commands which alter file names in a systematic way. Each command operates on some or all of the marked files, using a new name made by transforming the existing name.
Like the basic Dired file-manipulation commands (see Operating on Files), the commands described here operate either on the next n
files, or on all files marked with β*
β, or on the current file. (To mark files, use the commands described in Marks vs Flags.)
All of the commands described in this section work interactively: they ask you to confirm the operation for each candidate file. Thus, you can select more files than you actually need to operate on (e.g., with a regexp that matches many files), and then filter the selected names by typing y
or n
when the command prompts for confirmation.
% u
β
Rename each of the selected files to an upper-case name (dired-upcase
). If the old file names are Foo
and bar
, the new names are FOO
and BAR
.
% l
β
Rename each of the selected files to a lower-case name (dired-downcase
). If the old file names are Foo
and bar
, the new names are foo
and bar
.
% R from RET to RET
β
% C from RET to RET
β
% H from RET to RET
β
% S from RET to RET
β
These four commands rename, copy, make hard links and make soft links, in each case computing the new name by regular-expression substitution from the name of the old file.
The four regular-expression substitution commands effectively perform a search-and-replace on the selected file names. They read two arguments: a regular expression from
, and a substitution pattern to
; they match each old file name against from
, and then replace the matching part with to
. You can use β\&
β and β\digit
β in to
to refer to all or part of what the pattern matched in the old file name, as in replace-regexp
(see Regexp Replace). If the regular expression matches more than once in a file name, only the first match is replaced.
For example, % R ^.*$ RET x-\& RET
renames each selected file by prepending βx-
β to its name. The inverse of this, removing βx-
β from the front of each file name, is also possible: one method is % R ^x-\(.*\)$ RET \1 RET
; another is % R ^x- RET RET
. (Use β^
β and β$
β to anchor matches that should span the whole file name.)
Normally, the replacement process does not consider the filesβ directory names; it operates on the file name within the directory. If you specify a numeric argument of zero, then replacement affects the entire absolute file name including directory name. (A non-zero argument specifies the number of files to operate on.)
You may want to select the set of files to operate on using the same regexp from
that you will use to operate on them. To do this, mark those files with % m from RET
, then use the same regular expression in the command to operate on the files. To make this more convenient, the %
commands to operate on files use the last regular expression specified in any %
command as a default.