Confusion with rename
There are several software packages with the name “rename” available for Bash.
That can cause confusion if you’re not aware of the fact.
Adding to the confusion, on many distributions of Linux, like Debian and Ubuntu, the rename
command doesn’t refer to the “rename” that is part of the util-linux package. Instead, it likely refers to a “perl-rename” package. To call the util-linux version you’ll likely have to use the rename.ul
command.
There are reasons for this naming structure, but it’s important to know which command you’re dealing with, especially since they can overwrite filenames without warning.
Helpfully, the commands tldr rename
and tldr perl-rename
explicitly draw attention to this distinction and say which version the description refers to.
The commands man rename
and man rename.ul
don’t make the distinction as obvious, but they do outline the use of each command.
Many online Q&A and forum discussions about the rename command don’t make the distinction clear. And in some cases discussants seem to conflate the various packages without realizing it. It’s a confusion that’s easy to understand.
rename.ul
For now, most Debian-based Linux distributions use rename.ul
to refer to the util-linux command.
As the man page entry says, rename.ul “will rename the specified files by replacing the first occurrence of expression in their name by replacement."
It has the form
rename.ul [options] <expression> <replacement> <file>
For example,
rename.ul -v '_draft' '_final' *.txt
The above would replace the first occurrence of the string ‘_draft’ with ‘_final’ for all files in the directory with the extension .txt and display a list of which files were renamed.
That can be risky if you’re not sure about what you’re doing. It’s a good idea to also add the -n
option to first see what changes would be made. Then, if you like the results, rerun the command without the -n argument. To see what changes would take place before actually making them just amend the command to the following:
rename.ul -nv '_draft' '_final' *.txt
rename
For now, most Debian-based Linux distributions use rename
to refer to the Perl command.
As the tldr entry says, rename “renames files using a Perl Common Regular Expression”
It has the form
rename [options] <expression> <file>
For example,
rename -nv 's/_draft/_final/' *.txt
Because of the -n
argument, the above shows what files (if any) would be renamed and how they would be renamed by replacing the string ‘_draft’ with ‘_final’ for all files in the directory with the extension .txt.
Removing the -n
argument actually makes the changes and displays a list of which files were renamed.
rename -v 's/_draft/_final/' *.txt
Because rename accepts regular expressions, it can be very powerful, allowing for complicated file rename tasks.