2

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?

schily
  • 19,173
skrowten_hermit
  • 751
  • 4
  • 13
  • 34

1 Answers1

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.