tar: --exclude=".git": Cannot stat: No such file or directory

11

3

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?

Randomblue

Posted 2013-03-01T14:12:20.897

Reputation: 2 547

Use gnutar from Brew, MacPort, etc. Apple's tar is too crippled. – jww – 2016-03-20T10:44:47.577

Answers

23

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

Flup

Posted 2013-03-01T14:12:20.897

Reputation: 3 151

2Can 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 – 2015-10-23T01:38:32.547

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 – 2013-03-01T14:23:09.700

2Ah 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 – 2013-03-01T14:24:52.117

Sorry yes, edited. I'm new here so bear with me! – Flup – 2013-03-01T14:27:47.123

2

This gives you what you want:

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

Sonde

Posted 2013-03-01T14:12:20.897

Reputation: 21

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.

Kayvan Tehrani

Posted 2013-03-01T14:12:20.897

Reputation: 113

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 – 2016-03-20T10:47:47.533

Thanks dear @jww for your hint. I have edited my answer to be more effective. – Kayvan Tehrani – 2016-03-21T12:04:12.983