Rename Command Examples (with Examples)
- Linux
- November 5, 2023
Use Case 1: Rename files using simple substitutions
Code
rename foo bar *
Motivation
This use case is helpful when you want to replace a specific string, such as ‘foo’, with another string, such as ‘bar’, in multiple filenames at once. It saves time and effort compared to manually renaming each file individually.
Explanation
The rename
command is used with two arguments: the string to be replaced (foo
) and the new string to replace it with (bar
). The asterisk (*) is a wildcard that represents all files in the current directory. This command will find all occurrences of ‘foo’ in the filenames and replace them with ‘bar’.
Example Output
If you have files named file1.txt
, file2.txt
, and example.foo
, running the rename foo bar *
command will rename the files to file1.txt
, file2.txt
, and example.bar
.
Use Case 2: Dry-run to display rename changes without performing them
Code
rename -vn foo bar *
Motivation
Sometimes it is useful to preview the changes that will be made before actually renaming the files. This allows you to verify that the renaming process will produce the expected results and avoid any unintended consequences.
Explanation
The -vn
options are used in conjunction to perform a dry-run without performing any actual renaming. The -v
option stands for verbose and displays a detailed output of the changes that would occur. The -n
option stands for no action and prevents any renaming from taking place.
Example Output
When running the command rename -vn foo bar *
, the output will display each file that would be renamed and the corresponding changes. For example:
'file1.txt' would be renamed to 'file1.txt'
'example.foo' would be renamed to 'example.bar'
Use Case 3: Do not overwrite existing files
Code
rename -o foo bar *
Motivation
There may be cases where some files in the current directory already have names that would conflict with the renaming operation. To avoid accidentally overwriting existing files, the -o
option can be used to prevent any files from being overwritten during the renaming process.
Explanation
The -o
option is used to prevent the rename
command from overwriting existing files. When this option is specified, the command will skip any files that would lead to a name conflict and move on to the next file.
Example Output
If there are two files named file1.bar
and file2.foo
in the current directory and you run the command rename -o foo bar *
, only the file file2.foo
will be renamed to file2.bar
. The file file1.bar
will not be affected.
Use Case 4: Change file extensions
Code
rename .ext .bak *.ext
Motivation
Changing file extensions can be necessary when you want to modify the format or type of a group of files. This use case demonstrates how to easily change the extension of multiple files in one command.
Explanation
To change file extensions, the rename
command is used with two arguments: the current extension (.ext
) and the new extension (.bak
). The *.ext
wildcard represents all files with the .ext
extension in the current directory. This command will rename all files with the .ext
extension to have the .bak
extension.
Example Output
If there are files named file1.ext
, file2.ext
, and example.ext
in the current directory, running the rename .ext .bak *.ext
command will rename the files to file1.bak
, file2.bak
, and example.bak
.
Use Case 5: Prepend “foo” to all filenames in the current directory
Code
rename '' 'foo' *
Motivation
Sometimes you may need to add a common prefix to the filenames in a directory. This use case demonstrates how to prepend a string, such as ‘foo’, to all the filenames in the current directory using the rename
command.
Explanation
The rename
command is used with two arguments: an empty string (''
) and the desired prefix (foo
). The asterisk (*) wildcard represents all files in the current directory. This command will add the prefix ‘foo’ to the beginning of each filename.
Example Output
If you have files named file1.txt
, file2.txt
, and example.txt
in the current directory, running the rename '' 'foo' *
command will rename the files to foofile1.txt
, foofile2.txt
, and fooexample.txt
.
Use Case 6: Rename a group of increasingly numbered files with zero-padding
Code
rename foo foo00 foo? && rename foo foo0 foo??
Motivation
When you have a group of sequentially numbered files that need to be renamed with zero-padding (e.g., foo1
to foo001
, foo2
to foo002
, etc.), using the rename
command can automate the process and ensure consistent file naming.
Explanation
Two separate rename
commands are used in this case. The first command renames the files with a single-digit number to have a two-digit zero-padded number. The second command renames the files with a two-digit number to have a three-digit zero-padded number.
The syntax foo?
matches any single character after foo
, allowing the first rename
command to rename all files with a single-digit number. The syntax foo??
matches any two characters after foo
, allowing the second rename
command to rename all files with a two-digit number.
Example Output
If you have files named foo1
, foo2
, …, foo9
, foo10
, foo11
, …, foo99
in the current directory, running the command rename foo foo00 foo? && rename foo foo0 foo??
will rename the files as follows:
foo1
tofoo001
foo2
tofoo002
- …
foo9
tofoo009
foo10
tofoo010
foo11
tofoo011
- …
foo99
tofoo099