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.
In this short tutorial, we’ll show you how to rename a file in Linux. Most commonly these commands are used on cloud servers and work on most Unix-based systems, including CentOS and Ubuntu. This is our fist tutorial in a series of quick and short Linux tutorials.
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 on how the rename command works.
Q1. How to use 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.
This question already has an answer here:
- Rename files in directory 6 answers
- How to clean up file extensions? 7 answers
I have the below list of files
that I need to rename to
UdhayakumarUdhayakumar
marked as duplicate by terdon♦, slm♦, Bernhard, Joseph R., AnthonNov 26 '13 at 18:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3 Answers
Most standard shells provide a way to do simple text substitution within shell variables. http://tldp.org/LDP/abs/html/parameter-substitution.html explains as follows:
So use this script to loop through all the appropriate files and rename each of them:
I have added a -i option so you have the chance to confirm each renaming operation. As always, you should make a backup of all your files before doing any large amount of renaming or deleting.
Mark PlotnickMark Plotnick
If you don't have Perl's
rename
:If you do have Perl's
rename
:Joseph R.Joseph R.
Before trying complex commands like the following, backup your files. You never know what a typo (mine or yours) can cause.
With
mv
(as you asked in comment --- rename
as suggested in the other answer is probably safer, especially if you can have spaces or strange chars in your filenames) something of the styleRmanoRmano