How to Rename Multiple Files on Linux

How to Rename Multiple Files on Linux

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

Luckily, renaming software can help us solve this issue. In this tutorial, we will go through several options for batch renaming of files on Debian-based systems, as well as RHEL-based (RedHat Enterprise Linux) systems.

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 OSes, is one of the most powerful renaming tools on Linux. Rename works very similar to the way ‘sed’ works – it is essentially the sed utility for filenames. It can also use Perl expressions, so if you have some knowledge of Perl, this utility will work wonders for you.

Important: Some Debian-based operating systems that have ‘rename’ installed might actually have a version of ‘rename’ from the ‘util-linux’ package. This util-linux variant is a much simpler version of ‘rename’ which doesn’t have nearly as many features as the version that comes on Debian and Ubuntu.

Basic File Renaming

You can find and replace text in filenames by executing the following command. Make sure to change the values of ‘search’ and ‘replace’ to the strings of text you wish to affect. Keep in mind that wildcards are available inside of the search field. Note: You can use the -n option to perform a ‘dry run’ first, which does not affect your file names. The -v option makes all operations verbose.

Example:

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

Changing filetypes

You can also rename the filetype of certain files by using this method – just replace ‘search’ and ‘replace’ with the current and new filetype. Note: Don’t forget to add a backslash before your filetype, or else the command won’t work.

Example:

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

More Advanced Commands

If you would like to rename files and have a number incrementing at the end of the filename, this can be done as well with some Perl:

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

The reason for the ‘%03d’ in this command is to describe how many zeroes we want in front of our numbers. This is used to keep files in order when sorting files alphabetically. ‘sprintf’ is actually a function from Perl, used to format the name. This shows one of the strengths of this version of rename. Here is an example.

Files:

file1

file2

Command:

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

Output:

name001

name002

And in case you need a specific format for your filenames, there are plenty of Perl scripts available online for renaming files in all sorts of formats.

RHEL-based Operating Systems

RedHat Enterprise Linux and its related distributions such as CentOS also have a version of ‘rename’ installed by default. It is the ‘util-linux’ variant, meaning that it is not as feature-rich as the one installed on most Debian-based systems.

Basic File Renaming

As far as the util-linux version of rename goes, all it does is simple search-and-replace operations on filenames. You enter what string you are searching for in your files, then you enter what string you would like to replace the search string with.

rename "search" "replace" *

This is all of 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 chained together will be the way to go. Here is an example for renaming files while adding an incrementing number to the end of the filename. Just change ‘search’ and ‘replace’ in the script to the respective names which you are searching for and replacing. Here is an example:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

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

touch renameScript.sh

Step 2: Open the file with your preferred text editor, and add this script to the file. When done, close your text editor. (Don’t forget to change ‘search’ and ‘replace’ in the script to your searched and replaced filenames!)

find -name 'search' | # find file

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

bash # run

Step 3: Make the file executable.

chmod 644 renameScript.sh

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

sh renameScript.sh

Here’s what it would look like in action.

Files:

file1

file2

Command:

sh renameScript.sh

Output:

name001

name002

And should you need a specific format for your filenames, there are plenty of bash scripts online designed around renaming files with many different formats.

Of course, you don’t need to rename your files using scripts on Linux, if you use one of our Blazing-Fast SSD VPS Hosting services – in which case you can simply ask our expert Linux admins to rename your files for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to rename multiple files on Linux, please share it with your friends on social networks using the buttons on the left, or simply leave a reply below. Thanks.

5 thoughts on “How to Rename Multiple Files on Linux”

  1. From Gnome 3.22 and forward, this is a built in feature in their filemanager (nautilus). Just select multiple files, right click and select “Rename…”.

    Reply
  2. If I using example from the text I have this:

    rename -nv ‘s/file*/our $i; sprintf(“name%03d.txt”, 1+$i++)/e’ *
    file1 -> name001.txt1
    file2 -> name002.txt2
    file3 -> name003.txt3
    file4 -> name004.txt4

    But if I use only “.*” I have correct names:

    rename -nv ‘s/.*/our $i; sprintf(“name%03d.txt”, 1+$i++)/e’ *
    file1 -> name001.txt
    file2 -> name002.txt
    file3 -> name003.txt
    file4 -> name004.txt

    I don’t understand why. :(

    Reply
  3. Slackware et al also have the not-so-good version of rename. Luckily, the version in Deb/buntu/Mint is actually a perl script, so it and its manpage are portable.
    I don’t understand the ‘change file type’ example. Is it trying to replace filename extensions? They are an MS thing and shouldn’t be used to determine file type. Gnome, looking at you. See ‘man magic’ for the details on linux/unix filetypes.

    Reply
    • Hi Roland,

      It is really useful example. Sometimes you may need to change a file extension from .txt to .html, .txt to .text , etc…

      Reply
  4. The rename utility is one way to rename multiple files, but you have overlooked a better alternative — mmv (multiple move) — which also have an important safety feature of not overwriting files due to collisions arising from syntax errors in the target specification.

    Reply

Leave a Comment