Through Graphical User Interface (GUI), we can easily rename a file. Just right-click on the file and Rename. It opens a dialog box to enter a new name. From here even the extension of the file can also be modified. But, how changing the extension impacts the file is something we are not covering here.
Generally, we use a loop with mv command-line utility to rename multiple files. In this article, we cover how to rename multiple files with rename command-line utility in Ubuntu. But, before that we need to install relevant packages.
Note: Following operations require superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance. Besides, have a backup before you make any changes.
Install rename in Ubuntu
The package is available through standard Ubuntu repository. Therefore, update the repository first. Hence, open a terminal and issue the following –
sudo apt update
Next, to install rename package –
sudo apt install rename
Rename multiple files in Ubuntu
This is important, before making changes we can also check how things will pan out for us. This won’t make any changes to the files itself but, it would just show us what will be the outcome. To get that, use -n option,
rename -n 's/\.odt$/\.txt/' *.odt
The above command would show us how things would look like but won’t make any material changes to the .odt files.
Besides, we also have -v option (Verbose) to view the modifications made –
rename -v 's/\.odt$/\.txt/' *.odt
Case I. We start with the easiest first, rename the extension of files. So, let’s say we have multiple document files which end with extension .odt. To modify the extension to text file i.e. .txt, issue the following –
rename -v 's/\.odt$/\.txt/' *.odt
Case II. If we want our files to not have an extension then,
rename -v 's/\.txt$//' *.txt
Case III. To convert upper case letters to lower case and vice versa in filenames-
rename -v 'y/A-Z/a-z/' *
or,
rename -v 'y/a-z/A-Z/' *
As we have used a *, this would make changes to the all the files. To make changes to only text files, use .txt in place of *
Case IV. Rename part of filenames with another string. For instance, we have two text files – techpiezo-1.txt and techpiezo-2.txt and we would like these two as tp-1.txt and tp-2.txt. Then,
rename -v 's/techpiezo/tp/' *.txt
Case V. In Case IV, we got files as tp-1.txt and tp-2.txt. What if we want to have underscore(_) in place of hyphen(-).
rename -v 's/-/_/' *.txt
In conclusion, we have covered how to rename multiple files in Ubuntu through few basic examples. It is not possible for us to cover each and every combination possible. But, at least you get an idea on how to move forward.