One-line batch file renamer
If you want to batch rename a bunch of files (say “foo*.jpg” to “bar*.jpg”), you might think you could just do “mv foo*.jpg bar*.jpg” in the Terminal. However, this doesn’t work right since the shell expands each argument before the execution occurs. However, there’s a cool way to accomplish the same result with a (more complex) command line argument.
Open a terminal, and “cd” your way to the directory of interest (or just drag the folder you want to work with onto the terminal icon in the dock; it will open in that directory). Once there, we’ll run a ‘test’ before actually change any names. This first version of the command is “proof of concept”; it will output what will happen, without actually doing it. So to rename all those “foo*.jpg” files into “bar*.jpg” files, type:
ls foo*.jpg | awk '{print("mv "$1" "$1)}' | sed 's/foo/bar/2'
This should output a series of “mv” (the unix “move” command, which is used to rename files) lines, each one showing the old and new name for each file affected. If it all looks right, then just pipe the output to the shell to execute:
ls foo*.jpg | awk '{print("mv "$1" "$1)}' | sed 's/foo/bar/2' | /bin/sh
That should do the trick. Dierdre points out that this is an especially nice way to do it, since you get to see what will happen before you commit to it! I happen to agree with that logic completely!
To use this on your own files, you’ll need to replace the references to filenames and items to be replaced in both the “ls” and “sed” portions of the script.