I am trying to create a sequence of identical directories with names like 'file001' in sequence from 1 to 512. I am running the following in the shell:

for i in $(seq 001 512):
        mkdir file$i

Though this is creating all 512 directories, but they are being labelled as 'file1','file2'... instead of 'file001','file002' and so on. Can anyone please suggest some other way to create the folders in the way I require?

  • 1
    Does your seq have a -w option? – steeldriver Sep 12 '18 at 12:22

Try this instead:

mkdir file{001..512}

Use either of

for num in $(seq -w 001 512); do
    mkdir "file$num"

or, with bash release 4.0 or later (i.e not the default bash on macOS),

mkdir file{001..512}

With older bash if seq is unavailable:

for num in {1..512}; do
    mkdir "file$( printf '%03d' "$num" )"
  • Note that {001..512} comes from zsh decades before bash. macOS has always had zsh, zsh even used to be /bin/sh there. You can also do printf '%03d\n' {1..512} | xargs mkdir – Stéphane Chazelas Sep 15 '18 at 16:27

No need of for Loop. Try this :

mkdir `echo file{001..512}`

As pointed by in comments :

There is no need for command substitution and echo.

So, Brace Expansion is sufficient enough to achieve the task :

mkdir file{001..512}
  • 2
    There is no need for command substitution and echo. – maulinglawns Sep 12 '18 at 12:25
  • Yes, just saw your answer. :) – C0deDaedalus Sep 12 '18 at 12:25

Your Answer

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

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