How to rename multiple files on Linux

We will show you how to rename multiple files in Linux. Every operating system in the modern world comes with several ways to interact with the file system. Whether it’s creating files, renaming them, copying them or deleting them, they all come with functionality to do all of these tasks efficiently. However, one operation that most operating systems fail to do is rename multiple files at the same time. If you need a template, if you just want to rename many files in one go, renaming files can be quite tedious.

Fortunately, renaming the software can help us fix this problem. In this article, we will take a look at several options for batch renaming files on Debian based systems as well as RHEL based systems (RedHat Enterprise Linux).

Debian based operating systems

Debian and similar distributions such as Ubuntu and Linux Mint have a built-in command line tool for batch renaming files. “Rename”, offered on some Debian-based operating systems, is one of the most powerful renaming tools on Linux. The renaming work is very similar to how “sed” works – it is essentially a sed utility for filenames. It can also use a Perl expression, so if you have some knowledge of Perl this utility will work for you.

Important: On some Debian based operating systems on which “rename” will be installed may actually have the “rename” variant from the util-Linux package and package. This util-linux version is a much simpler version of “rename” that doesn’t have nearly as many features as the version that ships on Debian and Ubuntu.

File Renaming Basics

You can find and replace text in file names by running the following command. Make sure to change the Search and Replace value to the text you want to change. Keep in mind that masks are available inside the search box.

Note: You can use the -n option to dry start first, which does not affect filenames. The -v option makes all operations verbose.

Example:

rename -v 's/search/replace/' *

Changing file types

You can also rename the file types of specific files using this method – just replace “search” and “replace” with the new file type.

Note: Do not forget to add a backslash before the file type, otherwise the command will not work.

Example:

rename -v 's/.search/.replace/' *

Other additional commands

If you want to rename files and have an increasing number at the end of the filename, this can be done using Perl as well:

rename -v 's/search/our $i; sprintf("replace%03d.txt", 1+$i++)/e' *

The reason “% 03d” is in this command is to describe how many zeros we want to add before our numbers. This is used to store files when sorting files alphabetically. “Sprintf” is actually a Perl function used to format the name. It shows one of the strengths of this version of the rename. Here’s an example.

files:

file1

file2

Team:

rename -v 's/file*/our $i; sprintf("name%03d.txt", 1+$i++)/e' *

Output:

name001

name002

In case you need a specific format for your filenames, there are many Perl scripts available on the Internet to rename files in all kinds of formats.

RHEL based operating systems

RedHat Enterprise Linux and related distributions such as CentOS also have a “rename” version installed by default. This is a “Util-Linux” variant, not as feature-rich as the one found on most Debian-based systems.

File Renaming Basics

As far as the Util-Linux version goes, it all makes a simple search and replace operations for filenames. You enter the string you are looking for in the files and indicate which string you want to replace in the search string.

rename "search" "replace" *

This is all the functionality available with the built-in rename utility on RHEL based systems. If you need to do more advanced renaming, bash scripting using loops and operations tied together is fine. Here’s an example of renaming files while adding an increasing number to the end of the file. Just change ‘search’ and ‘replace’ in the script to the corresponding names you are looking for and replace. Here’s an example:

Step 1: Create a script file in the directory containing the files you want to rename.

touch renameScript.sh

Step 2: Open the file in a text editor and add this script to the file. Close your text editor when you’re done. (Don’t forget to change “search” and “replace” in the script for your search!)

find -name 'search' | # find file

gawk 'BEGIN{ a=1 }{ printf "mv %s replace%03dn", $0, a++ }' 

bash # run

Step 3: Make the file executable.

chmod 644 renameScript.sh

Step 4: Execute the script by entering the following command.

sh renameScript.sh

This is what it will look like in the output.

files:

file1

file2

Team:

sh renameScript.sh

Output:

name001

name002

Sidebar