A process will be writing to a set of directories, one for each hour using date +%H notation, so 00, 01, 02... 22, 23. I'm wanting to keep the leading 0 on single digit hours to keep sorted listings tidy.

I=00; until [ $I -gt 23 ]; do mkdir $I; let I=I+1; done

Has predictable effects of not keeping a leading 0 before the single digits. I could just go and make them by hand, but feel there should be a way to do this. Any ideas?

link|improve this question
feedback

3 Answers

up vote 11 down vote accepted
mkdir $(seq -w 00 23)
link|improve this answer
feedback
mkdir {0{0..9},{10..23}}

Or in Bash 4:

 mkdir {00..23}
link|improve this answer
1  
+1 for not spawning a subprocess. – James Sneeringer Nov 11 '09 at 16:10
+1 , from my IRC hang out: "seq(1) is a highly nonstandard external command used to count to 10 in silly Linux howtos. Use one of these instead: for x in {1..10} (bash3.x) or for ((x=1; x<=10; x++)) (bash 2.04+) or i=1; while [ $i -le 10 ]; do ...; i=$(($i+1)); done (ksh/POSIX)" – Kyle Brandt Nov 11 '09 at 18:46
At least in Bash, you can do ((i++)) to increment a counter in a while loop (or elsewhere). Also, if you use double parentheses you can use <= and leave off the $: i=1; while ((i<=10)); do echo $i; ((i++)); done. – Dennis Williamson Nov 11 '09 at 19:16
feedback
for i in `seq -w 00 23`;do mkdir $i;done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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