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

Using sed, I can search and replace text in a file. Is there a way I can do search and replace of filenames? For example if I have a bunch of files in a folder with names like these:

  • foo01
  • foo02
  • bar001
  • bar002

I would like to quickly rename all of the ones starting with foo so that they have 3 digits instead of 2.

share|improve this question
up vote 2 down vote accepted
shopt -s nullglob
for file in foo*
  newnum=$(printf "%03d" $num)
  mv "$file" "$newfile"
share|improve this answer
Wow. What does all of that do? – Svish Jul 30 '10 at 14:12
@Svish Take a look at the Advanced Bash Scripting Guide – KeithB Jul 30 '10 at 14:16
@KeithB: Don't feel like reading a whole book just to decipher a few lines of script... – Svish Jul 30 '10 at 14:41
then read this: – user31894 Jul 30 '10 at 14:54
@ghostdog74: Thanks :) – Svish Jul 30 '10 at 15:11

There's a Perl script that may already be on your system called rename or prename.

rename 's/\d\d$/0$&/' foo*

If you run that multiple times it will continue to insert zeros. To prevent that, this version only renames files that end in two digits (preceded by at least one non-digit):

rename 's/([^\d])(\d\d)$/${1}0$2/' foo*
  • s/// is the substitute command
  • \d stands for a digit
  • $& in the first example stands for everything that was matched between the first two slashes (two digits in this case)
  • [^\d] stands for all characters ([]) that are not (^) digits (\d)
  • ${1} stands for what was matched in the first set of parentheses (a non-digit), the braces set the "1" off from the literal "0"
  • $2 stands for what was matched in the second set of parentheses (two digits), the braces aren't necessary here, but you could use them
share|improve this answer
Looks like exactly what I want. Unfortunately it doesn't seem to exist on Mac OS X =/ – Svish Jul 30 '10 at 14:57
This is excellent, thanks :) – Richard Smith-Unna Jan 20 '13 at 13:43

If you use MacPorts, there is a package called renameutils. I've never used it, but from the description, it may be what you want.

renameutils - tools that make renaming files easier Description ¶

renameutils contains 5 programs: qmv, qcp, imv, icp, and deurlname

qmv and qcp use the aid of a text editor to create a "plan" that is executed when the file is saved - great for batch moves

imv and icp are interactive programs with GNU readline support

deurlname removes URL encoded characters from a filename

More info at the project's homepage

share|improve this answer
Interesting. Will have a look at that some time :) – Svish Jul 30 '10 at 15:46

Have a look mmv (should be available for all kinds of Linux but is rarely installed by default).

share|improve this answer

You must log in to answer this question.

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