4. Rename files

Check if the perl Module File::Rename is installed:

rename -h
If not, to install File::Rename, copy and paste the appropriate command in to your terminal.

via cpanm

cpanm File::Rename
or via the CPAN shell perl -MCPAN -e shell install File::Rename

4.1 Add prefix filename


rename -v 's/^/PRE_/' 0001.png
The switch -v means the verbose mode. This mode shows the result after the file has been renamed. In our case:

0001.png renamed as PRE_0001.png

Notice that s/^/PRE_/ is a regular expression (as in all following examples).

4.2 Remove prefix filename


rename -v 's/^img//' img0001.png
img0001.png renamed as 0001.png

4.3 Add suffix filename


rename -v 's/^(.+)\.([^.]+)$/$1_SUF.$2/' 0001.png
0001.png renamed as 0001_SUF.png

The code ^(.+)\.([^.]+)$ matches one or more characters at the beginning ^(.+), followed by a dot \. and some not dots at the end ([^.]+)$.

([^.]+)$ could be replaced by (.*)\.(\w+)$. The "\w" means "any word character", which is usually alphanumeric (letters, numbers, regardless of case) plus underscore (_).

4.4 Remove suffix filename


rename -v 's/_SUF\.([^.]+)$/.$1/' 0001_SUF.png
0001_SUF.png renamed as 0001.png

4.5 Remove spaces in filename


rename -v 's/\s//g' 'a b c.txt' # filename between apostrophes!
a b c.txt renamed as abc.txt

4.6 Change extension filename


rename -v 's/IMG0001.JPG/IMG0001.jpg/g' IMG0001.JPG
IMG0001.JPG renamed as IMG0001.jpg

Or more general for a file with IMG as prefix and followed by 4 characters:

rename -v 's/(IMG....).JPG/$1.jpg/g' IMG0002.JPG
IMG0002.JPG renamed as IMG0002.jpg

For a file with IMG as prefix and followed by 4 digits

rename -v 's/(IMG\d{4}).JPG/$1.jpg/g' IMG0003.JPG
IMG0003.JPG renamed as IMG0003.jpg

4.7 Rearranging the elements of a filename


rename -v 's/(.{3})_(.{3}).JPG/$2_$1.jpg/g' abc_def.JPG
abc_def.JPG renamed as def_abc.jpg

The first and second capture group are swapped by reordering the storage variables $1 and $2

4.8 Rename multiple files

Replace in the previous examples the file by multiple files to apply the actions on more than one file:

rename -v 's/(IMG....).JPG/$1.jpg/g' IMG0001.JPG IMG0002.JPG IMG0003.JPG IMG0004.JPG
IMG0001.JPG renamed as IMG0001.jpg
IMG0002.JPG renamed as IMG0002.jpg
IMG0003.JPG renamed as IMG0003.jpg
IMG0004.JPG renamed as IMG0004.jpg


Or more general, using the wildcard *:

rename -v 's/(IMG....).JPG/$1.jpg/g' IMG*.JPG
with the same result.

4.9 Padding numbers in a filename

Suppose you've the files 1.txt, 6.txt, 10.txt, 99.txt and want to rename them as follows: 001.txt, 006.txt, 010.txt, 099.txt. This padding can be done by:

rename -v 's/\d+/sprintf "%03d", $&/e' 1.txt 6.txt 10.txt 99.txt 1024.txt
1.txt renamed as 001.txt
6.txt renamed as 006.txt
10.txt renamed as 010.txt
99.txt renamed as 099.txt


  1. sprintf("%03d", $number) will print $number padded with 0s until its length is 3 (hence, 1024.txt is not affected).
  2. the special variable $& refers to the last successful pattern match. You could use alternatively $MATCH
  3. the switch e enables to use expressions, here sprintf

4.10 Module perlmv

The module Perlmv lets you rename/move files, using scriptlets (Perl code). Perlmv offers scriptlets, recursive mode, automatic renaming in case of conflicts. You can also link, symlink, or copy. It's worth studying.

The command

perlmv -l
shows all scriptlets available. In my case:

add-extension-according-to-mime-type
dedup-space
keep-one-ext
pinyin
remove-all-ext
remove-common-prefix
remove-common-suffix
remove-ext
to-number
to-number-ext
to-timestamp
to-timestamp-ext
trim
unaccent
To study e.g. scriptlet to-timestamp-ext, enter

perlmv -s to-timestamp-ext
which outputs

### Name: to-timestamp-ext (from App::perlmv::scriptlets::std)
### Summary: Rename files into timestamp. Preserve extensions. Ex: file1.txt -> 2010-05-13-10_43_49.txt
use POSIX; /.+\.(.+)/; $ext=$1;
@st = lstat ;
POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime $st[9]).(defined($ext) ? ".$ext" : "")

Example:

perlmv -v  to-timestamp-ext perlmv.txt
which outputs

move `perlmv.txt` -> `2023-07-01-09_51_02.txt`