I have the following directory structure (all names given here are directories; they all contain files and subdirectories):

a/a, a/b, a/c, ..., a/x, a/y, a/z
a/a/a, a/a/b, a/a/c, ..., a/a/x, a/a/y, a/a/z
a/b/a, a/b/b, a/b/c, ..., a/b/x, a/b/y, a/b/z
a/x/a, a/x/b, a/x/c, ..., a/x/x, a/x/y, a/x/z

The letters are just descriptive; they're not the real names. Also, the depth is variable, with a maximum of around 10 subdirectories.

I would like to tar the whole tree from directory a but exclude anything named x. So, in the above, I would like to tar everything except a/x and everything under it; a/a/x and everything under it; and a/b/x and everything under it.

I was thinking of using find and grep -v but I'm wondering, is there an easier way?

  • 2
    Are you using GNU tar, which has --exclude?
    – Kusalananda
    Apr 13, 2021 at 11:43
  • @Kusalananda yes, GNU tar. Now I see that --exclude is exactly what I need! Thanks for pointing it out.
    – k314159
    Apr 13, 2021 at 12:09

1 Answer 1


Thanks for @Kusalananda for the comment, I can use the --exclude option available in GNU tar:

To exclude everything under any directory named x (but not the directory itself):

tar cf a.tar --exclude='x/*' a

To exclude directory x and everything under it (but note that this also excludes any regular files named x too):

tar cf a.tar --exclude='x' a

You must log in to answer this question.

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