Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:
Renaming Files With Regular Expressions and rename in Linux
Last updated: December 29, 2024
1. Overview
Although renaming files is a common task, sometimes, when having to rename many, it can become tedious. The Linux shell supports powerful tools we can use with regular expressions to rename files with a common pattern. For example, the rename command lets us transform file names based on such patterns.
In this tutorial, we’ll show how to rename files using regex.
2. Example Subset
To demonstrate, we work with subtitle files the names of which are in a specific format:
$ ls -l
total 0
-rw-rw-r-- 1 maurice maurice 0 Dec 24 18:00 'Superman - 6x03 - The Man of Steel.srt'
-rw-rw-r-- 1 maurice maurice 0 Dec 24 18:00 'Superman - 6x05 - Kryptonite Unleashed.srt'
-rw-rw-r-- 1 maurice maurice 0 Dec 24 18:00 'Superman - 6x20 - Battle for Metropolis.srt'
...
So, let’s analyze the file name structure:
'Superman - 6x03 - The Man of Steel.srt'
In particular, there are two main parts:
- 6 – represents the season number
- 03 – represents the episode number
The other parts of the file name are unnecessary for the new format. This is because our goal is to rename these files into a cleaner format that specifies the season and episode numbers only:
S06E03.srt
S06E05.srt
S06E20.srt
...
Therefore, we only extract the season and episode numbers from the original file names and reformat them. For this, we can use the rename command with regular expressions.
3. Renaming the Files
Before we proceed, we need to ensure that rename is installed on the system.
Firstly, let’s create the command we use to accomplish the task at hand:
$ rename -n 's/^.* - (\d)x(\d{2}) .*$/S0$1E$2.srt/' *.srt
rename(Superman - 6x03 - The Man of Steel.srt, S06E03.srt)
rename(Superman - 6x05 - Kryptonite Unleashed.srt, S06E05.srt)
rename(Superman - 6x20 - Battle for Metropolis.srt, S06E20.srt)
...
Here, we combine the regex pattern and replacement format in the rename command:
- -n performs a dry run to display what the command would do first without making the changes, in this case, renaming the files
- *.srt applies the command to all .srt files in the current directory
Next, let’s discuss the regex pattern itself:
^.* - (\d)x(\d{2}) .*$
This pattern holds the season and episode numbers:
- ^.* matches any characters at the start of the file name
- (\d)x(\d{2}) holds the season number (\d) and the episode number (\d{2}) after the dash (–)
- .*$ matches the remaining part of the file name
Further, let’s specify the replacement format S0$1E$2.srt:
- S0 adds a leading zero to the season number, in this case, S06
- $1 represents the first capture group holding the season number, in this case, 6
- E adds a literal E
- $2 represents the second capture group holding the episode number, in this case, 03, 05, or 20
Of course, we can add capture groups to also get the original names.
Once we ensure the dry run works as intended, we can remove the -n flag:
$ rename 's/^.* - (\d)x(\d{2}) .*$/S0$1E$2.srt/' *.srt
Removing the -n flag means we actually perform the rename operation:
$ ls -l
total 0
-rw-rw-r-- 1 maurice maurice 0 Dec 24 18:00 S06E03.srt
-rw-rw-r-- 1 maurice maurice 0 Dec 24 18:00 S06E05.srt
-rw-rw-r-- 1 maurice maurice 0 Dec 24 18:00 S06E20.srt
...
The resulting files now contain different file names.
4. Conclusion
In this article, we explored how to rename files using regular expressions in Linux via the rename command.
We can utilize this task to save time and effort. For instance, we can easily use it to automate renaming multiple files once we understand the structure of the file names and the regex patterns needed. Therefore, the rename command can be helpful to system administrators when they’re working with large file collections.