Proximus
Proximus

Reputation: 311

tar command not excluding files properly

While running a tar command today, I was trying to exclude a directory of files inside of the file system. I ran this command -

tar --exclude='./my/directory/here' -zcvf backup.tar.gz docroot/

However, when it was tar'ing the directory, it was still including files from the "here". How can I get it to exclude the files properly?

Mine is not working correctly - see demo -

$ sudo tar --exclude='./jsmith/testDirectory/' -zcvf backup.tar.gz ./jsmith/
[sudo] password for jsmith:
./jsmith/
./jsmith/.bash_logout
./jsmith/backup2015-9-8.sql.gz
./jsmith/.mysql_history
./jsmith/.bashrc
./jsmith/testDirectory/
./jsmith/testDirectory/test4.sh
./jsmith/testDirectory/test2.sh
./jsmith/testDirectory/test6.sh
./jsmith/testDirectory/deploy/
./jsmith/testDirectory/test8.sh
./jsmith/testDirectory/tets12.sh
./jsmith/testDirectory/test1.sh
./jsmith/testDirectory/test7.sh
./jsmith/testDirectory/.sh
./jsmith/testDirectory/test9.sh
./jsmith/testDirectory/test11.sh
./jsmith/testDirectory/test10.sh
./jsmith/testDirectory/test3.sh
./jsmith/testDirectory/test5.sh
./jsmith/.viminfo
./jsmith/.drush/
./jsmith/.drush/cache/
./jsmith/.drush/cache/usage/
./jsmith/.drush/cache/default/

Upvotes: 0

Views: 533

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295423

Your naming needs to be consistent!

tar --exclude='./my/directory/here' -zcvf backup.tar.gz docroot/

...should be, instead, something like:

tar --exclude='docroot/my/directory/here' -zcvf backup.tar.gz docroot/

...which is to say: If you don't use a leading ./ in the name of the files to be included in the tarball, you can't use a leading ./ in the name of the files to exclude. (The inverse applies as well: If you did use ./docroot in the filename, you would need to use it in the exclude pattern as well).


Observe:

$ mkdir -p docroot/my/directory/here/content docroot/other/content
$ tar --exclude='./docroot/my/directory/here/' -zcvf backup.tar ./docroot/
a ./docroot
a ./docroot/my
a ./docroot/other
a ./docroot/other/content
a ./docroot/my/directory

Notably, while my/directory is present, my/directory/here is not, and neither is my/directory/here/content.

Upvotes: 1

Related Questions