简体   繁体   English

tar命令未正确排除文件

[英]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. 今天在运行tar命令时,我试图排除文件系统内部文件的目录。 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". 但是,当它对目录进行压缩时,它仍然包含来自“ 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/

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). (相反的情况也适用:如果确实在文件名中使用./docroot ,则也需要在排除模式中使用它)。


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 . 值得注意的是,虽然my/directory存在,但my/directory/here不存在, my/directory/here/content也不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM