Renaming files at the command line
Command Line – Bulk Renaming
When it comes to renaming multiple files, the command line offers time-saving options in the form of mv, rename, and mmv.
Modern computers are full of collections of photos, music, files, and ebooks. These collections increase the need for efficient file management, including the renaming of files. Often, users want to show by naming conventions that a set of files belong together, or, in the case of music files, to ensure that songs are played in a certain order. Similarly, projects may want to indicate file contributors or revision numbers. Fortunately, the Bash shell includes three standard commands for adjusting file names: mv
, rename
, and mmv
.
Desktops routinely include a file manager, in which a file's name can be altered in the same dialog window as its other properties. However this feature is designed for renaming a single file in the simplest of circumstances. Even more importantly, as I found while digitizing music ripped from cassettes, when you work with multiple files, opening and closing the file properties dialog a dozen or more times becomes tedious after a few repetitions. Almost always, it is far easier to open a command line.
At the prompt, you could use a script, especially if you are doing a routine operation like a dated backup. However, even easier is to place all the files to be renamed in the same folder and use their path and a regular expression – probably an asterisk (*) – to rename the files all at once. You do have to be careful that a typo does not leave your file names in confusion, but you can use some of the built-in safeguards and do in a couple of minutes what might take you 10 minutes from the desktop.
People Gotta mv
mv
[1] is short for "move," but its inclusion here makes sense when you realize that to move a file is to change at least part of its path – which is also its name. The command is especially useful for simple backups, and the command is about what you might expect:
mv SOURCE-PATH TARGET-PATH
However, what you might not expect is that, by default, the original file is deleted. If you want to copy the original file, add the option --no-clobber
(-n
) directly after the basic command. The target, of course, can be the same directory, with only the file name being different.
If you are making a backup, you can streamline the process by using --update
(-u
), so that only files not already in the target directory are added. You might also use -stop-trailing-slashes
to delete automatically any unnecessary slashes at the end of the path, especially if you are copying and pasting. You might also want to add --suffix=SUFFIX
to change the extension, perhaps to BK
to identify the backup copy.
These can be useful features for renaming a single file or, with a simple regular expression like an asterisk (*), for moving a group of files to another directory. Otherwise, mv
's ability to manipulate file names is limited (Figure 1).
The Intricacies of rename
By contrast, the rename
[2] command is more complex. It is designed to use Perl expressions [3], the same set of wild card characters used in commands such as awk
, grep
, and sed
. In fact, apart from a few examples, the man page assumes that you already know how to use Perl expressions. However, the basic structure is simple enough to pick up on the fly:
rename OPTIONS EXPRESSIONS SOURCE-FILES
The first thing to notice about this structure is that, unlike mv
, rename
does not include a target file. Instead, the designated files are renamed where they are, based on the structure of the expressions entered.
Secondly, this structure and behavior comes with an increased chance of temporarily losing track of files renamed because of unexpected results. rename
will not overwrite existing files unless you use the -force
(-f
) option, but you further safeguard your files with another two of rename's handful of options. The first option, --nono (-n)
, does a dry run for the command you have constructed, terminating successfully if there are no errors or else listing any syntax problems that need to be corrected. The second option is --verbose (-v). As in many commands, --verbose gives you extra information about the operation as it happens. However, in the case of rename
, the extra information is a listing of files that have been successfully renamed. This list gives you a way of instantly checking what you have done.
The most important element in a rename
command's structure is the expressions. Technically, an expression is another option, but its structure is very different from other options. Expressions do not start with one or two hyphens, and they have their own syntax for defining which files are to be renamed and how. Each expression is contained within single quotation marks, indicating that all elements should be processed together, and consists of three parts:
'OPERATOR/SEARCH-STRING/OUTPUT-STRING/MODIFIER'
The modifier is optional.
For example, the complete command to convert the name of all files in the current directory from upper case-letters to lower-case letters would be:
rename 'y/A-Z/a-z/' ./*
With this structure, rename
offers a huge variety of ways to rename files (Figure 2). In effect, the file name is treated as a file when the expression is applied. Obviously, some Perl expressions are not applicable to a file – for instance, using \t
to search for a tab would be pointless. However, you can search for ranges of characters and numbers, as in the example above, and search for strings at the start of the file name (^
) or the end ($
), two alternatives (a|bv
), a single character (.
), and many more expressions. As you might expect if you have any experience with Perl, there can be more than one structure that will make the same changes.
A complete list of all the possibilities would take pages, but the following list should cover most of the common expressions:
's/ORIGINAL-STRING/RENAMED-STRING/g'
: Replaces one set of characters with another one in every instance. By default, the expression is case insensitive, but adding ani
as a modifier changes both lower- and upper-case matches. Without theg
, only the first match is changed.'y/a-z/A-Z/'
: Converts lower-case letters to upper case. By changing the order of the ranges, you can convert upper case to lower case.'s/\.ORIGINAL-EXTENSION$/.NEW-EXTENSION/'
: Changes the file extension.$
searches at the end of the file name.'s/STRING-TO-DELETE\./ /' *STRING-TO-DELETE*
: Deletes a series of characters and replaces it with nothing, which is indicated by the blank space between the last two forward slashes.'s/ /\_/g'
: Replaces a blank space in a file name with an underscore. This example is especially useful if you are ripping or downloading music files or want the files to be used easily from the command line, perhaps for backups. You could also designate a blank space with\w
. You can use modifications of this structure for removing commas, apostrophes, and other illegal characters in a Bash file name.
Often, the structure to achieve other results may depend on the details of what you want to do. For example, to add a string to file names, you could use 's/ORIGINAL-STRING/RENAMED-STRING/g'
, adding the new string to the original one in the renamed string. Alternatively, you could start the new string with .
to place it at the start of the file name, or start the new string with $
to place it at the end of the file name.
Basically, any renaming that can be done in Perl should be doable using the rename
command, but it requires more than a passing acquaintance with Perl. For example, incremental file naming can be done using Perl's sprintf
, although that would be an article in itself.
Other Ways to Rename
If mv
is too simple for your needs, and rename
's use of Perl too complex, you might try mmv
[4], a newer command that is carried by many distributions, but generally not installed by default. mmv
has only some of rename
's versatility, but is simpler to use. Instead of the command structure having a separate section for expressions, mmv
incorporates the renaming instructions into the source and target designations. For example, the basic command structure is:
mmv '*ORIGINAL-NAME' '#1NEW-NAME'
With *
being the notation for the original name, and #1
the new name (Figure 3). However, mmv
can also do more complex operations, such as replacing the first instance of a string in each file name with another string
mmv '*ORIGINAL-STRING*' '#1NEW-STRING#2'
In all cases, mmv
offers several options for handling files, such as moving the original file to the new name, copying the original and its permissions and then deleting the original, or renaming the source file without moving it. The examples in the man and info pages should be enough to get you started, although detailed information about the command structure appears to be missing.
Alternatively, those working with music files might want to return to the desktop and use pyRenamer [5], which has all the functions of rename
, but has the added advantage of reading meta tags directly, which allows the naming of music files in various combinations of elements such as track number, title, and genre. The only disadvantage of pyRenamer is that you need to refresh the display manually after adding files to the current directory.
None of the renaming tools I mention are ideal by themselves. However, with a combination of these commands, you can organize your growing collection of files and have a reasonable chance of finding them later. File search utilities can help, but, in the end, nothing beats the organized structure that renaming tools help you to create.
Infos
- mv: https://linux.die.net/man/1/mv
- rename: https://linux.die.net/man/2/rename
- Perl expressions: http://jkorpela.fi/perl/regexp.html
- mmv: https://linux.die.net/man/4/mmv
- PyRenamer: https://github.com/SteveRyherd/pyRenamer
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.