15

I need to create folders starting from 00 to 99 (00, 01, 02, 03, etc....) in several hundred places. Is there a single line command that will let me do that?

44

mulaz's answer is correct, but many people say seq is evil beacuse most shells will let you do the following

mkdir {00..99}

However in some older versions of bash, 0-9 arent padded, so you would have to do

mkdir 0{0..9} {10..99}
  • 9
    +1 Should be the accepted answer IMHO. Not only is this idiomatic Bash, it doesn't require using an external program (which seq is). – Trollhorn Jun 12 '12 at 9:45
  • 1
    This is how it should be done. – phoxis Jun 12 '12 at 15:09
  • 7
    The following works too: > mkdir {0..9}{0..9} – Orieg Jun 12 '12 at 21:37
17

Will this do?

for i in `seq -w 0 99`; do mkdir $i; done

does a loop for numbers 0-99, and "-w" sets the equal width (0 padding for 0-9)

  • 7
    seq -w 0 99 | xargs mkdir would also do the job. – Jay Jun 12 '12 at 0:24
  • 11
    You can ditch the loop and just do mkdir $(seq -w 0 99). Or use backticks instead of $(), but I can't put backticks in because of serverfault syntax. – Patrick Jun 12 '12 at 0:52
  • @Patrick: Yes, you can: mkdir `seq -w 0 99` (I couldn't avoid the extra space). See here, but it looks like the trick of including spaces in the delimiters doesn't work here. – Keith Thompson Jun 12 '12 at 1:24
  • 2
    @Patrick backticks are bad: mywiki.wooledge.org/BashFAQ/082 – Andrew Jun 12 '12 at 2:21
  • @Andrew yes I am aware of that, but I prefer to stay with the coding style of whatever I'm replying to. – Patrick Jun 12 '12 at 2:27
2

I know this is old, but my recommendation would be:

for i in seq -f %02g 0 99 ; do mkdir $i ; done

the -f %02g ensures it stays at least two characters, such as 00 or 99, and will still allow 3 character numbers past 99 so if you have 100 it will not become 001. It will be 00-99 100 instead of 001-100 such as the -w does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy