rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is particularly useful when you need to rename a large number of files in a specific format, such as changing the file extension or adding a prefix or suffix to the file name.

The basic syntax of the rename command is as follows:

rename [options] 'perlexpr' [files]

Here are some examples of how to use the rename command:

1) To rename all files in the current directory with a ".txt" extension to a ".md" extension, you can use the following command:

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

2) This command uses a regular expression to match files with a ".txt" extension and replace it with ".md". The * specifies that all files in the current directory should be renamed.

To add a prefix "old_" to all files in the current directory, you can use the following command:

rename 's/^/old_/' *

This command uses a regular expression to match the start of the file name and add the prefix "old_" to it.

3) To remove a prefix "old_" from all files in the current directory, you can use the following command:

rename 's/^old_//' *

This command uses a regular expression to match the prefix "old_" and remove it from the file name.

4) To change the case of all files in the current directory to uppercase, you can use the following command:

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

This command uses the y option to translate all lowercase letters to uppercase.

5) To move all files in the current directory to a subdirectory called "backup , you can use the following command:

rename 's/^/backup\//' *

This command uses a regular expression to match the start of the file name and add the "backup/" prefix to it. This essentially moves the files to a subdirectory called "backup".

6) To rename all files in the current directory with a specific pattern in the file name, you can use the following command:

rename 's/pattern/newname/' *

This command uses a regular expression to match the pattern in the file name and replace it with "newname".

7) To rename a specific file, you can use the following command:

rename 's/oldname/newname/' oldname

This command uses a regular expression to match the "oldname" and replace it with "newname".

8) To rename files recursively in all subdirectories, you can use the -R option

rename -R 's/oldname/newname/' /path/to/directory

It's important to note that the rename command can be dangerous if used improperly, as it can rename multiple files at once and overwrite existing files. It's always a good idea to test the command with a small set of files before running it on a large number of files. Additionally, it's recommended to use the -n option, which will print the names of the files to be renamed, but will not actually perform the rename operation. This allows you to review the changes and make sure they are what you expect before running the command without the -n option.

It's also worth noting that the rename command is not available on all distributions by default. On some distributions, you may need to install the perl-rename package, or use the mv command with a shell script to achieve similar functionality.