Take the 2-minute tour ×

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.

share|improve this question
Why do you want to do this? –  matthias krull Aug 5 '10 at 15:02
Mostly, it's because I want to know how close the tar operation is to being completed. When the files are being loaded in random order, there's no way to tell with the -v flag. –  Erick Robertson Aug 5 '10 at 16:01
That's not entirely true; If you pipe the output to a file and know the number of files (say a quick find command), you can compare the -v output (wc -l) with the number of files from find to get a sense of progress... –  Slartibartfast Aug 6 '10 at 1:45
add comment

4 Answers

The order of the files within the tar file does not really matter, since when the files are extracted, the filesystem will not preserve the order anyway.

There is no switch for this, but if you really wanted it, you could provide tar with a list of filenames in sorted order, and it would create the tar file with the order you give it.

% tar cf tarfile tmp/diff.txt src/hellow.c junkimage.IMG barry/thegroup
% tar tf tarfile
share|improve this answer
or just sort the output: tar tf tarfile | sort –  Doug Harris Aug 5 '10 at 14:53
I have way too many files (20,000+) to specify them all on the command line. –  Erick Robertson Aug 5 '10 at 16:00
The order of the files within the tar file does matter if you need to decompress and display while downloading. –  Erik Oct 5 '12 at 7:12
add comment

Assuming you don't have any files with newlines in the names:

find /source_directory -print | sort | tar -czf target.tgz -T -

If that doesn't work (never tried it, so I don't know of - means stdin for the -T argument):

find /source_directory -print | sort > /tmp/temporary_file_list
tar -czf target.tgz -T /tmp/temporary_file_list

Then there is the question of why. But sometimes it is easier not to ask.

share|improve this answer
add comment

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.

share|improve this answer
Excellent suggestion for using the --no-recursion option, thanks. –  Erik Oct 5 '12 at 7:20
This is the solution that worked for me. I have a different use case than Erick and Google brought me here. I am collecting snapshots over time of the complete state of a remote system. The data is highly redundant. Sorting the tar input by time (filenames have a timestamp) improves the compressor's performance. A quick test shows an improvement by factor 2 (lzma2). Also, I do not unpack the archive into a filesystem, but do a stream processing over tar entries. A sorted stream makes a lot nicer debug output and has other benefits in the process chain. +1 –  JoBo Feb 8 at 13:27
add comment
find . -depth -print0 | sort -z | pax -wvd0 > file.tar

Pax is sort of the POSIX successor to cpio and tar and kind of fuses the best aspects of both. It writes tar archives (ustar) by default. It also does automatic spanning and prompting for media and prints a summary when it's done.

share|improve this answer
add comment

Your Answer


