Renaming multiple files on Linux sounds like a simple task, but it can get rather complex. It’s possible to bulk rename files with the mv
command and a bit of Bash scripting, or use the mmv
and rename
utilities – which aren’t ordinarily installed by default. In this guide, we’ll show various examples for renaming multiple files at once from the Linux command line.
Rename Multiple Files With mv
The mv
command is a default part of Linux and can be used to rename multiple files, but a little scripting is required to do the job. Some examples also rely on other default Linux utilities like ls
, find
, xargs
, etc.
Example 1. Change all file names to lowercase.
$ for f in `ls | grep [A-Z]`; do mv "$f" "`echo $f | tr 'A-Z' 'a-z'`"; done
Example 2. Change all file names to uppercase.
$ for f in `ls | grep [a-z]`; do mv "$f" "`echo $f | tr 'a-z' 'A-Z'`"; done
Example 3. Change all file extensions from .jpeg
to .jpg
.
$ for f in *.jpeg; do mv "$f" "${f%.jpeg}.jpg"; done
Example 4. Remove the .bak
file extension from all files.
$ for f in `ls *.bak`; do mv $f ${f%.*}; done
Example 5. Add a .bak
file extension to all files.
$ for f in `ls`; do mv $f $f.bak; done
Example 6. Use xargs
to append “_backup” to every file.
$ ls * | xargs -I{} mv {} {}_backup
Example 7. Use the find
, sed
, and mv
commands to change all files to lowercase.
$ find . -maxdepth 1 -type f -name '*[A-Z]*'|sed -n 's/\(.*\/\)\(.*\)/mv -n -v -T \1\2 \1\L\2/p'|sh
Example 8. Use the find -exec
and mv
commands to append “_backup” to every file ending in .log
extension.
$ find . -type f -name "*.log" -exec mv {} {}_backup \;
Example 9. Similar to the previous example, but replace find’s -exec
option with xargs
to append “_backup” to every file ending in .log
extension.
$ find . -type f -name '*log' -print0 | xargs --null -I{} mv {} {}_backup
Example 10. Replace a pattern in every file using mv
and sed
. This will replace “IMG” with “Vacation” in every .jpg
file.
$ for f in *.jpg; do mv "$f" "`echo "$f" | sed s/IMG/Vacation/`"; done
Example 11. Add the current timestamp to all files ending in .log
. This would change a file such as access.log
to access_20210418040151.log
.
$ for f in *.log; do mv "$f" "${f%.log}"_`date +%Y%m%d%H%M%S`.log; done
Rename Multiple Files With rename
The rename utility makes our bulk renaming tasks a bit easier, but the utility isn’t always installed by default. Use the appropriate command below to install it with your system’s package manager.
Ubuntu, Debian, and Linux Mint:
$ sudo apt install rename
Fedora, AlmaLinux, CentOS, and RHEL:
$ sudo dnf install prename
Arch Linux and Manjaro:
$ sudo pacman -S perl-rename
-n
option in your rename
commands if you want to preview the changes before making them.Example 1. Change all file names to lowercase.
$ rename 'y/A-Z/a-z/' *
Example 2. Change all file names to uppercase.
$ rename 'y/a-z/A-Z/' *
Example 3. Strip the .bak
extension from all files.
$ rename 's/\.bak$//' *.bak
Example 4. Change the extension of all .jpeg
files to .jpg
.
$ rename 's/\.jpeg/\.jpg/' *.jpeg
Example 5. Change the uppercase extension .JPG
to lowercase .jpg
for all files.
$ rename 's/\.JPG/\.jpg/' *.JPG
Example 6. Remove blank spaces from all file names.
$ rename "s/ *//g" *
Example 7. Replace blank spaces with underscores for all file names.
$ rename 's/\s+/_/g' *
Example 8. Capitalize the first letter of all file names.
$ rename 's/\b(\w)/\U$1/g' *
Example 9. Replace a pattern in every file name. This command will replace “IMG” with “Vacation” in every .jpg
file.
$ rename 's/IMG/Vacation/' *.jpg
Example 10. Delete part of a file name. This command will remove “IMG_” from every .jpg
file.
$ rename 's/IMG_//' *.jpg
Example 11. Append the .bak
extension to all files.
$ rename 's/(.*)/$1.bak/' *
Rename Multiple Files With mmv
The mmv utility excels at renaming files based on patterns, such as removing or adding strings to all files, or rearranging parts of file names. It’s not usually installed by default, but you can use the appropriate command below to install mmv with your system’s package manager.
Ubuntu, Debian, and Linux Mint:
$ sudo apt install mmv
Fedora, AlmaLinux, CentOS, and RHEL:
$ sudo dnf install mmv
Arch Linux and Manjaro (install from AUR):
$ git clone https://aur.archlinux.org/mmv.git $ cd mmv/ $ makepkg -si
-n
option in your mmv
commands if you want to preview the changes before making them.Example 1. Change the extension of all .jpeg
files to .jpg
.
$ mmv '*.jpeg' '#1.jpg'
Example 2. Change all file names to lowercase.
$ mmv '*' '#l1'
Example 3. Change all file names to uppercase.
$ mmv '*' '#u1'
Example 4. Rearrange parts of a file name. This command will change music files with pattern Song-Artist.mp3
to Artist-Song.mp3
.
$ mmv '*-*.mp3' '#2-#1.mp3'
Example 5. Replace the first occurrence of “IMG” with “Vacation” in all file names.
$ mmv '*IMG*' '#1Vacation#2'
Example 6. Add a prefix to every file name. This command will prepend “backup_” to every .log
file.
$ mmv '*.log' 'backup_#1.log'
Example 7. Add a suffix to every file name. This command will append “_backup” to every .log
file.
$ mmv '*.log' '#1.log_backup'
Example 8. Remove the “IMG_” prefix from all .jpg
files.
$ mmv 'IMG_*.jpg' '#1'
Example 9. Remove the “_old” suffix from all files.
$ mmv '*_old' '#1'
Bulk Rename Files via GUI
If the command line isn’t really your thing, it’s possible to rename multiple files at once via GUI. One of the best tools to do the job is the Bulk Rename application that comes with Thunar.
Thunar is the default file browser for Xfce, but you can install it regardless of what desktop environment you’re using. If you’re already using Xfce or Thunar, then Bulk Rename is already part of your system. Otherwise, use the appropriate command below to install it with your system’s package manager.
Ubuntu, Debian, and Linux Mint:
$ sudo apt install thunar
Fedora, AlmaLinux, CentOS, and RHEL:
$ sudo dnf install thunar
Arch Linux and Manjaro:
$ sudo pacman -S thunar
Step 1. After Thunar is installed, search for and open the Bulk Rename utility.
Step 2. Click on the plus sign to add files that you’d like to rename.
Step 3. Choose from the list of options what you’d like to do with the files. You can change them all to uppercase or lowercase letters, add a date and time, insert text at a certain position, add automatic numbering, replace characters, or search and replace certain patterns in the file names, among other things. In the example below, we’ve appended a .txt
file extension to all of our files.
Step 4. When you’re happy with the previewed name changes in the “New Name” column, click on “Rename Files” to perform the bulk renaming.
Renaming Multiple Files on WSL
If you’re using Windows Subsystem for Linux, some commands above won’t work, particularly those that involve changing files to lowercase or uppercase. Windows is case-insensitive, thus considers File.txt
, file.txt
, and FILE.txt
to all be the same file.
You’ll simply get an error that the file already exists if you try to rename FILE.txt
to file.txt
. To avoid this issue, use one of the following examples.
Example 1. Change the uppercase extension .JPG
to lowercase .jpg
for all files (using default utilities).
$ for f in *.JPG; do mv "$f" "${f%.JPG}.jpgaux"; done && for f in *.jpgaux; do mv "$f" "${f%.jpgaux}.jpg"; done
Example 2. Change the uppercase extension .JPG
to lowercase .jpg
for all files (using rename utility).
$ rename 's/\.JPG$/\.jpgaux/' *.JPG && rename 's/\.jpgaux$/\.jpg/' *.jpgaux
Example 3. Change the uppercase extension .JPG
to lowercase .jpg
for all files (using mmv utility).
$ mmv '*.JPG' '#1.jpgaux' && mmv '*.jpgaux' '#1.jpg'
Example 4. Change all file names to lowercase (using default utilities).
$ for f in `ls | grep [A-Z]`; do mv "$f" "`echo $f.aux | tr 'A-Z' 'a-z'`"; done && for f in `ls *.aux`; do mv $f ${f%.*}; done
Example 5. Change all file names to lowercase (using mmv utility).
$ mmv '*' '#l1.aux' && mmv '*.aux' '#1'
Conclusion
It’s possible to use default Bash utilities for all of your bulk renaming on Linux, but the rename and mmv tools make it a lot easier. We can also use Thunar’s Bulk Rename application when we prefer to use GUI over the command line. The examples in this guide cover a wide range of renaming scenarios and can be easily adapted to fit other needs.
Thunar Bulk rename is worth mentioning as a GUI tool to do the job.
Thanks for the suggestion. We’ve incorporated it into the article.