1

Tar version:

root@node: /tmp > tar --version
bsdtar 3.4.2 - libarchive 3.4.2 zlib/1.2.11 liblzma/5.2.5 bz2lib/1.0.8

I have a directory structure which looks like this:

root@node: /tmp > ls -lrth main
total 8
drwxr-xr-x  2 root  wheel   512B Jun 26 00:02 b
-rw-r--r--  1 root  wheel     0B Jun 26 00:02 c.txt
lrwxr-xr-x  1 root  wheel    11B Jun 26 00:02 a -> /tmp/main/b

When I use --exclude on a, I would want b to be excluded as well, but that's not happening. May I know what I could be missing?

I have tried following commands:

root@node : /tmp > tar --exclude /tmp/main/a -zcvf /tmp/test.tar.gz /tmp/main
tar: Removing leading '/' from member names
a tmp/main
a tmp/main/b
a tmp/main/c.txt

root@node : /tmp > tar tf /tmp/test.tar.gz
tmp/main/
tmp/main/b/
tmp/main/c.txt

Also tried using the --dereference option, but no luck:

root@node : /tmp > tar --dereference --exclude /tmp/main/a -zcvf /tmp/test.tar.gz /tmp/main
tar: Removing leading '/' from member names
a tmp/main
a tmp/main/b
a tmp/main/c.txt

root@node : /tmp > tar tf /tmp/test.tar.gz
tmp/main/
tmp/main/b/
tmp/main/c.txt
2
  • 2
    But there is nothing in your command saying that it should exclude b... Are you expecting tar to read the target of the excluded symbolic link and assume that this should be excluded too?
    – Kusalananda
    Commented Jun 26 at 7:23
  • @Kusalananda that's right, I was expecting tar to find the symlink and exclude that too. Commented Jun 26 at 8:09

2 Answers 2

1

It is intended behaviour. It does exclude the file that you asked it to exclude, that is the file which is a symbolic link. Similarly, if you delete the symbolic link using rm, the file that the link pointed to does not get deleted.

If you want to exclude b, you need to specify that explicitly.

Do you have the need to do that programmatically? I would suggest looking into the find and possibly xargs then in order to conjure up some piped command that does that for you.

0

create a textfile with all files/dirs to exclude from the tar archive and invoke tar with the --exclude-from="yourtextfile" option

tar -zcvf /path/to/archive.tar.gz --exclude-from="yourtextfile"

You must log in to answer this question.

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