Skip to content

norayr/rename

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

rename

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.

Usage

rename [options] FROM TO FILE...
rename -g [options] FROMPAT TOPAT FILE...

Options

-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.

Default mode: substring replacement

In default mode, FROM is not a regular expression. It is a plain substring.

Rename .jpeg suffixes to .jpg:

rename .jpeg .jpg *.jpeg

Rename .txt to .text:

rename .txt .text *.txt

Replace 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

Dry run

Use -n before doing a large rename:

rename -n .txt .text *.txt

This prints the planned renames without changing files.

Verbose mode can be combined with it:

rename -n -v .txt .text *.txt

Overwrite behavior

By default, existing target files are not overwritten:

rename .txt .text *.txt

This is equivalent to:

rename -o .txt .text *.txt

To overwrite existing targets, use -f:

rename -f .txt .text *.txt

Use -n first if you are unsure:

rename -n -f .txt .text *.txt

Glob-template mode: -g

Glob-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

Important shell quoting note

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.

Difference between glob mode and regular expressions

-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/.

Safe workflow

For many files, first run:

rename -n -g '*.txt' '*.text' *

Then, if the output looks correct, run:

rename -g '*.txt' '*.text' *

About

util-linux like rename

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors