1

I have a folder in Home directory with the following structure:

top-tree
+-- .git
+-- branch1
|   +-- branch11
    |   +-- .idea
    |   +-- branch111
    |   +-- branch112
    |   +-- branch113
    |   +-- branch114
        |   +-- branch1141
        |   +-- branch1142
        |   +-- branch1143
    |   +-- branch115
    |   +-- branch116
    |   +-- branch117

Now, I want to tar the entire contents but must exclude all the hidden directories like .git and .idea and one particular directory branch114 which has at least 2 subdirectories (the number of subdirectories may vary from time to time).

I tried to do this by using the tar command with the following parameters:

tar -czvf archive.tar.gz --exclude=~/top-tree/branch1/branch11/branch114 --exclude=~/top-tree/.git --exclude=~/top-tree/branch1/branch11/.idea top-tree/

tar -czvf archive.tar.gz --exclude=~/top-tree/branch1/branch11/branch114 --exclude=~/top-tree/.*

But none of the above seems to work. Every single time, the entire contents of the folder top-tree is placed in archive.tar.gz. I've tried out a lot of things (so many that I can't quote here including this one) suggested in this website and others, played with the permutations of parameters etc. But unlike other similar questions, mine seems like it's a problem with the path. The tar version I'm using is 1.29. Why doesn't this work?

  • The --exclude option does not apply to tar but to GNU tar. – schily Mar 21 at 13:36
  • Possible duplicate of tar directory and exclude multiple subdirectories – Mr Shunz Mar 21 at 15:27
  • at least one issue is that the exclude states an absolute path but the path to archive is relative, thus not the same path for (gnu)tar (even if it's the same path on the filesystem for the given cwd). Beside this, duplicate still applies – A.B Mar 21 at 18:34
2

You're archiving paths of the form top-tree/stuff and excluding paths of the form /home/skrowten/top-tree/stuff. Your exclusion patterns never match anything.

The simple solution is

tar -czvf archive.tar.gz --exclude=top-tree/branch1/branch11/branch114 --exclude=top-tree/.git --exclude=top-tree/branch1/branch11/.idea top-tree

Note that if there is e.g. a top-tree/subdir/top-tree/.git then this will be excluded as well. A trick to avoid this is to start the command line path with ./, and rely on the fact that . will only ever appear at the root.

tar -czvf archive.tar.gz --exclude=./top-tree/branch1/branch11/branch114 --exclude=./top-tree/.git --exclude=./top-tree/branch1/branch11/.idea ./top-tree

If the leading ./ bothers you, add the option --transform='s!^\./!!' to the tar call.

  • I figured it must be something like this. You mean the exclude parameter must have /top-tree at the highest level and not ~/, right? But unfortunately, your suggestion doesn't work as well. I'm pulling my hair here! – skrowten_hermit Mar 21 at 10:36
  • Oh right, no, tar doesn't do that. You need a relative path. I've corrected my answer. – Gilles Mar 21 at 10:40
  • Great! It worked. But the path now is preceded by a . as in ./top-tree/ is the parent directory instead of top-tree/ It would be nice if you add how to (1) make top-tree/ the parent directory and (2) to exclude any hidden folder within top-tree/. Could yo do that? – skrowten_hermit Mar 21 at 10:47
  • 1
    @skrowten_hermit --transform='s!^\./!!' – Gilles Mar 21 at 11: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.