Linux – tar a directory and only include certain file types

command-line-interfacelinuxtar

Following the instructions here: http://linuxdevcenter.com/pub/a/linux/lpt/20_08.html

I'm aiming to tar up a directory but only want to include .php files from that directory.

Given the aforementioned instructions, I've come up with this command. It creates a file called IncludeTheseFiles which lists all the .php files, then the tar is supposed to do it's job only using the files listed in IncludeTheseFiles

find myProjectDirectory -type f -print | \
egrep '(\.[php]|[Mm]akefile)$' > IncludeTheseFiles
tar cvf myProjectTarName -I IncludeTheseFiles

However, when I run this it doesn't like the I include option?

tar: invalid option -- I

Best Answer

This does the trick, no comparison file needed:

find myProjectDirectory -type f \( -name \*\.php -o -name \*\.js -o -name \*\.css -o -name \*\.inc \) | xargs tar -rf myProjectTarName.tar