$ touch dir/{{1..8},{a..p}}$ tar cJvf file.tar.xz dir/dir/dir/odir/kdir/bdir/3dir/1dir/idir/7dir/4dir/edir/adir/gdir/2dir/ddir/5dir/8dir/cdir/ndir/fdir/hdir/6dir/ldir/mdir/jdir/pI would have expected it to be alphabetical. But apparently it's not. What's the formula, here?
How is the order in which tar works on files determined?
linux;tar;gnu;coreutils
As @samiam has stated the list is returned to you in a semi-random order via readdir(). I'll just add the following.The list returned is what I would call the directory order. On older filesystems, the order is often the creation order that the file entries in the directory's table were added. There is of course a caveat to this, when a directory entry is deleted, this entry is then recycled, so any subsequent files that are stored will replace the previous entry, so the order will no longer by based solely on creation time.On modern filesystems where directory data structures are based on a search tree or hash table, the order is practically unpredictable.ExamplesPoking at the files created when you run your touch command reveals the following inodes were assigned.$ touch dir/{{1..8},{a..p}}$ stat --printf=%n -- %i\n dir/*dir/1 -- 10883235dir/2 -- 10883236dir/3 -- 10883242dir/4 -- 10883243dir/5 -- 10883244dir/6 -- 10883245dir/7 -- 10883246dir/8 -- 10883247dir/a -- 10883248dir/b -- 10883249dir/c -- 10883250dir/d -- 10883251dir/e -- 10883252dir/f -- 10883253dir/g -- 10883254dir/h -- 10883255dir/i -- 10883256dir/j -- 10883299dir/k -- 10883302dir/l -- 10883303dir/m -- 10883311dir/n -- 10883424dir/o -- 10883426dir/p -- 10883427So we can see that the brace expansion used by touch creates the filenames in alphabetical order and so they're assigned sequential inode numbers when written to the HDD. (That however does not influence the order in the directory.)Running your tar command multiple times would seem to indicate that there is an order to the list, since running it multiple times yields the same list each time. Here I've run it 100 times and then compared the runs and they're all identical.$ for i in {1..100};do tar cJvf file.tar.xz dir/ > run${i};done$ for i in {1..100};do cmp run1 run${i};done$ If we strategically delete say dir/e and then add a new file dir/ee we can see that this new file has taken the place that dir/e occupied prior in the directories entries table.$ rm dir/e$ touch dir/eeNow let's keep the output from one of the for loop above, just the 1st one.$ mv run1 r1ANow if we re-run the for loop that will run the tar command 100 times again, and compare this second run with the previous one:$ sdiff r1A run1dir/                                dir/...dir/c                               dir/cdir/f                               dir/fdir/e                             | dir/eedir/o                               dir/odir/2                               dir/2...We notice that dir/ee has taken dir/e's place in the directories table.