0

I have following in a directory:

dir1
dir2
file1
file2

I want to tar up everything except dir1

When I do:

find . -path '*dir1*' -prune -o -print

I don't see dir1 in the output ( as expected)

But when I do:

find . -path '*dir1*' -prune -o -exec tar -czvf documents.tgz  '{}' \+

I see that dir1 also gets tarred up in documents.tgz. Why ?

1
  • You could just use * instead of . to exclude the current working directory. Oct 15, 2014 at 8:38

2 Answers 2

2

It's because of the . in the output. . also gets tared.

Use this intead, it will exclude both; . and dir1:

find . -not -path '*dir1*' -not -path "." -exec tar -czvf documents.tgz  '{}' \+

See the contents of documents.tgz:

$ tar tf documents.tgz
./file2
./file1
./dir2/
1

tar also has an "exclude" option you can add one or more times.

For example:

tar czvf documents.tgz --exlude dir1 --exclude dir2 * 

Note: I would rather create the tar in a different directory, to avoid including documents.tgz itself in tar in case you run the command another time without deleting it in the meantime.

You must log in to answer this question.

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