How to Rename Files Using Linux

The 'rename' command for Linux changes filenames in a single batch

Although your Linux distribution's desktop environment supports GUI tools that modify file and filenames, the most nuanced and powerful approach requires the rename shell command.

Some distributions do not include rename by default. Install it from the repository or seek an alternative.

Chalk board with, "Hello my name is" written on it.
winhorse / Getty Images

Using the 'rename' Command

The command takes the general form:

rename [options] [-e perlexpr] [files]

Common options include:

  • -d: Do not rename the directory component of a filename.
  • -e perlexpr: Expression, in Perl syntax, to apply to the relevant file names.
  • -f: Permits overwriting of existing files.
  • -n: Prints what the file-rename process looks like, but doesn't actually change the filenames. Useful for testing.
  • -v: Print the names of the files that were renamed.

The perlexpr statement uses Perl regular expressions to effect a batch-rename process.

The first argument of rename parses as an expression even without the -e flag.

Example Uses of 'rename'

rename command for linux

As with many shell commands, rename and its expressions are case sensitive.

To capitalize the first letter of every file in the directory, execute:

rename 's/\b(\w)/\U$1/g' *.txt

To rename the file extension for all the files in the directory from .Txt to .bak, and to display the results of the command verbosely, execute:

rename -v 's/\.Txt$/\.bak/' *.Txt

To rename a single file, it's easier to use the mv command rather than developing Perl regex for that purpose. To rename test.txt to sample.txt, execute mv test.txt sample.txt.