Using find, xargs, and tar to create a huge archive (Cygwin, Linux, Unix)

By Alvin J. Alexander, devdaily.com

I did somethings wrong in a previous blog entry that led me to use the "pax" command. There's nothing particularly wrong with the pax command, other than the fact that it's not available for Cygwin, and I really needed to created a huge archive. (I know that pax is available for our Linux and Unix systems, but I can't find a version for Cygwin.)

In my earlier entry I stated that something like this did not work for me:

find . -type f -name "*.java" | xargs tar cvf myfile.tar

What I think was happening was that as xargs was managing the input to the tar command, tar kept re-writing the archive. That is, each time xargs passed a new block of input files to tar, tar perceived it as a new command, and went on to re-create the file named myfile.tar. So, instead of the huge myfile.tar that I expected, I ended up with only a few files in the archive.

This problem is easily remedied if you use the 'r' switch/command with tar instead of the 'c' switch/command. The 'r' switch tells tar to append to the archive, while 'c' says "create".

All that being said, this command worked just fine for me to create a very large tar archive:

find . -type f -name "*.java" | xargs tar rvf myfile.tar

This combination of find, tar, and xargs worked like a champ for me. I guess this is one of those things where Unix is "intuitively obvious once you know how to do it", because in retrospect this seems like the obvious solution.


devdaily logo