Creating 50 directories with 50 files inside

With zsh or bash or yash -o braceexpand:

$ mkdir dir-{01..50}
$ touch dir-{01..50}/file{01..50}.txt
$ ls dir-45
file01.txt file09.txt file17.txt file25.txt file33.txt file41.txt file49.txt
file02.txt file10.txt file18.txt file26.txt file34.txt file42.txt file50.txt
file03.txt file11.txt file19.txt file27.txt file35.txt file43.txt
file04.txt file12.txt file20.txt file28.txt file36.txt file44.txt
file05.txt file13.txt file21.txt file29.txt file37.txt file45.txt
file06.txt file14.txt file22.txt file30.txt file38.txt file46.txt
file07.txt file15.txt file23.txt file31.txt file39.txt file47.txt
file08.txt file16.txt file24.txt file32.txt file40.txt file48.txt

$ tar -cf archive.tar dir-{01..50}

With ksh93:

$ mkdir dir-{01..50%02d}
$ touch dir-{01..50%02d}/file{01..50%02d}.txt
$ tar -cf archive.tar dir-{01..50%02d}

The ksh93 brace expansion takes a printf()-style format string that can be used to create the zero-filled numbers.


With a POSIX sh:

i=0    
while [ "$(( i += 1 ))" -le 50 ]; do
    zi=$( printf '%02d' "$i" )
    mkdir "dir-$zi"

    j=0
    while [ "$(( j += 1 ))" -le 50 ]; do
        zj=$( printf '%02d' "$j" )
        touch "dir-$zi/file$zj.txt"
    done
done

tar -cf archive.tar dir-*  # assuming only the folders we just created exists

An alternative for just creating your tar archive without creating so many files, in bash:

mkdir dir-01
touch dir-01/file{01..50}.txt
tar -cf archive.tar dir-01

for i in {02..50}; do
    mv "dir-$(( i - 1 ))" "dir-$i"
    tar -uf archive.tar "dir-$i"
done

This just creates one of the directories and adds it to the archive. Since all files in all 50 directories are identical in name and contents, it then renames the directory and appends it to the archive in successive iterations to add the other 49 directories.


For each directory, create 50 files:

for i in {1..50}; do
    mkdir dir$i
    touch dir$i/file{1..50}
done

And to do the counting, ls -l dir*/* | nl = 2500


POSIXly, and to avoid problems when that list of 50 x 50 file path potentially exceeding the limit on the size of args+env:

awk 'BEGIN{for (i = 1; i <= 50; i++) printf "dir-%02d\n", i}' |
  xargs mkdir &&

  awk 'BEGIN{for (i = 1; i <= 50; i++)
              for (j = 1; j <= 50; j++)
                printf "dir-%02d/file-%02d.txt\n", i, j}' |
    xargs touch

With zsh (where {01..50} comes from), you can work around the arg list too long with zargs:

autoload zargs
mkdir dir-{01..50}
zargs dir-{01..50}/file-{01..50}.txt -- touch

You can also make dir{02..50} symlinks to dir-01 and use the h tar option:

mkdir dir-01
touch dir-01/file-{01..05}
for d in dir-{02..05}; do ln -s dir-01 "$f"; done
tar zchf file.tar.gz dir-{01..50}