Possible Duplicate:
Renaming lots of files in Linux according to a pattern
I have multiple files in this format:
file_1.pdf
file_2.pdf
...
file_100.pdf
My question is how can I rename all files, that look like this:
file_001.pdf
file_002.pdf
...
file_100.pdf
I know you can rename multiple files with ‘rename’, but I don’t know how to do this in this case.
You can do this using the Perl tool
renamefrom theshellprompt. (There are other tools with the same name which may or may not be able to do this, so be careful.)If you want to do a dry run to make sure you don’t clobber any files, add the
-nswitch to the command.note
If you run the following command (
linux)and you have a result like
then this seems to be the right tool =)
This seems to be the default
renamecommand onUbuntu.To make it the default on
Debianand derivative likeUbuntu:Explanations
s///is the base substitution expression :s/to_replace/replaced/, checkperldoc perlre(\d+)capture with()at least one integer :\dor more :+in$1sprintf("%03d", $1)sprintfis likeprintf, but not used to print but to format a string with the same syntax.%03dis for zero padding, and$1is the captured string. Checkperldoc -f sprintfemodifier at the end of the expression