linux - tar 命令不能正确排除文件

标签 linux unix

今天在运行 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/

相关文章:

mysql - 如何自动终止 MySQL 中的休眠进程?

node.js - 在 443 端口上使用 SSL 运行 Node 应用程序(在 80 上工作)

bash - 使用 scp 将文件从一台服务器复制到另一台服务器时发送密码

shell - 执行 KornShell 脚本

unix - 在安装时使用 CMake 创建指向库的自定义符号链接(symbolic link)

linux - 什么时候执行脚本?

linux - 用于本地(非远程)命令执行的 ssh 隧道

linux - Linux 上的 Docker 桌面使用大量内存

linux - perf stat 周期数和频率缩放

c++ - Boost::this_thread::get_id() 无字符串操作