Renaming a single file in Linux is simple using the mv command. But when you need to rename multiple files, things can get more complicated. Thankfully, Linux offers several easy methods for batch renaming files.
In this comprehensive guide, we will cover the best tools and techniques to rename multiple files in Linux. We‘ll demonstrate how to:
- Rename files using the built-in
mvcommand - Use advanced renaming utilities like
rename,mmvandqmv - Rename files based on regular expressions, patterns and wildcards
- Recursively rename files in subdirectories
- Preview changes before renaming
Whether you need to remove file extensions, change case, append text or whatever other batch operation, you‘ll be equipped with the knowledge to do it efficiently. Let‘s get started!
Renaming Multiple Files with MV
The mv (move) command in Linux can rename one or more files in a single operation. For a single file, you simply run:
mv old_filename.txt new_filename.txt
To rename multiple files with mv, we need to utilize a for loop to iterate through all the files we want to rename.
Here is an example loop that appends _new to every .txt file in the current directory:
for file in *.txt; do
mv $file ${file}_new
done
Breaking this down:
for file in *.txt;: Loop through all files matching the*.txtwildcard patternmv $file ${file}_new: Runmvcommand to rename current file ($file) appending_new- The
; doneterminates theforloop when all files have been processed
The ${file} syntax extracts just the file name without the extension. This allows us to append _new before re-adding the original .txt extension.
We can combine this technique with other mv features like changing extensions, case and more. For example, renaming all .txt to .text files:
for file in *.txt; do
mv $file ${file/txt/text}
done
Here we used parameter expansion (${file/txt/text}) to substitute txt with text in the file name.
The mv method works on any Linux distribution without requiring any additional tools. However, it can get unwieldy for more advanced rename operations. Next we‘ll cover more flexible utilities suited for complex batch renames.
Advanced File Renaming Utilities
Most Linux distributions have advanced file renaming utilities that support regular expressions (regex), substitution rules and more. They allow you to perform almost any batch rename operation conceivable.
Some popular examples include:
rename: Powerful Perl-based tool for complex regex renamesmmv: Move/copy/append/link multiple files by substituting textqmv: Quicken Move – an interactive renmaing utilityimv: Image metadata-based renaming
These are not installed by default on Linux, but are readily available through most distro package managers.
For example, on Ubuntu/Debian we can install rename and mmv like this:
sudo apt install rename mmv
Next we‘ll demonstrate how to use each of these utilities to effectively rename batches of files.
The Rename Command
The rename command allows you to rename multiple files via regex substitution rules. Here is the basic syntax:
rename [options] ‘s/regex_pattern/replacement_string‘ files
Some examples:
-
Convert
.txtextensions to.text:rename ‘s/\.txt$/.text/‘ *.txt -
Replace spaces with underscores:
rename ‘s/ /_/g‘ * -
Rename files by changing case:
rename ‘y/A-Z/a-z/‘ * # Change to lowercase rename ‘y/a-z/A-Z/‘ * # Change to uppercase
You can chain multiple substitution operations together using semicolons:
rename ‘s/\.txt/.text/; s/file/doc/‘ *.txt
This one-liner converts extensions to .text and replaces instances of file with doc in file names.
For complex substitutions, it can be easier to instead save them to a script like:
#!/bin/bash
for file in *.txt; do
rename ‘s/\.txt$/.text/‘ $file
rename ‘s/file/document/g‘ $file
done
This loops through each .txt file, first changing the extension then replacing all file substrings with document.
By leveraging rename‘s advanced regex support, you can match and replace virtually any pattern for powerful batch renames.
The mmv Utility
The mmv command is designed specifically for moving, copying and linking multiple files through wildcard matching.
The basic syntax is:
mmv ‘source_pattern‘ ‘destination_pattern‘
For simply renaming files, we pass the same source and destination specification:
mmv ‘*old_name*.txt‘ ‘#1new_name.text‘
Let‘s break down what‘s happening here:
*old_name*.txt: Source matching – selects files likemy_old_file.txt#1: Represents the matching text in the source pattern (myin this case)new_name.text: Destination name – renames to something likemy_new_file.text
Now let‘s walk through some examples:
-
Convert extensions from
.txtto.text:mmv ‘*.txt‘ ‘#1.text‘ -
Rename files by adding a prefix:
mmv ‘*file.txt‘ ‘new_#1‘ -
Replace spaces with underscores across all files:
mmv ‘* *‘ ‘#1_#2‘
We can match multiple terms using #1, #2, etc and reorganize their order in the destination pattern.
The mmv command is slower than rename but more intuitive when using wildcards for rearranging file names.
QMV – An Interactive Renaming Utility
The qmv (quick move) command built into renameutils provides an interactive way to rename multiple files.
To use it:
-
Navigate to the directory with your files
-
Run the command:
qmv * -
An editor will open up listing all files for renaming
-
Make your changes, save and close – files will be instantly renamed
For example, to convert a batch of .txt files to .text, you‘d:
-
Run
qmv *.txt -
Edit each
.txtextension to.text -
Save and exit – simple as that!
The interactive editor makes qmv quick and easy to use. And it supports powerful regex substitutions like rename too.
Here are some example regex search/replace operations:
-
Strip file extensions:
s/\.[a-zA-Z0-9]+$// -
Convert spaces to underscores:
s/\s/_/g
You can run qmv recursively (qmv **) to rename files in subdirectories too.
Overall, qmv gives you an intuitive and regex-capable batch file renaming solution.
IMV – Metadata Based Renaming
The imv tool specializes in renaming image files based on EXIF metadata like capture date/time.
For example to rename images under a directory like 2022-01-30_11-25-55.jpg, you would run:
imv -f "%Y-%m-%d_%H-%M-%S" *.jpg
Breaking this down:
-f "%Y-%m-%d_%H-%M-%S": Rename pattern using date/time format codes*.jpg: Select all JPEG images in current directory
So imv extracts the original photo timestamp and constructs a file name matching your specified format.
This makes it trivial to organize batches of images under uniform dated filenames, ideal for managing media libraries.
The utility also has options for preserving current names and specifying offsets to avoid collisions after renaming.
For photography collections and media management, imv is an indispensable tool for synchronizing filenames based on EXIF data.
Advanced Rename Techniques
So far we covered the core utilities and standard usage for renaming multiple files in Linux.
Here are some more advanced techniques that can prove useful:
Recursively Rename Files in Subdirectories
The rename tools rename, mmv and qmv have built-in support for recursing into subdirectories.
This allows you to:
- Rename batches of files scattered across folders
- Consistently apply naming policies across your system
Here is an example using mmv:
mmv -r ‘My Music/#2/#1.mp3‘ ‘Music/#1/#2.mp3‘
The -r flag enables recursive mode. This restructures MP3 files like:
My Music/Artist/song.mp3
TO
Music/song/Artist.mp3
So we extracted the base file name and artist folder to rearrange into the preferred structure.
Recursively handling files means greatly simplified system organization. No matter how unstructured your initial setup, you can uniformly rename every file into logical order.
Preview Renames Before Execution
Blindly executing mass renames can be dangerous if the regex/wildcards don‘t exactly match your intent.
Thankfully several utilities have preview flags to safely test run changes before applying them.
For example with rename, include the -n or --no-act option:
rename -n ‘s/\.txt$/.text/‘ *.txt
This will go through the motions and print each rename operation without actually changing anything.
Once you verify the expected output, remove the -n flag to execute the changes.
The mmv command likewise has a built-in simulation mode. Append an N character like:
mmv ‘*old*.txt‘ ‘#1new.text‘N
Previewing gives you confidence that renames will apply correctly before impacting production files.
Schedule Batch Renames via Cron
For organization tasks that need to run periodically, you can schedule the file rename scripts using Cron.
Cron is the time-based job scheduler on Linux. By adding entries to the crontab file, we can automate commands:
# Rename media files at 12am daily
0 0 * * * /scripts/rename-media.sh
The rename-media.sh script would contain your desired file transformations.
Some examples of cron-based renaming tasks:
- Monthly report generation with updated timestamps
- Weekly restructuring of log files by date
- Daily normalization of downloaded image names
Rather than remembering to manually issue complex rename operations, Cron handles it reliably in the background.
Conclusion
Renaming multiple files on the Linux CLI doesn‘t have to be difficult or tedious. The methods highlighted in this guide provide flexible approaches for any batch file renaming scenario.
To recap, we covered how to efficiently rename multiple files using:
- The built-in
mvcommand - Advanced utilities like
rename,mmv,qmvand more - Wildcards, regular expressions and substitution rules
- Recursively traverse directories
- Preview intended rename operations
No matter your specific requirements – changing case, appending text, stripping extensions, reordering elements, etc – one of these renaming tools is up for the job!
By mastering Linux file renaming, you can whip any messy structure into organized shape. Saving immense headaches when managing lots of data.
So try out these techniques for streamlining your folder and file administration. And rename your way to order out of chaos!


