I want to create a tar file where all of the directories and files are processed in alphabetical order. This is for the entire directory hierarchy that's being tarred up, so it would start by processing the first directory alphabetically, and then sub-directories in there alphabetically, etc. I looked through the man page and can't find a switch for this.
I will admit, this is half novelty, half slight optimization. I just can't believe that there isn't an easy way to do this. I must be missing something.
Best Answer
Slartibartfast is on the right track, but tar's default behaviour is to descend into directories, so you may get more than one copy of the same file included in the generated tar file. You can check by doing
tar tf file.tar | sort
The workaround is to include the --no-recursion option to tar. Also, you should be able to send in strange filenames by using the-print0
option to find, then using--null
option to tar. The end result looks like this:find paths -print0 | sort -z | tar cf tarfile.tar --no-recursion --null -T -
You can check the order in the tar file by using
tar tsf tarfile.tar
. Although you'll probably never need the -print0, -z, and --null options unless you know you're going to encounter a filename with a newline embedded in it, I've never tried it.