Rename Linux Files with Rename

21

When working with files, renaming them is one of the most basic actions. In Linux, renaming a file or directory is mostly done using mv. However, when it comes to renaming multiple files or folders, using mv becomes complex. It requires constructing complex loops and piping to get the job done. The rename command is specifically designed for such situations.

Rename is a powerful batch renaming tool that uses Perl expression to rename multiple files and directories in a single command. In this guide, check out how to rename files with rename.

Installing Rename

The rename command doesn’t come pre-installed. Thankfully, we can install it from the official package servers of any Linux distro.

There are two versions of tool, each with its own syntax and feature. For this guide, we’ll focus on the one that uses Perl.

In the case of Debian/Ubuntu, rename is available as the package “rename”. Install the package.

$ sudo apt update

$ sudo apt install rename

In the case of CentOS and Fedora, the package name is different. It’s known as “prename”. Install the package.

$ sudo yum install prename

In the case of Arch Linux, the package name is “perl-rename”. Install the package.

$ sudo pacman -S perl-rename

Using Rename

Let’s have a look at various ways of using the rename command.

Basic Command Structure

The tool follows the following command structure:

$ rename options> perl_expression> files>

The rename command is actually a Perl script. It operates using the Perl expression. As you can guess, using it to the fullest requires expertise in Perl regular expressions.

Although there are numerous ways of constructing the Perl expression, the following expression is quite common to come across when working with the rename command.

$ s/search_pattern>/replacement>/

Basic usage

Here, I have a bunch of dummy files ready to be renamed.

The following rename command will rename all the file extensions from TXT to TEXT.

$ rename ‘s/.txt/.text/’ *.txt

Here, the target files to rename is expressed using the wildcard (*) symbol.

Verbose Mode

By default, rename doesn’t print much output. However, having more in-depth details on its actions can help in various scenarios, especially when it comes to debugging. In verbose mode, rename prints out more info about the ongoing task to better understand and identify possible issues.

To run rename in verbose mode, add the “-v” flag.

$ rename -v ‘s/.txt/.text/’ *.txt

Dry Run

When running rename, the changes are made permanent. In many situations, it will lead to unnecessary trouble. To prevent unwanted changes to file and folder names, we can test out the command using a dry run.

A dry run is basically a test run that doesn’t change anything with the actual files and folders. However, using verbose mode will print the output as if the command was actually performed. That’s why it’s always a good idea to dry run with verbose mode. It’s recommended to perform a dry run before running any rename command on important files and folders.

To perform a dry run, add the “-n” flag.

$ rename -v -n ‘s/.txt/.text/’ *.txt

Overwriting Existing Files

The default behavior of rename is not to overwrite the already-existing files. If you don’t fear overwriting the existing ones, then you can force rename to overwrite using the “-f” flag.

$ rename -v -f ‘s/.txt/.text/’ *.txt

Sample Rename Perl Expressions

The basic structure of the rename command is very simple. Its main power is the incorporation of the Perl regular expressions. Regular expression is a clever way of describing a pattern and specifying the actions. For beginners, here’s a guide on regular expression using grep and egrep. Although using different tools, the fundamentals of regular expression still apply.

Here’s a shortlist of some common rename Perl expressions. Note that these are only for reference. Before putting them into use, always perform a dry run to ensure that it’s working as intended.

Replacing spaces

The following expression will replace spaces in filenames with an underscore.

Converting file names to uppercase

Need the file names in uppercase? The following Perl expression combined with the rename command will do just that.

Converting file names to lowercase

The following Perl expression, when used with rename, will rename the filenames from uppercase to lowercase.

Removing parts of the file names

If filenames contain unnecessary contents, we can always remove them using Perl expression. For example, to rename backup files (.bak) to the original files, we can simply remove the “.bak” portion from the file name.

Renaming multiple matching patterns

Let’s say you’re with TEXT and TXT files. Despite different file extensions, both are in the same format. Instead of dealing with both TEXT and TXT, we can rename all of them to TXT files.

Similarly, instead of dealing with both JPEG and JPG, we can rename all of them to JPG.

Note that in both cases, the file extensions have similarities except for one character. That’s why we could use this shortcut. If the file extensions are completely different, then it won’t work.

Conclusion

The rename command is a simple tool to master. It’s really powerful when you’re working with tons of files and renaming them into various formats. It’s also safe to incorporate into bash scripts for automation. If you’re a beginner, here’s a wonderful starting guide on bash scripting.

Happy computing!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More