Renaming files and directories is one of the most basic tasks you often need to perform on a Linux system. Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users who are new to Linux. You can rename files using a GUI file manager or via the command-line terminal.
On this page
If you work with files on the command line in Linux, renaming files is one of the most frequent tasks you may find yourself involved in. We've already discussed the mv command that lets you do this. And here, in this tutorial, we will discuss another such tool, dubbed rename.
But before we start with that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.
Linux rename command
As the name suggests, the rename command in Linux allows you to rename files. Following is its syntax:
And here's how the tool's man page explains it:
Note that if you don't have the rename command installed, you can get it using the following command:
Following are some Q&A-styled examples that should give you a better idea of how the rename command works.
Q1. How to use the rename command?
Basic usage isn't exactly straight forward, I must say, but it's not difficult to understand. Let's say you have a file named 'test.txt' and you want to rename it to 'new.txt'. Then here's how you use the rename command to do this.
Q2. How rename handles symbolic links?
By default, the rename command just renames the file which is passed as input, even if it's a symbolic link. However, if you want the tool to not rename symbolic links, but act on their targets instead, then use the -s command line option.
So if symlink.txt is a symbolic link that points to root.txt, then the following command will make sure the rename operation happens for root.txt:
Q3. How to make a dry run with rename?
Suppose you only want to see the final change that'll happen with a rename command, without actually carrying it out, then use the -n command line option. For example, the following command:
produced the following output:
but didn't actually rename new.txt to test.txt.
Q4. How rename handles overwriting of files?
By default, the rename command doesn't perform the operation if it involves overwriting an existing file. However, you can force the tool to overwrite using the -f command-line option.
For example, the following command:
produced the following output:
But when the -f command was used:
The operation completed smoothly and test.txt got overwritten.
Conclusion
The rename command doesn't offer too many options, and we've already discussed some of the main ones here. After you're done practicing these, head to the tool's man page to learn more about it.
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 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:
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.
![Rename file linux commandline Rename file linux commandline](/uploads/1/2/5/5/125539121/524320331.jpg)
Example:
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:
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:
![Rename Rename](/uploads/1/2/5/5/125539121/141657540.png)
Command:
Output:
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.
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.
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:
Step 1: Create a file for the script in the directory containing the files you wish to rename.
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!)
Step 3: Make the file executable.
Step 4: Execute your script by entering the following command.
Here’s what it would look like in action.
Files:
Command:
Output:
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.