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.
here's what it returns:
usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory – mxg – 2009-12-25T15:00:48.010
2Worked for me: perhaps you should try quoting instances of your $i: "$i" – Jeremy L – 2009-12-25T15:48:51.723
It works also for mac. – mxg – 2009-12-25T17:36:27.013
This is, more or less, the standard UNIX way of doing the task, as all you need is a Bourne shell. The perl method is newfangled. Cool, but new-fangled. – pcapademic – 2009-12-25T20:27:40.067
@EricJLN: it's only "new-fangled" compared to UNIX itself -- Perl's been around for more than two decades now. (by comparison, the Bourne shell's only a single decade older;
bash
is the same age as Perl.) the Perl Cookbook was published in 1998 and includes a simple version of the same script; it's certainly older than that. – quack quixote – 2009-12-25T21:01:31.327