Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers

up vote 2 down vote accepted
#!/bin/bash
shopt -s nullglob
for file in foo*
do
  filename=${file%%[0-9]*}
  num=${file##*[^0-9]}
  newnum=$(printf "%03d" $num)
  newfile=${filename}${newnum}
  mv "$file" "$newfile"
done
share|improve this answer
1  
Wow. What does all of that do? –  Svish Jul 30 '10 at 14:12
    
@Svish Take a look at the Advanced Bash Scripting Guide tldp.org/LDP/abs/html/index.html –  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: tldp.org/LDP/abs/html/refcards.html#AEN22102 –  user31894 Jul 30 '10 at 14:54
    
@ghostdog74: Thanks :) –  Svish Jul 30 '10 at 15:11
show 2 more comments

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 Jan 20 '13 at 13:43
add comment

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 http://www.nongnu.org/renameutils/

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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.