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.

link|improve this question

67% accept rate
1  
Why do you want to do this? – mugen kenichi 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
1  
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
feedback

3 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
tmp/diff.txt
src/hellow.c
junkimage.IMG
barry/thegroup
link|improve this answer
2  
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
feedback

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.

link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.