Renaming files at the command line

Command Line – Bulk Renaming

© Lead Image © Golkin Oleg, 123rf.com

© Lead Image © Golkin Oleg, 123rf.com

Article from Issue 216/2018
Author(s):

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).

Figure 1: Running mv in verbose mode shows the command's limited options.

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.

Figure 2: rename uses Perl expressions, allowing for a variety of ways to rename files. Shown here is a range of characters to be renamed.

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 an i as a modifier changes both lower- and upper-case matches. Without the g, 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

Figure 3: mmv uses its own syntax to provide a versatile option midway between mv and rename.
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.

The Author

Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest coast art. You can read more of his work at http://brucebyfield.wordpress.com

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News