Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How I can rename images with 'rename' command from "something_full.jpg" to "something_500.jpg" recursive?

share|improve this question
up vote 6 down vote accepted

Basically, you can use the rename tool for that. It should come in a Perl-based version with Debian-based Linux distros, but you can easily download it from source as well (obviously, you need to make it executable first with chmod +x).

The following command will replace the _full part with _500 on all JPG files in the current directory.

rename 's/_full/_500/' *.jpg

To do this recursively, starting from your current directory, use rename with find.

find . -type f -iname "*.jpg" -exec rename 's/_full/_500/' {} \;

Note: You may want to test the command before actually executing is. In order to do that, add the -n option to rename (e.g. between rename and the 's//' string).

share|improve this answer
my bad, I underestimated Larry there! How about piping the jpg file paths instead of the exec? – nik Apr 14 '12 at 17:55
Thank you. I successfully used find . -type f -iname "*.jpg" -exec rename 's/_full/_500/' {} \;. – Ticksy Apr 14 '12 at 18:02
@nik Why would you trade the security offered by find against something that involves piping constructs (and may fail due to spaces in filenames, etc.)? I have found the latter to be insecure and hard to read at best. – slhck Apr 14 '12 at 18:34

You may want to look into zmv. zmv is a zsh specific feature, and requires autoload -U zmv to be somewhere in your .zshrc file.

EDIT: As an example, the task the OP posed could be solved with

noglob zmv '(*)_full.(*)' '$1_500.$2'

An excerpt from the zshcontrib man page:

zmv [ -finqQsvwW ] [ -C | -L | -M | -p program ] [ -o optstring ] srcpat dest

Move (usually, rename) files matching the pattern srcpat to corresponding files having names of the form given by dest, where srcpat contains parentheses surrounding patterns which will be replaced in turn by $1, $2, ... in dest. For example,

zmv '(*).lis' '$1.txt'

renames foo.lis to foo.txt, my.old.stuff.lis to my.old.stuff.txt, and so on.

The pattern is always treated as an EXTENDED_GLOB pattern. Any file whose name is not changed by the substitution is simply ignored. Any error (a substitution resulted in an empty string, two substitutions gave the same result, the destination was an existing regular file and -f was not given) causes the entire function to abort without doing anything.

EXTENDED_GLOB patterns are tremendously powerful. Here is a good primer on the concept.

share|improve this answer
I've added an example and some documentation. – mkaito Apr 16 '12 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .