How to Rename Files in Linux

Renaming files is not a particularly advanced operation; as long as it’s done on a small number of files, it usually doesn’t require special tools. However, when there’s an entire folder of photos from last year’s vacation waiting to be renamed, it might be wise to consider some time-saving tricks or apps.

There are two general approaches to batch file renaming: it can be done either via the command-line interface or by using a standalone application. Linux users already know how powerful the CLI can be, so it shouldn’t be surprising that there are several commands for file renaming.

A simple way to rename files and folders is with the mv command (shortened from “move”). Its primary purpose is moving files and folders, but it can also rename them, since the act of renaming a file is interpreted by the filesystem as moving it from one name to another.

The following syntax is used to rename files with mv:

mv (option) filename1.ext filename2.ext

where “filename1.ext” is the original, “old” name of the file, and “filename2.ext” the new name.

linux-rename-files-mv

The same pattern works for folder renaming. If the files are not located in the currently active folder, their full path has to be specified:

mv /home/user/Files/filename1.ext /home/user/Files/filename2.ext

Note that the mv command requires write permission for the folder containing the files. In case of system files and folders, the user needs to obtain root permissions to rename files by prepending mv with sudo or su. An extra layer of protection is provided by the “-i” (interactive) option, which asks the user to confirm the file rename before it’s actually applied.

There’s also the “-v” (verbose) option which lists all changes that have been made by mv. Options are written after mv but before the filenames.

This command is slightly more advanced than mv because it requires the knowledge of, or at least a basic familiarity with regular expressions. That may sound scary, but don’t give up on rename just yet – it can be used for plain batch renaming simply by following tutorials like this one.

The rename syntax looks like this:

rename (option) 's/oldname/newname' file1.ext file24.ext

The letter “s” stands for “substitute” and it’s the main part of the regular expression. Single quotes around it are obligatory. Available options are

  • “-v” (verbose; prints the list of renamed files along with their new names)
  • “-n” (“no action”; a test mode or simulation which only shows the files that will be changed without touching them)
  • “-f” (a forced overwrite of the original files)

The rename command also accepts wildcards to rename multiple files of the same type, and it works on file extensions as well. For example, this would change all files with the extension .jpeg to .jpg:

rename 's\.jpeg/\.jpg/' *

The wildcard symbol (*) means that all files in the folder will be affected.

The regular expression also has its own options (modifiers): “g” (global; affects all occurrences of the expression) and “i” (performs case-insensitive substitution). They are written at the end of the expression, just before the closing single quote, and can be combined:

rename -n 's/DSC/photo/gi' *.jpg

This would apply to all .jpg files that contain “DSC”, “dSC”, “dsc”… and change that part of the filename to “photo”. However, because of the “-n” option, the command wouldn’t actually rename the files but just print them in the console window.

Substitution is not the only thing that this regular expression can do. There’s also translation – marked by the letter “y” – which can transform the filenames on a more complex level. It is most often used to change the filename case:

rename 'y/a-z/A-Z/' *.jpg

This would change the names of all .jpg files from lowercase to uppercase. To do it vice-versa, just switch the “oldname” and “newname” parts of the regular expression.

Using the rename command boils down to mixing a few basic patterns to achieve the desired result. Thanks to the “-n” option the users will never have to put their files at risk (or their nerves at stake), since it offers a safe and useful preview of what the renamed files will look like.

Finally, the solution that all the anti-console users have been waiting for: a desktop application where everything can be done with a simple click of the mouse. pyRenamer is a crazy-powerful file renaming tool written in Python. At the moment, the official website seems to be down, but users of Ubuntu and its derivatives can still install pyRenamer from the repositories using the command:

sudo apt-get install pyrenamer

Alternatively, you can compile it using this forked code from GitHub.

linux-rename-files-pyrenamer

The interface consists of four parts:

  1. a tree-view file browser for selecting files and folders
  2. a central preview pane showing the filenames before and after renaming
  3. a tabbed control area for choosing the rename criteria
  4. the Options sidebar

pyRenamer can remove accents and duplicate symbols from filenames, replace any string of text with another, change filenames to uppercase, lowercase or sentence case, and automatically insert or remove spaces and underscores. Beginners will love it, because all this can simply be selected in the tabbed area, previewed in the main area and confirmed by clicking Rename. If they wish to experiment with patterns, pyRenamer provides a cheatsheet to make it easier.

linux-rename-files-pyrenamer3

Advanced users will appreciate pyRenamer’s ability to rename multimedia files by reading from their metadata. It’s also possible to manually rename a single file when a batch rename is unnecessary.

In essence, pyRenamer wraps the functionality of mv and remove commands into a user-friendly GUI. It’s a great choice for those who aren’t confident in their CLI skills.

Of course, there are other ways to rename files in Linux – by writing a script, for example, or by using other tools similar to pyRenamer. What do you use to rename your files? Share your tips and tricks in the comments.