Renaming a file is easy when you are using a mouse in a graphical explorer. But if you are new to the Linux command line and stuck to the terminal, things could be overwhelming.
In the terminal, you can use the mv command to rename a file in Ubuntu:
mv OLD_NAME NEW_NAME
Replace 'OLD_NAME' and 'NEW_NAME' with the current file name and the name that you wish to rename it with, respectively.
You can rename individual files in this way. There are also ways to rename multiple files in the command line.
I'll go over these things in a bit more detail here.
Rename a single file
To rename a file, the
mv command is used. The mv command is for moving (or more like cut-paste kind of operations) files from one location to another. But if you use the mv command on a file in the same location, it renames the file instead.
Below is the syntax for
mv OLD_NAME NEW_NAME
Let us take a look at a simple example. Here, I rename
pls_rename.txt file to
$ ls *.txt pls_rename.txt $ mv -v pls_rename.txt renamed_it.txt renamed 'pls_rename.txt' -> 'renamed_it.txt' $ ls *.txt renamed_it.txt
Renaming multiple files
mv command is great, but it can not rename multiple files at once. For example, you can not issue a command like this:
$ ls *.txt image_1.txt image_2.txt image_3.txt image_4.txt image_4.txt $ mv -v *.txt *.jpeg zsh: no matches found: *.jpeg
So what do you do when you have multiple files and want to rename them in Ubuntu command line?
You can try using the find-exec command combination but it gets complicated. A simpler option is using the rename command.
Unfortunately, it is not pre-installed in Ubuntu. Use the apt command to install it:
sudo apt install rename
rename command uses regular expression pattern matching to rename files. Below is its syntax:
rename PERL-REGEX FILES
The term 'PERL-REGEX' in the syntax means that the regular expression it receives must be in Perl's syntax.
Let us try to rename the 'image_*.txt' files to their correct extension. Since the only thing you want to do is replace '.txt' with '.jpeg', you can use substitution with regular expressions like this:
$ rename -v 's/.txt/.jpeg/' *.txt image_1.txt renamed as image_1.jpeg image_2.txt renamed as image_2.jpeg image_3.txt renamed as image_3.jpeg image_4.txt renamed as image_4.jpeg image_5.txt renamed as image_5.jpeg
Regular expressions are powerful. Try renaming a file with lowercase characters into uppercase characters, DOS-style :p
$ ls i_am_not_screaming maybe_i_am_screaming who_knows $ rename -v 'y/a-z/A-Z/' * i_am_not_screaming renamed as I_AM_NOT_SCREAMING maybe_i_am_screaming renamed as MAYBE_I_AM_SCREAMING who_knows renamed as WHO_KNOWS $ ls I_AM_NOT_SCREAMING MAYBE_I_AM_SCREAMING WHO_KNOWS
Tips on preventing mistakes while bulk renaming files
This is an example, and hence I only created 5 files for demonstration.
But in real life, you might have upwards of hundreds of files. Since, you and I, both are humans, we tend to make mistakes, especially with regular expressions.
For that to not be a scathing issue, there exists a life-saving option. That is the
When you use this option, the rename command will show what the changes might look like, instead of actually renaming files. Very handy!
If you are aware of the term 'dry run', this is essentially that.
Let us take a look at an example of renaming all 'image_*.jpeg' files back to the '.txt' extension:
$ rename --nono 's/.jpeg/.txt/' *.jpeg rename(image_1.jpeg, image_1.txt) rename(image_2.jpeg, image_2.txt) rename(image_3.jpeg, image_3.txt) rename(image_4.jpeg, image_4.txt) rename(image_5.jpeg, image_5.txt) $ ls *.jpeg image_1.txt image_2.txt image_3.txt image_4.txt image_5.txt
As you can see, the files have not been renamed. It was only demonstrated what might occur if I were to run the command.
This article covers the steps to rename individual files with the mv command and multiple files with the rename command.
Most people are usually familiar with the mv command but not rename. I hope you liked learning about the rename command in Ubuntu. Bookmark this site and visit regularly for more Ubuntu tutorials.