Batch Rename Files Using 'rename' and Regex
-
Jason Yang - 12 Nov, 2025
- Updated 04 Jan, 2026
- Views —
Stop wasting time with single mv commands. For any serious Linux user or developer, mastering the rename command with Perl Regular Expressions (Regex) is essential for efficient file management. It’s the cleanest, fastest way to standardize and declutter large directories.
The Power Formula
The rename utility uses the standard Perl substitution syntax: s/find/replace/g.
rename 's/find_pattern/replace_string/g' files_to_target
This single-line command allows you to define complex search patterns and execute changes across thousands of files instantly.
Removing a Time Stamp Prefix
Instead of removing questionable branding, let’s tackle a common problem: removing useless metadata or time stamps from filenames.
Suppose your files look like this: [2025-10-29]-1. Welcome.mp4 and you want to remove the date stamp.
rename 's/^\[\d{4}-\d{2}-\d{2}\]-//' *.mp4
| Regex Element | Purpose |
|---|---|
^ | Anchors the search to the start of the filename. |
\[, \] | Escapes the brackets, matching the literal [ and ]. |
\d{4} | Matches exactly four digits (YYYY). |
\d{2} | Matches exactly two digits (MM or DD). |
// | The substitution replaces the entire matched pattern with nothing (deletion). |
Compressing Multiple Spaces
Ever deal with filenames riddled with double or triple spaces? A simple Regex can normalize all inconsistent spacing to a single space.
Target filenames like: 1. Welcome Aboard! (Extra Spaces).mp4
rename 's/ {2,}/ /g' *.mp4
| Regex Element | Purpose |
|---|---|
{2,} | Matches two or more consecutive space characters. |
/ / | Replaces the entire block of multiple spaces with a single space. |
/g | Ensures this replacement is applied globally throughout the filename. |
Recursive Renaming with find
When your files are in nested subdirectories, you must combine find with rename.
- Navigate to the parent directory:
cd /path/to/your/main/directory
- Execute the combined command:
find . -type f -name "*.mp4" -exec rename 's/-\[HR\]-//g' {} +
This is the most powerful method: find recursively locates all files matching the pattern, and the -exec flag efficiently runs the rename substitution on each one.
Installation Check
If you encounter a rename: command not found error on your system (common on some minimal installs), simply run:
sudo apt install rename
Mastering the rename command with Regex is a crucial step towards true Linux proficiency. Enjoy your beautifully organized directories!
Would you like to explore how to use capture groups (e.g., $1, $2) in rename to reorder parts of a filename? That is another powerful Regex technique!