Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 29, 2024
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.
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:
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.
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:
Next, let’s discuss the regex pattern itself:
^.* - (\d)x(\d{2}) .*$
This pattern holds the season and episode numbers:
Further, let’s specify the replacement format S0$1E$2.srt:
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.
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.