In March of last year, my colleague, Pulkit Chandak, penned an excellent article providing the best ways to delete, copy, move, and rename files via the Linux command-line. This article aims to delve deeper into how to rename files in Linux.
Before we begin, understand that many CLI commands can help you batch rename files, and we will allude to those. However, this article is not that. Here, you’ll learn how to rename a file using the commands that Linux natively offers.
mv command – the best method for renaming files
The oldest and most tried-and-true method of renaming files is with the mv command. I remember it well from my days when I wore a younger man’s clothes, as a Unix administrator.
For example, I have a file in my directory named tevin.txt. However, I need to change it to michael.txt. To accomplish this with the mv command is easy.
# mv tevin.txt michael.txt
Not only did the mv command rename tevin.txt and create michael.txt, but tevin.txt is gone, too.
rename command – the best method for batch renaming
The rename utility is another option for renaming your files, but it’s for the batch renaming of the files. It’s not like your usual Linux command. Instead, unlike most Linux commands that are written in C, the rename command is written in Perl and is a portion of a script that lives in /usr/bin/ on most Linux distros. Installing it is simple.
# sudo apt install rename
The syntax for the command is:
# rename 's/old-name/new-name/' files
As you see, it requires a mandatory Perl expression enclosed in ‘ ‘ (see ‘s/old-name/new-name’ in the syntax example above. However, it also comes with optional arguments, too. These are:
- -v – Verbose: print names of files successfully renamed
- -n – No action: print names of files to be renamed, but don’t rename
- -f – Overwrite: allow existing files to be overwritten
- -h – Help: print SYNOPSIS and OPTIONS
- -m – Manual: print manual page
- -V – Version: show version number
- -e – Expression: code to act on files name
- -E-Statement: code to act on files name, as -e but terminated by ‘;’
# rename [-v] [-n] [-f] perlexpr [files]
Let’s use the basic syntax to rename the files in our ~/personal directory to rename all the .txt files to .doc files.
# rename 's/\.txt$/.doc/' *
As you can see, the example was successful. Now let’s rename all the files back to .txt with the -v (Verbose) option.
# rename -v 's/\.doc$/.txt/' *
Not only did we successfully rename all our files back to the .txt extension, but the -v option also let us know exactly the changes made.
Let’s try one more. In this example, we will capitalize the first letter of all the filenames. We’ll also use the -v option once more so we can see what’s going on.
# rename -v 's/./\U$&/' *
Pretty slick, right?
However, the options and the Perl syntax required is sometimes confusing. Mastery of the command takes time. I highly recommend that you use the -n option (no action) to ‘test’ run the rename command before committing to it.
Other commands for renaming files
There are a plethora of other commands that you can use to batch rename files, such as mmv, renameutils, qmv, qcp, imv, icp, and vimv. However, the mv command has been and will continue to be my ‘go to’ command for renaming files at the command line, unless batch renaming. If batch renaming, I stick with rename.
GUI options for renaming files
Provided you have the correct permissions, you can always rename your files via the GUI, using a file manager such as Thunar, Dolphin, or Nautilus. Besides, there are utilities such as Métamorphose2, KRename, pyRenamer, and many others that exist strictly for batch renaming files in the GUI. Let’s install one of my favorites, pyRenamer, and test it out.
The installation of pyRenamer is simple.
# sudo apt install pyrenamer
After installation, start pyRenamer from the terminal.
You’ll see that you have many tabs and options for batch renaming your files, like Patterns, Substitutions, etc. For our last example, we will rename the files we capitalized in our previous example to lower-case again.
Navigate to the correct directory (/home/mtravisrose/personal). Note our three files in the upper right-hand window. Select the Substitutions tab. Under Common substitutions, check the Capitalization box, and All downcase. Click Preview and ensure that the expected option appears in the upper right-hand window.
Once satisfied that the renamed file names in the upper right-hand window are as desired, click Rename to commit.
Unfortunately, documentation for the tool is scarce, and Help only offers the About option. If you use the program as your GUI batch file rename utility, it will be a labor of trial and error. Perhaps that’s why you must click the Preview button before committing your changes?
While there are many ways to rename your files, most of which you can easily accomplish via the GUI mentioned in our article, I strongly encourage you to use the command line and the mv command for renaming a single file. If batch renaming, the rename command is the route you want to take. The more time you spend in the terminal, the savvier a Linux user you’ll be.