11

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?

  • Use gnutar from Brew, MacPort, etc. Apple's tar is too crippled. – jww Mar 20 '16 at 10:44
22

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
  • 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
  • 2
    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
2

This gives you what you want:

tar czvf $NAME-$VERSION.tar.gz $NAME-$VERSION --wildcards --exclude=.git --exclude=.gitignore
1

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.

  • 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 '16 at 10:47
  • Thanks dear @jww for your hint. I have edited my answer to be more effective. – Kayvan Tehrani Mar 21 '16 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.