How to use the command 'rename' (with examples)
The ‘rename’ command is a Perl script that allows users to rename multiple files using Perl Common Regular Expressions. It provides a powerful method for batch renaming files based on specific patterns or substitutions.
Use case 1: Rename files using a Perl Regular Expression
Code:
rename 's/foo/bar/' *
Motivation:
This use case allows users to replace the word ‘foo’ with ‘bar’ in the filenames.
Explanation:
- ‘rename’: The command used for renaming files.
- ’s/foo/bar/’: The Perl Regular Expression (
s/pattern/replacement/
) used to find and replace text in each filename. In this case, ‘foo’ will be replaced with ‘bar’. - ‘*’: Specifies that all files in the current directory will be affected.
Example Output:
Assuming there are three files in the current directory: ‘file1_foo.txt’, ‘file2_foo.txt’, and ‘file3_foo.txt’, after running the command, the files will be renamed to ‘file1_bar.txt’, ‘file2_bar.txt’, and ‘file3_bar.txt’ respectively.
Use case 2: Dry-run - display which renames would occur without performing them
Code:
rename -n 's/foo/bar/' *
Motivation:
This use case allows users to preview the potential renaming operations without actually renaming the files.
Explanation:
- ‘rename’: The command used for renaming files.
- ‘-n’: The option to perform a dry-run. It displays the renaming operations that would be performed without actually renaming the files.
- ’s/foo/bar/’: The Perl Regular Expression used to find and replace text in each filename. In this case, ‘foo’ will be replaced with ‘bar’.
- ‘*’: Specifies that all files in the current directory will be affected.
Example Output:
Assuming there are three files in the current directory: ‘file1_foo.txt’, ‘file2_foo.txt’, and ‘file3_foo.txt’, the command will output:
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:
This use case allows users to forcibly rename files, even if the operation would overwrite existing files with the same name.
Explanation:
- ‘rename’: The command used for renaming files.
- ‘-f’: The option to force renaming, even if it would remove existing destination files.
- ’s/foo/bar/’: The Perl Regular Expression used to find and replace text in each filename. In this case, ‘foo’ will be replaced with ‘bar’.
- ‘*’: Specifies that all files in the current directory will be affected.
Example Output:
Assuming there are three files in the current directory: ‘file1_foo.txt’, ‘file2_bar.txt’, and ‘file3_foo.txt’, the command will rename the files as follows:
file1_foo.txt renamed as file1_bar.txt
file2_bar.txt renamed as file2_bara.txt
file3_foo.txt renamed as file3_bar.txt
Use case 4: Convert filenames to lower case
Code:
rename 'y/A-Z/a-z/' *
Motivation:
This use case allows users to convert filenames to lower case, making them more uniform and consistent.
Explanation:
- ‘rename’: The command used for renaming files.
- ‘y/A-Z/a-z/’: The Perl Transliteration (
y/source/destination/
) used to convert characters from uppercase to lowercase. - ‘*’: Specifies that all files in the current directory will be affected.
Example Output:
Assuming there are three files in the current directory: ‘File1.txt’, ‘file2.txt’, and ‘FILE3.txt’, after running the command, the files will be renamed to ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’ respectively.
Use case 5: Replace whitespace with underscores
Code:
rename 's/\s+/_/g' *
Motivation:
This use case allows users to replace spaces with underscores, which can be helpful for creating filenames that are URL-friendly or consistent with naming conventions.
Explanation:
- ‘rename’: The command used for renaming files.
- ’s/\s+/_/g’: The Perl Regular Expression (
s/pattern/replacement/
) used to find and replace whitespace with underscores.\s
: Matches any whitespace character (e.g., space, tab).+
: Matches one or more occurrences of the preceding pattern (whitespace)._
: The replacement text, in this case, an underscore.g
: Global flag applied to the regular expression to match and replace all occurrences.
- ‘*’: Specifies that all files in the current directory will be affected.
Example Output:
Assuming there are three files in the current directory: ‘file 1.txt’, ‘file_2.txt’, and ‘file 3.txt’, after running the command, the files will be renamed to ‘file_1.txt’, ‘file_2.txt’, and ‘file_3.txt’ respectively.
Conclusion:
The ‘rename’ command provides a flexible and efficient way to rename multiple files using Perl Common Regular Expressions. Whether you need to replace text, convert case, or modify filenames, ‘rename’ offers a variety of options to suit different use cases. It can save time and effort when working with large batches of files.