1. Overview

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.

2. Understanding the rename Command

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:

  • [options] – represents options to modify the behavior of the rename command
  • ‘s/old_pattern/new_pattern/’ – the substitution pattern that indicates the changes to make to the declared filenames
  • files – specifies the file or files to rename

Now, let’s discuss the substitution pattern.

2.1. Substitution Pattern

The rename command transforms filenames based on the substitution pattern. To clarify, we declare these patterns using regular expressions:

  • s – indicates it’s a substitution operation
  • old_pattern – specifies the substring to replace
  • new_pattern – specifies the substring to replace the old_pattern

Now, let’s look at practical examples.

3. Renaming Files

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.

3.1. Adding Prefixes

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:

  • (.+) – represents the first capturing group ($1) that matches everything before the last dot in the filename – in this case, file
  • \. – matches the last dot in the filename
  • (.+) – represents the second capturing group ($2) that captures the extension part of the filename – in this case, txt
  • / – separates the old pattern from the new pattern
  • $1_edited.$2 – the new pattern, which adds _edited after the first capturing group ($1)

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:

  • ‘/^/photo_/’ – adds photo_ at the beginning of each filename
  • *.jpg – targets all the files with the .jpg extension

This command prepends photo_ to all image files with the .jpg extension in the current directory.

3.2. Converting Spaces to Underscores

Here, let’s utilize rename to replace spaces with underscores in filenames:

$ rename 's/ /_/g' *

So, let’s break down the command:

  • ‘s/ /_/g’ – this regular expression instructs the rename command to replace all occurrences of spaces with underscores in filenames
  • * – matches all filenames in the current working directory

Meanwhile, the g flag specifies that all occurrences of the space character are to be replaced, not just the first one.

3.3. Removing Special Characters

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:

  • ‘[^a-zA-Z0-9.]’ – this pattern matches any character that is not a letter (uppercase or lowercase), a digit, or a period
  • // – specifies that any character matched by the [^a-zA-Z0-9.] pattern should be replaced with nothing, thereby removing these characters
  • * – represents a wildcard character that selects all files in the current directory as a target for renaming

For instance, the filename for file!@#.txt switches to file.txt.

3.4. Changing File Extensions

We can also use the rename command to replace file extensions:

$ rename 's/\.txt$/\.md/' *.txt

Let’s analyze the pattern:

  • \.txt$\.txt matches the string .txt in the filename while $ ensures that the match .txt is at the end of the filename
  • \.md/ – specifies that the matched string .txt should be replaced with .md

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.

4. Using rename With Options

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.

4.1. The -n Option

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.

4.2. The -v Option

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.

4.3. The -f 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.

5. Conclusion

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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.