Tar Command – Tar Up All PDFs in a Directory Retaining Structure

findrecursivetar

I'm trying to create a compressed tarball that contains all PDF files that exist in one of my directories. The directory structure needs to be retained. Empty directories are not needed, but I really don't care if they're there.

For example, say I had a directory that looked like this:

dir
dir/subdir1
dir/subdir1/subsubdir1/song.mp3
dir/subdir2
dir/subdir2/subsubdir1
dir/subdir2/subsubdir1/document.pdf
dir/subdir2/subsubdir1/another-song.mp3
dir/subdir2/subsubdir1/top-ten-movies.txt
dir/subdir3
dir/subdir3/another-document.pdf

After running the command, I'd like to have dir.tar.gz contain this:

dir
dir/subdir2
dir/subdir2/subsubdir1
dir/subdir2/subsubdir1/document.pdf
dir/subdir3
dir/subdir3/another-document.pdf

Possible?

Best Answer

This will list all the PDFs:

$ find dir/ -name '*.pdf'
./dir/subdir2/subsubdir1/document.pdf
./dir/subdir3/another-document.pdf

You can pipe that to xargs to get it as a single space-delimited line, and feed that to tar to create the archive:

$ find dir/ -name '*.pdf' | xargs tar czf dir.tar.gz

(This way omits the empty directories)

Related Question