How to use the command 'rename' (with examples)

How to use the command 'rename' (with examples)

The ‘rename’ command is a useful tool for renaming multiple files in one go using Perl regular expressions. It is particularly handy when you need to perform batch renaming operations or make specific changes to multiple filenames at once.

Use case 1: Rename files using a Perl Common Regular Expression (substitute ‘foo’ with ‘bar’ wherever found)

Code:

rename 's/foo/bar/' *

Motivation: Suppose you have a directory full of files that contain the word ‘foo’ in their names, and you want to replace it with ‘bar’. Using the command above, you can easily rename all the files matching the pattern by substituting ‘foo’ with ‘bar’.

Explanation:

  • rename: The command itself.
  • 's/foo/bar/': The Perl regular expression specifying the substitution pattern.
  • *: The wildcard character representing all files in the current working directory.

Example output: Suppose you have three files named ‘file1_foo.txt’, ‘file2_foo.txt’, and ‘file3_foo.txt’. After executing the command, the files will be renamed to ‘file1_bar.txt’, ‘file2_bar.txt’, and ‘file3_bar.txt’.

Use case 2: Dry-run - display which renames would occur without performing them

Code:

rename -n 's/foo/bar/' *

Motivation: Sometimes, before actually renaming the files, it is helpful to preview the changes that will be made. The ‘-n’ option allows you to perform a dry-run, displaying the expected outcome without modifying any filenames.

Explanation:

  • -n: The option for performing a dry-run.
  • 's/foo/bar/': The Perl regular expression specifying the substitution pattern.
  • *: The wildcard character representing all files in the current working directory.

Example output: If you have the same three files as in the previous example, executing the command will output the expected changes without actually renaming the files:

file1_foo.txt renamed as file1_bar.txt
file2_foo.txt renamed as file2_bar.txt
file3_foo.txt renamed as file3_bar.txt

Use case 3: Force renaming even if the operation would remove existing destination files

Code:

rename -f 's/foo/bar/' *

Motivation: By default, the ‘rename’ command does not allow renaming if there are files with the same name at the destination. However, there might be cases where you want to forcefully rename the files and overwrite any existing files with the same name. This can be useful when you want to ensure consistent naming across a set of files.

Explanation:

  • -f: The option to force renaming even if it would delete existing destination files.
  • 's/foo/bar/': The Perl regular expression specifying the substitution pattern.
  • *: The wildcard character representing all files in the current working directory.

Example output: If you have two files named ‘file1_foo.txt’ and ‘file1_bar.txt’, executing the command will rename ‘file1_foo.txt’ to ‘file1_bar.txt’, overwriting the existing ‘file1_bar.txt’.

Use case 4: Convert filenames to lower case (use -f in case-insensitive filesystems to prevent “already exists” errors)

Code:

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

Motivation: There might be instances where you want to convert all filenames in a directory to lower case for consistency or to ensure compatibility with case-insensitive filesystems. This command allows you to achieve that with ease.

Explanation:

  • y/A-Z/a-z/: The Perl expression that performs the case conversion from uppercase to lowercase.
  • *: The wildcard character representing all files in the current working directory.

Example output: Suppose you have three files named ‘File1.txt’, ‘file2.txt’, and ‘FILE3.txt’. After executing the command, the filenames will be converted to ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’.

Use case 5: Replace whitespace with underscores

Code:

rename 's/\s+/_/g' *

Motivation: When working with filenames, it is often desirable to replace whitespace characters with underscores to improve readability or avoid issues with certain applications that do not handle spaces well. This command allows you to replace all whitespace occurrences with underscores in multiple filenames simultaneously.

Explanation:

  • 's/\s+/_/g': The Perl regular expression that matches one or more whitespace characters and replaces them with underscores.
  • *: The wildcard character representing all files in the current working directory.

Example output: Suppose you have two files named ‘file 1.txt’ and ‘file 2.txt’. After executing the command, the filenames will be renamed to ‘file_1.txt’ and ‘file_2.txt’.

Conclusion:

The ‘rename’ command is a powerful tool for batch renaming files, allowing you to perform various renaming operations using Perl regular expressions. By understanding the different use cases and their corresponding code examples, you can efficiently manage and modify filenames in your Linux system.

Related Posts

Managing Networking Status with nmcli networking (with examples)

Managing Networking Status with nmcli networking (with examples)

NetworkManager is a powerful tool for managing network connections on Linux systems.

Read More
How to use the command 'tail' (with examples)

How to use the command 'tail' (with examples)

The ’tail’ command is a utility in Unix-like operating systems that allows users to display the last part of a file.

Read More
Exploring the Powerful Features of `pio run` Command (with examples)

Exploring the Powerful Features of `pio run` Command (with examples)

1: Listing all available project targets Code: pio run --list-targets Motivation: When working on a PlatformIO project, it’s important to know the available project targets or tasks that can be executed.

Read More