Rename Command in Linux



rename is a Linux command line utility used to batch rename multiple files or directories. This command is pretty useful when you need to apply a consistent naming pattern or make systematic changes to the file names. The rename command can save significant time and effort compared to renaming files individually, especially when dealing with a large number of files.

Table of Contents

Here is a comprehensive guide to the options available with the rename command −

Installing of rename Commnad

By default, the rename command is not installed on many Linux distributions. However, you can easily install it using the package manager specific to your Linux distribution.

To install rename on various Linux distributions, follow these instructions −

For Debian/Ubuntu-based distributions

sudo apt install rename

For Red Hat/CentOS-based distributions

sudo yum install prename

For Fedora

sudo dnf install prename

For Arch Linux

sudo pacman -S perl-rename

Syntax of rename Command

The basic syntax for the rename command is −

rename [options] 's/old-pattern/new-pattern/' files

Where −

  • old-pattern is the text you want to replace.
  • new-pattern is the text you want to replace it with.
  • files specifies the files you want to rename.

rename Command Options

Here are some common options you can use with the command rename to customize its behavior −

Option Description
-d, --filename, --nopath, --nofullpath Renames only the filename component of the path, not the directory.
-e Expression: executes code to act on file names. Can be repeated to build up code (similar to perl -e).
-E Statement: executes code to act on file names, similar to -e but terminated by ;.
-f, --force Force overwrite: allows existing files to be overwritten without prompting
-h, --help Displays the help information, including synopsis and options.
-m, --man Shows the manual page for detailed information.
-n, --nono No action: displays the names of the files that would be renamed without actually renaming them.
--path, --fullpath Renames the full path, including any directory components. This is the default behavior.
-u, --unicode [encoding] Treats filenames as Perl (Unicode) strings when executing user-supplied code, and optionally encodes/decodes filenames using the specified encoding.
-v, --verbose Verbose mode: prints the names of the files that have been successfully renamed.
-V, --version Outputs the version number of the rename command.
-0, --null Uses the null character (\0) as the record separator when reading from standard input.

Examples of rename Command in Linux

Here are few practical examples of Linux rename command −

  • Simple File Renaming
  • Adding a Prefix to File Names
  • Removing a Prefix from File Names
  • Using Verbose Mode
  • Testing Renaming without Changing Files

Simple File Renaming

To rename all .txt files to .md files in the current directory, you can use −

rename 's/\.txt$/.md/' *.txt

This command changes the file extension from .txt to .md for all .txt files in the directory.

Rename Command in Linux1

Adding a Prefix to File Names

To add the prefix new_ to all .txt files, use −

rename 's/^/new_/' *.txt

This command prepends new_ to the beginning of each .txt file name.

Rename Command in Linux2

Removing a Prefix from File Names

If you want to remove the prefix old_ from all .txt files, apply −

rename 's/^old_//' *.txt

This command removes old_ from the beginning of each .txt file name.

Rename Command in Linux3

Using Verbose Mode

To see the names of the files as they are being renamed, use the -v option −

rename -v 's/\.txt$/.md/' *.txt

This command provides a detailed output showing the changes being made to the file names.

Testing Renaming without Changing Files

To display the names of files to be renamed without actually renaming them, use the -n option −

rename -n 's/\.txt$/.md/' *.txt

This command shows what the file names would look like if they were renamed, without making any actual changes.

Conclusion

The rename command is an invaluable command for anyone who needs to manage file names efficiently in a Linux environment. By leveraging the various options and understanding the syntax, you can perform bulk renaming operations quickly and accurately. Whether you need to change file extensions, add or remove prefixes, or perform complex pattern substitutions, the rename command offers a robust solution. Regular use of this command can streamline your workflow and save you considerable time and effort.

Advertisements