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: May 17, 2024
Renaming files is a common task for any Linux system administrator. Moreover, renaming multiple files at once can save us a lot of time. We’ll use the rename command to accomplish this task efficiently.
In this tutorial, we’ll explore the rename command with clear explanations and practical examples.
The rename command helps to change the name of a single file or multiple files simultaneously depending on a specific pattern. What’s more, its syntax follows a simple format:
$ rename [options] 's/old_pattern/new_pattern/' files
Let’s explore the syntax above:
Now, let’s discuss the substitution pattern.
The rename command transforms filenames based on the substitution pattern. To clarify, we declare these patterns using regular expressions:
Now, let’s look at practical examples.
In a Debian-based system, we can easily install this tool with apt:
$ sudo apt install rename
Once this is done, let’s see its most basic use, which is renaming a single file:
$ rename 's/report.txt/presentation.pdf/' report.txt
This command changes the filename report.txt to presentation.pdf.
First, let’s start by adding a prefix to a single file. To demonstrate, we’ll rename the file file.txt and add a string between the .txt extension and the file part:
$ rename 's/(.+)\.(.+)/$1_edited.$2/' file.txt
Here’s the breakdown of the pattern used above:
This command selects file.txt, captures the string file in the filename, and appends _edited to it. Therefore, the filename for the file file.txt changes to file_edited.txt.
Next, we can also add a prefix to multiple files:
$ rename 's/^/photo_/' *.jpg
Let’s analyze this command:
This command prepends photo_ to all image files with the .jpg extension in the current directory.
Here, let’s utilize rename to replace spaces with underscores in filenames:
$ rename 's/ /_/g' *
So, let’s break down the command:
Meanwhile, the g flag specifies that all occurrences of the space character are to be replaced, not just the first one.
Next, let’s remove characters that are not letters, digits, or periods from the filenames:
$ rename 's/[^a-zA-Z0-9.]//g' *
Let’s analyze the regular expression pattern:
For instance, the filename for file!@#.txt switches to file.txt.
We can also use the rename command to replace file extensions:
$ rename 's/\.txt$/\.md/' *.txt
Let’s analyze the pattern:
So, when we execute this command in a directory with multiple files, it renames all files with the .txt extension to contain the .md extension instead. For example, a filename for example.txt changes to example.md.
The rename command offers various options that boost its functionality.
To illustrate, let’s utilize the sample files file_1.txt, file_2.txt, file_3.txt, and file_4.txt:
$ ls
file_1.txt file_2.txt file_3.txt file_4.txt
Now, let’s explore the different options.
The -n option instructs rename to show what changes would look like without actually renaming any files:
$ rename -n 's/file_/file/' file*.txt
rename(file_1.txt, file1.txt)
rename(file_2.txt, file2.txt)
rename(file_3.txt, file3.txt)
rename(file_4.txt, file4.txt)
This command shows that the file_1.txt, file_2.txt, file_3.txt, and file_4.txt files would be renamed to file1.txt, file2.txt, file3.txt, and file4.txt, respectively.
The -v option enables instructs rename to display the files being renamed:
$ rename -v 's/file_/file/' file*.txt
file_1.txt renamed as file1.txt
file_2.txt renamed as file2.txt
file_3.txt renamed as file3.txt
file_4.txt renamed as file4.txt
Above, the command provides information on each renaming operation. So, let’s confirm the changes:
$ ls
file1.txt file2.txt file3.txt file4.txt
Here, we see that the files are renamed as indicated with the help of the -v option.
With this option, we rename files while forcefully overwriting existing files with the same name. To demonstrate, let’s add file1.txt to our files:
$ ls
file_1.txt file1.txt file_2.txt file_3.txt file_4.txt
By default, we get a message that the file is not renamed when we try to overwrite an existing file:
$ rename 's/file_/file/' file*.txt
file_1.txt not renamed: file1.txt already exists
Above, file_1.txt is not renamed since file1.txt already exists. Therefore, let’s use the -f option to solve this issue:
$ rename -f 's/file_/file/' file*.txt
Here, -f ensures that file_1.txt is changed to file1.txt and overwrites the existing file1.txt file.
In this article, we showed that rename is resourceful when renaming files in Linux.
The rename command is flexible and allows us to perform simple and complex renaming tasks using regular expressions. For example, it offers us several options that we can use to customize the renaming process, like previewing renaming operations and forcing the renaming without a prompt for confirmation. With these options, the rename command helps us improve how we manage files.