Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is driving me crazy. I want to tar a directory, but exclude some files. This is my command:

tar -zcvf $NAME-$VERSION.tar.gz $NAME-$VERSION --exclude='.git' --exclude='.gitignore'

While the above command works on RedHat EL5, it doesn't work on Mac OS 10.8.2. I have tried replacing the ' with ", \' and \". I have tried removing the equal signs. Nothing works.

What am I doing wrong?

share|improve this question
    
Use gnutar from Brew, MacPort, etc. Apple's tar is too crippled. – jww Mar 20 at 10:44
up vote 10 down vote accepted

All the options need to be together before the arguments, and the -f flag needs to precede the tar file:

tar -zcv --exclude='.git' --exclude='.gitignore' -f $NAME-$VERSION.tar.gz $NAME-$VERSION
share|improve this answer
    
If you do tar --help, does the --exclude option appear in the help? I don't know OSX but perhaps there's a gtar instead of the stock tar. – Flup Mar 1 '13 at 14:23
2  
Ah I see it :) try this: tar --exclude='.git' --exclude='.gitignore' -zcvf $NAME-$VERSION.tar.gz $NAME-$VERSION The -f flag expects a filename as the next argument. – Flup Mar 1 '13 at 14:24
    
Sorry yes, edited. I'm new here so bear with me! – Flup Mar 1 '13 at 14:27
    
Can attest that this works on a Mac - 10.9. Took me a while to find the right version of this which is why I leave my comment here! – JohnAllen Oct 23 '15 at 1:38

This gives you what you want:

tar czvf $NAME-$VERSION.tar.gz $NAME-$VERSION --wildcards --exclude=.git --exclude=.gitignore
share|improve this answer

It seems you like to filter .git meta directories, the easiest way is using --exclude-vcs to exclude version control system directories:

tar -zcvf $NAME-$VERSION.tar.gz $NAME-$VERSION --exclude-vsc

I have used "tar (GNU tar) 1.27.1" on my Linux machine, it seems mac's BSD General Command(non GNU version) does not support this option.

share|improve this answer
    
Are you certain this works with Apple's tar? See Apple's tar(1) man page. it does not list --exclude-vsc or --exclude-vcs. – jww Mar 20 at 10:47
    
Thanks dear @jww for your hint. I have edited my answer to be more effective. – Kayvan Tehrani Mar 21 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .