Home > bashism, linux > Number Replacement in Filenames

Number Replacement in Filenames

October 15th, 2013 Leave a comment Go to comments

Just assume you have a lot of files with numbers in their names, but these numbers do not have leading zeros. How would you fix that?

rename 's/\d+/sprintf("%04d",$&)/e' *.FILEENDING

This command leaves the characters in the file name and replaces all numbers with the same number and leading zeros. Works with bash and ZSH, no warranty 😉 You also can replace the “04d” with “0Xd” and you get X digit numbers.