今天在运行 tar 命令时,我试图排除文件系统内的文件目录。我运行了这个命令 -
tar --exclude='./my/directory/here' -zcvf backup.tar.gz docroot/
但是,当它对目录进行 tar 处理时,它仍然包含来自“此处”的文件。我怎样才能让它正确排除文件?
我的工作不正常 - 看演示 -
$ 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/
最佳答案
您的命名需要保持一致!
tar --exclude='./my/directory/here' -zcvf backup.tar.gz docroot/
...应该是这样的:
tar --exclude='docroot/my/directory/here' -zcvf backup.tar.gz docroot/
...也就是说:如果您不在要包含在压缩包中的文件名中使用前导 ./
,则不能使用前导 ./
在要排除的文件的名称中。 (反之亦然:如果您确实在文件名中使用了./docroot
,那么您也需要在排除模式中使用它)。
观察:
$ 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
值得注意的是,虽然 my/directory
存在,但 my/directory/here
不存在,my/directory/here/content
也不存在>.
关于linux - tar 命令不能正确排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33090203/