Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a directory like this:

randomized_quad015.png
randomized_quad030.png
randomized_quad045.png
randomized_quad060.png
randomized_quad075.png
randomized_quad090.png
randomized_quad1005.png
randomized_quad1020.png
randomized_quad1035.png
randomized_quad1050.png
randomized_quad105.png
randomized_quad1065.png
randomized_quad1080.png
randomized_quad1095.png
randomized_quad1110.png
randomized_quad1125.png
randomized_quad1140.png

and I want to rename the first files adding 0 in front of number, like:

randomized_quad0015.png

But I don't know how. Some help?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

As an alternative to the various Perl-based methods in other answers, you can do this particular example, and lots of other similar ones with plain bash:

for i in randomized_quad???.png; do   
  mv $i randomized_quad0${i#randomized_quad}
done

This loops over the filenames with only three characters in the number region, and the variable expression in braces takes each filename, snips "randomized_quad" off the front (with the # operator), and adds "randomized_quad0" on the front.

Other excitingly useful operators like this include:

  • % - cut something off the end
  • / - search and replace (/ for the first match, // for all matches)
  • : - for extracting substrings

A more complete list with examples is here. Also, note that these use POSIX-style regular expressions, so . will match any character, not just a "." If you want to match a ".", you should 'escape' it, with a backslash:

TestVariable1="filename.png"
TestVariable2="filename0png"

echo "${TestVariable1%.png}"
> filename

echo "${TestVariable2%.png}"
> filename

echo "${TestVariable1%\.png}"
> filename

echo "${TestVariable2%\.png}"
> filename0png
share|improve this answer

rename is a command-line tool that lets you rename files, using a Perl command to transform the name. A Perl command that's commonly used in this circumstance is s/REGEXP/REPLACEMENT/.

If I interpret your question correctly, you want to add a leading 0 in the names that have fewer than 4 digits, so that all the names end up having 4 digits. This slightly complicated expression handles the general case where you have file names with 1, 2 or 3 digits: it aligns all of them to 4 digits. The e modifier at the end causes the replacement to be interpreted not as a string but as a Perl expression. This expression repeats the digit 0 as many times as necessary to pad the number to 4 digits.

rename 's/(?=([0-9]+)\.)/"0" x (4 - length($1))/e' *.png

If the files you want to rename all have three digits, there are simpler ways, such as

rename 's/[0-9]+\./0$&/' *[!0-9][0-9][0-9][0-9].png

or

rename 's/(?<=[^0-9])[0-9]{3}\./0$&/' *.png
share|improve this answer

With a recent version of bash you could write:

for f in randomized_quad*.png; do
    if [[ $f =~ [1-9][0-9]* ]]; then
        new=$(printf "randomized_quad%04d.png" ${BASH_REMATCH[0]})
        [[ $f != $new ]] && mv -v $f $new
    fi
done
share|improve this answer
rename 's/quad([0-9]{3})\.png/quad0$1/' randomized_quad*
share|improve this answer
    
you might mean {3} –  J.F. Sebastian Sep 13 '12 at 18:58

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.