I know with mkdir I can do mkdir A B C D E F to create each directory. How do I create directories A-Z or 1-100 with out typing in each letter or number?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The {} syntax is Bash syntax not tied to the for construct.
mkdir {A..Z}
is sufficient all by itself.
http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion
Method 2
It’s probably easiest to just use a for loop:
for char in {A..Z}; do
mkdir $char
done
for num in {1..100}; do
mkdir $num
done
You need at least bash 3.0 though; otherwise you have to use something like seq
Method 3
You can also do more complex combinations (try these with echo instead of mkdir so there’s no cleanup afterwards):
Compare
$ echo pre-{{F..G},{3..4},{m..n}}-post
pre-F-post pre-G-post pre-3-post pre-4-post pre-m-post pre-n-post
to
$ echo pre-{F..G}{3..4}{m..n}-post
pre-F3m-post pre-F3n-post pre-F4m-post pre-F4n-post pre-G3m-post pre-G3n-post
pre-G4m-post pre-G4n-post
If you have Bash 4, try
$ echo file{0001..10}
file0001 file0002 file0003 file0004 file0005 file0006 file0007 file0008 file0009
file0010
and
$ echo file{0001..10..2}
file0001 file0003 file0005 file0007 file0009
Method 4
On Linux you can generate sequences of digits with the “seq” command, but this doesn’t exist on all Unix systems. For example to generate directories from 1 to 100:
mkdir `seq 1 100`
While you can certainly make directories A to Z with shell utils:
seq 65 90
| while read foo; do printf "%bn" `printf '\\x%xn' $foo`; done
| xargs mkdir
It’s probably a lot less ugly to just use Perl:
perl -e 'foreach (ord "A"..ord "Z") { mkdir chr $_ }'
Method 5
YAA ( Yet Another Answer 😉
Under bash, there is a lot of thing you can do! Simple sample:
mkdir -p /tmp/mkdirdemo/1stLev_{R..T}{0..2}/2ndL_{one,two,three}_{a..c}{a..z}/3rd_{30..42}
This will create 9 directories, with 2106 subdirectories and 27378 third level subdirs.
ls -d /tmp/mkdirdemo/1stLev_*/*/*|wc -l 27378 find /tmp/mkdirdemo -mindepth 3 -type d | wc -l 27378 $ find /tmp/mkdirdemo -mindepth 3 -type d|sort|sed -ne '1,2p;27377,$p;3s/.*/.../p' /tmp/mkdirdemo/1stLev_R0/2ndL_one_aa/3rd_30 /tmp/mkdirdemo/1stLev_R0/2ndL_one_aa/3rd_31 ... /tmp/mkdirdemo/1stLev_T2/2ndL_two_cz/3rd_41 /tmp/mkdirdemo/1stLev_T2/2ndL_two_cz/3rd_42
Method 6
mkdir direct{1..3} will result in mkdir direct1 direct2 direct3 and so on . Same for {a..z}
Method 7
mkdir {A..Z}
mkdir {0..100}
mkdir test_{A..Z}
and so on.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0