# convert single digit to double digits

for f ([0-9].txt) zmv \$f '\${(l:1::0:)}'\$f
zsh: add leading zero ... altogether pointless, as there can only be a maximum of 10 'single digit' files, and so a maximum of 10 files the command can act on. Padding further zeros will produce '0010', '001' and so break sequance. The only proper method is to itterate the numbers like so: i=1; for f (*) zmv \$f '\${(l:3::0:)\$((++i))}'.txt but this has the unfortunate side effect of incrementing the values by 1 ... which may not be desirable.
Sample Output
```% touch {1-9}.txt 10.txt
% for f (*.txt) zmv \$f '\${(l:1::0:)}'\$f
% ls
01.txt
02.txt
03.txt
04.txt
05.txt
06.txt
07.txt
08.txt
09.txt
10.txt```

0
2013-03-22 01:53:42

## 7 Alternatives + Submit Alt

• from 1.ogg 2.ogg 3.ogg 10.ogg 11.ogg to 01.ogg 02.ogg 03.ogg 10.ogg 11.ogg

21
for i in ?.ogg; do mv \$i 0\$i; done
· 2012-05-15 02:52:52
• Uses 'rename' to pad zeros in front of first existing number in each filename. The "--" is not required, but it will prevent errors on filenames which start with "-". You can change the "2d" to any number you want, equaling the total numeric output: aka, 4d = ????, 8d = ????????, etc. I setup a handful of handy functions to this effect (because I couldn't figure out how to insert a var for the value) in the form of 'padnum?', such as: padnum5 () { /usr/bin/rename 's/\d+/sprintf("%05d",\$&)/e' -- \$@ } Which would change a file "foo-1.txt" to "foo-00001.txt"

6
rename 's/\d+/sprintf("%02d",\$&)/e' -- \$@
· 2013-02-14 18:29:18
• works only in zsh, requires autoload zmv

2
zmv '(?.ogg)' '0\$1'
· 2012-05-15 18:17:09
• each number in a file name gets expanded to the number of digets provided as arg_1 of the arguments in rjust_file_nums. Put the funciton in the .bashrc file. Be sure to \$ source ~/.bashrc so that the function will be accessible from bash. Show Sample Output

2
function rjust_file_nums() {for i in *.ogg; do; mv \$i `ruby -e "print ARGV.first.gsub(/\d+/){|d| d.rjust(\$1,'0')}" \$i`; done}
· 2012-05-19 15:41:06
• each number in a file name gets expanded to the number of digets provided as arg_1 of the arguments in rjust_file_nums. Put the funciton in the .bashrc file. Be sure to \$ source ~/.bashrc so that the function will be accessible from bash. Show Sample Output

1
function rjust_file_nums(){for i in *.ogg; do; mv \$i `ruby -e "print ARGV.first.gsub(/\d+/){|d| d.rjust(\$1,'0')}" \$i`; done }
· 2012-05-19 15:39:39

### What Others Think

Pug Puppies for Sale Near Me pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under \$500 Near Me pug puppies for sale in texas Amazing! This blog looks just like my old one! It's on a completely different subject but it has pretty much the same layout and design. Wonderful choice of colors!
rahimhh21 · 102 weeks and 1 day ago

### What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.