0

I found these commands in a tutorial that create a parent directory with hundreds of sub-directories, followed by a touch command that creates hundreds 26 empty files in created sub-directories:

mkdir -p playground/dir-{00{1..9},0{10..99},100}

touch playground/dir-{00{1..9},0{10..99},100}/file-{A..Z}

My question is how does this syntax work exactly? What is the bracket expansion doing and what does the 00 and 0 and 100 signify? I know that 100 is the last value but I'm not sure how the 00 and ,0 work. Same question for the touch command as well.

Please help me understand. Thanks.

New contributor
synth45 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2

Well, let's have a look:

1) command mkdir -p playground/dir-{00{1..9},0{10..99},100}

You simply create 100 directories in playground/, each of them in the following format: dir-xxx where xxx are digits. By {00{1..9},0{10..99},100}, you're saying 3 things:

  1. create directories 00x where x is from 1 to 9 that's the 00{1..9} part of it
  2. create directories 0xx where xx are from 10 to 99, that's the 0{10..99}
  3. part of it create directory 100

2) when you have all the directories from 1), then you run the touch command

You simply touch files in the following format file-x where x is one character from the range A-Z. By that {00{1..9},0{10..99},100} part, you're simply saying, you want to touch the files in those directories created in 1). As you can see, the part in braces is the same as in the mkdir command.

Hope it helps. If you want to explore a bit more about brace expansion, you can read for example this: https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html

New contributor
puzzle is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • A "simple" version would probably use {001..100} to generate the numbers from 001 to 100, but that's a different story... – Freddy Aug 14 at 20:11
  • @Freddy perhaps the origins of the OP's code pre-date bash 4.0-alpha (which apparently introduced {0x..0y} (zeropadding brace expansion) - see for example BashChanges: Quoting, expansions, substitutions and related – steeldriver Aug 15 at 2:21
  • @steeldriver Yeah, could be. FYI, this book seems to be the source of the code. – Freddy Aug 15 at 3:52
0

See "Brace expansion" in the bash man page.

Supplied integers may be prefixed with ‘0’ to force each term to have the same width.

https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html

Your Answer

synth45 is a new contributor. Be nice, and check out our Code of Conduct.

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.