rename is a small filename renaming utility.
By default it behaves like the util-linux style rename: it replaces a plain substring in each filename.
It also has a friendly glob-template mode with -g, where * in the source pattern captures part of the filename and * in the target pattern inserts it.
rename [options] FROM TO FILE...
rename -g [options] FROMPAT TOPAT FILE...-n dry run; show what would be renamed, but do not rename
-v verbose output
-a replace all occurrences of FROM
-l replace the last occurrence of FROM
-g glob-template mode
-o do not overwrite existing target files; this is the default
-f force overwrite existing target files
-a and -l cannot be used together.
In default mode, FROM is not a regular expression. It is a plain substring.
Rename .jpeg suffixes to .jpg:
rename .jpeg .jpg *.jpegRename .txt to .text:
rename .txt .text *.txtReplace the first occurrence only:
rename foo bar *Example:
foo.txt -> bar.txt
old-foo.txt -> old-bar.txt
foo-foo.txt -> bar-foo.txt
Replace all occurrences with -a:
rename -a foo bar *Example:
foo-foo.txt -> bar-bar.txt
Replace only the last occurrence with -l:
rename -l foo bar *Example:
foo-foo.txt -> foo-bar.txt
Use -n before doing a large rename:
rename -n .txt .text *.txtThis prints the planned renames without changing files.
Verbose mode can be combined with it:
rename -n -v .txt .text *.txtBy default, existing target files are not overwritten:
rename .txt .text *.txtThis is equivalent to:
rename -o .txt .text *.txtTo overwrite existing targets, use -f:
rename -f .txt .text *.txtUse -n first if you are unsure:
rename -n -f .txt .text *.txtGlob-template mode is intended for friendly filename transformations.
The source pattern may contain one *. The * captures the changing part of the filename.
The target pattern may also contain one *. That * is replaced with the captured part.
Rename .txt files to .text:
rename -g '*.txt' '*.text' *Example:
notes.txt -> notes.text
book.txt -> book.text
a.txt -> a.text
Rename only names beginning with a:
rename -g 'a*.txt' 'a*.text' *Example:
abc.txt -> abc.text
article.txt -> article.text
b.txt unchanged
Change a prefix while preserving the captured middle part:
rename -g 'a*.txt' 'b*.text' *Example:
abc.txt -> bbc.text
article.txt -> brticle.text
Rename camera images:
rename -g 'IMG_*.JPG' 'photo-*.jpg' *Example:
IMG_0001.JPG -> photo-0001.jpg
IMG_0242.JPG -> photo-0242.jpg
Remove a fixed prefix:
rename -g 'old-*.txt' '*.txt' *Example:
old-report.txt -> report.txt
old-notes.txt -> notes.txt
Add a prefix:
rename -g '*.txt' 'new-*.txt' *Example:
report.txt -> new-report.txt
notes.txt -> new-notes.txt
Quote glob-template patterns.
Correct:
rename -g '*.txt' '*.text' *
rename -g 'a*.txt' 'a*.text' *Usually wrong:
rename -g *.txt *.text *Without quotes, the shell expands *.txt before rename sees it. For example, if the directory contains a.txt and b.txt, the program may receive a.txt b.txt instead of the pattern *.txt.
-g mode is not regexp mode.
In shell-style glob patterns:
*.txt
means “anything ending in .txt”.
In regular expressions, the equivalent would be closer to:
.*\.txt$
This program's -g mode uses the friendlier glob-template idea, not regexp substitution syntax like s/pattern/replacement/.
For many files, first run:
rename -n -g '*.txt' '*.text' *Then, if the output looks correct, run:
rename -g '*.txt' '*.text' *