VaTo
VaTo

Reputation: 3098

tar --exclude flag not excluding the directory if I add the '/' at the end of the path

if I use this command:

sudo tar -zcvpf exclusion.tar.gz mybkup/ --exclude=mybkup/etc/ssh/

then If I try to see if the ssh directory is included in the tarball contents I see that in fact is included:

[user@smashingx1 ~]$ tar -tvzf exclusion.tar.gz | grep ssh
lrwxrwxrwx user/user         0 2016-11-30 04:26 mybkup/etc/systemd/system/multi-user.target.wants/sshd.service -> /usr/lib/systemd/system/sshd.service
-rw-r----- user/user       506 2016-11-30 04:26 mybkup/etc/sysconfig/sshd
-rw-r--r-- user/user      5996 2016-11-30 04:26 mybkup/etc/xdg/autostart/gnome-keyring-ssh.desktop
drwxr-xr-x user/user         0 2016-11-30 04:26 mybkup/etc/ssh/
-rw-r--r-- user/user    242153 2016-11-30 04:26 mybkup/etc/ssh/moduli
-rw-r--r-- user/user      2208 2016-11-30 04:26 mybkup/etc/ssh/ssh_config
-rw-r--r-- user/user       382 2016-11-30 04:26 mybkup/etc/ssh/ssh_host_rsa_key.pub
-rw-r--r-- user/user       162 2016-11-30 04:26 mybkup/etc/ssh/ssh_host_ecdsa_key.pub
-rw-r--r-- user/user      4760 2016-11-30 04:26 mybkup/etc/ssh/sshd_config
-rw-r--r-- user/user        82 2016-11-30 04:26 mybkup/etc/ssh/ssh_host_ed25519_key.pub
-rw-r----- user/user      1675 2016-11-30 04:26 mybkup/etc/ssh/ssh_host_rsa_key
-rw-r----- user/user       227 2016-11-30 04:26 mybkup/etc/ssh/ssh_host_ecdsa_key
-rw------- user/user      4361 2016-11-30 04:26 mybkup/etc/ssh/sshd_config.rpmnew
-rw-r----- user/user       387 2016-11-30 04:26 mybkup/etc/ssh/ssh_host_ed25519_key
-rw-r--r-- user/user      8730 2016-11-30 04:26 mybkup/etc/selinux/targeted/modules/active/modules/rssh.pp
-rw-r--r-- user/user     18774 2016-11-30 04:26 mybkup/etc/selinux/targeted/modules/active/modules/ssh.pp
-rw-r--r-- user/user       904 2016-11-30 04:26 mybkup/etc/pam.d/sshd

but if I dont add the last '/' at the end of the directory path now it excludes the directory:

sudo tar -zcvpf exclusion.tar.gz mybkup/ --exclude=mybkup/etc/ssh

tar -tvzf exclusion.tar.gz | grep ssh

lrwxrwxrwx user/user         0 2016-11-30 04:26 mybkup/etc/systemd/system/multi-user.target.wants/sshd.service -> /usr/lib/systemd/system/sshd.service
-rw-r----- user/user       506 2016-11-30 04:26 mybkup/etc/sysconfig/sshd
-rw-r--r-- user/user      5996 2016-11-30 04:26 mybkup/etc/xdg/autostart/gnome-keyring-ssh.desktop
-rw-r--r-- user/user      8730 2016-11-30 04:26 mybkup/etc/selinux/targeted/modules/active/modules/rssh.pp
-rw-r--r-- user/user     18774 2016-11-30 04:26 mybkup/etc/selinux/targeted/modules/active/modules/ssh.pp
-rw-r--r-- user/user       904 2016-11-30 04:26 mybkup/etc/pam.d/sshd

I am confused because I thought that by instructing tar to exclude the directory, it would exclude the directory and its contents, but apparently is not the case, can somebody explain please?

Upvotes: 1

Views: 262

Answers (2)

janos
janos

Reputation: 124804

First of all, a quick tip. You can test the behavior easier without actually creating a tar file, like this:

tar c mybkup/ --exclude=mybkup/etc/ssh/ | tar t

I hope you can appreciate the beauty in that.

The exclude pattern definition is very primitive. And sadly, man tar doesn't explain anything about how "PATTERN" should be written and how it is interpreted.

Based on some observations, I came up with the following reasoning. In your example, mybkup/etc/ssh/ is not excluded because it is neither a file nor a directory. Although mybkup/etc/ssh is a directory, but mybkup/etc/ssh/ is not. In other words, the trailing / is not part of the name of the directory, and so it's not treated as a directory, and therefore simply ignored, not excluded.

I'm afraid that's just the way it is. This is such a primitive feature.

Shell globs are ok though. For example, given your files, if you do this:

tar c mybkup --exclude='mybkup/etc/sy*' | tar t

It will exclude mybkup/etc/systemd and mybkup/etc/sysconfig directories and their contents. (Note that it's important to quote mybkup/etc/sy* so that tar will interpret the * instead of the shell.)

Upvotes: 1

Rakib
Rakib

Reputation: 2096

--exclude=PATTERN

exclude files, given as a PATTERN

when you use --exclude=mybackup/etc/ssh/ tar doesn't get any exact match of the patter. and when you use --exclude=mybackup/etc/ssh tar get exact match of the patter.

Upvotes: 0

Related Questions