tar a lot of files from find

Posted on Sun 08 September 2019 in linux

The Naive Way

For files with a certain pattern to be put into a tarball, the naive idea is:

find . [-OPTION PARAM] -exec tar cvf foo.tar {} +

However, when there are a lot of matched files (e.g. 100+), the output tarball just fails to contain all of them.

It is because find chops up its result into several chunks before handing over to tar.

How to Circumvent

1. Append the files into the tarball rather than "create" a tarball [1]

Use -r in tar for appending.

find . [-OPTION PARAM] -exec tar rvf foo.tar {} +
                                 ^

2. Suppress the chop-up [2]

Print the full result and pipe to tar as stdin.

find . [-OPTION PARAM] -print | tar cvf foo.tar --files-from -