There's a handy perl file-renaming script that gets installed with the main Perl package on Debian-based systems (including Ubuntu). It's usually named rename
, but sometimes is called prename
(Perl rename). Use like this:
rename expr file1 file2 file3
The command uses Perl to evaluate expr
for each file
argument, and it renames each file to the output of the expr
evalutation. So, for simple regex replacements like your example:
rename 's(images/(.*)\.png$)(test/$1.test.png)' ./images/*
# here's the expression:
# s()() -- just like s/// (but i don't need to escape the / in the filename)
# match regex: images/(.*)\.png$ -- match filenames of the form images/*.png
# replace: test/$1.test.png -- turn them into test/*.test.png
If files don't match the expression (for example, non-PNG files), the substitution won't do anything and their filenames won't be changed.