我在bash脚本中有一条非常简单的代码行,它可以成功执行(即生成_data.tar
文件),但它不会排除通过--exclude
选项被告知为排除的子目录:
/bin/tar -cf /home/_data.tar --exclude='/data/sub1/*' --exclude='/data/sub2/*' --exclude='/data/sub3/*' --exclude='/data/sub4/*' --exclude='/data/sub5/*' /data
相反,它将生成一个_data.tar
文件,其中包含/ data下的所有内容,包括我要排除的子目录中的文件。
知道为什么吗?以及如何解决这个问题?
更新我根据下面第一个答案中提供的链接实现了我的观察结果(顶层dir首先,最后一个排除后没有空格):
/bin/tar -cf /home/_data.tar /data --exclude='/data/sub1/*' --exclude='/data/sub2/*' --exclude='/data/sub3/*' --exclude='/data/sub4/*' --exclude='/data/sub5/*'
但这没有帮助。所有“排除的”子目录都存在于结果_data.tar
文件中。
这令人费解。无论这是当前tar中的错误(CentOS 6.2,Linux 2.6.32上的GNU tar 1.23)还是tar对空白和其他易于忽略的错别字的“极端敏感性”,我都认为这是一个错误。目前。
这太可怕了:我尝试了以下建议的见解(无尾随/*
),但在生产脚本中仍然不起作用:
/bin/tar -cf /home/_data.tar /data --exclude='/data/sub1' --exclude='/data/sub2' --exclude='/data/sub3' --exclude='/data/sub4'
除了引号和2个空格(而不是1),我看不到我尝试过的内容与@Richard Perrin所做的任何区别。我将尝试这种方式(必须等待每晚运行的脚本作为要备份的目录)数量庞大)并进行报告。
/bin/tar -cf /home/_data.tar /data --exclude=/data/sub1 --exclude=/data/sub2 --exclude=/data/sub3 --exclude=/data/sub4
我开始认为所有这些tar --exclude
敏感度不是焦油而是环境中的某种东西,但是那又会是什么呢?
有效!最后一个变体尝试了工作(没有单引号和单空格,而不是--exclude
s之间的双倍空格)。很奇怪,但是接受。
逆天!事实证明,tar
仅当顶级目录位于命令行的最后时,才会排除(1.15.1)的较早版本。这与1.23版的要求完全相反。供参考。
如果要排除整个目录,则模式应匹配该目录,而不是其中的文件。使用--exclude=/data/sub1
代替--exclude='/data/sub1/*'
引用这些模式时要小心,以防止它们受到外壳扩展的影响。
参见以下示例,在最终调用中遇到了麻烦:
$ for i in 0 1 2; do mkdir -p /tmp/data/sub$i; echo foo > /tmp/data/sub$i/foo; done
$ find /tmp/data
/tmp/data
/tmp/data/sub2
/tmp/data/sub2/foo
/tmp/data/sub0
/tmp/data/sub0/foo
/tmp/data/sub1
/tmp/data/sub1/foo
$ tar -zvcf /tmp/_data.tar /tmp/data --exclude='/tmp/data/sub[1-2]'
tar: Removing leading `/' from member names
/tmp/data/
/tmp/data/sub0/
/tmp/data/sub0/foo
$ tar -zvcf /tmp/_data.tar /tmp/data --exclude=/tmp/data/sub[1-2]
tar: Removing leading `/' from member names
/tmp/data/
/tmp/data/sub0/
/tmp/data/sub0/foo
$ echo tar -zvcf /tmp/_data.tar /tmp/data --exclude=/tmp/data/sub[1-2]
tar -zvcf /tmp/_data.tar /tmp/data --exclude=/tmp/data/sub[1-2]
$ tar -zvcf /tmp/_data.tar /tmp/data --exclude /tmp/data/sub[1-2]
tar: Removing leading `/' from member names
/tmp/data/
/tmp/data/sub2/
/tmp/data/sub2/foo
/tmp/data/sub0/
/tmp/data/sub0/foo
/tmp/data/sub2/
tar: Removing leading `/' from hard link targets
/tmp/data/sub2/foo
$ echo tar -zvcf /tmp/_data.tar /tmp/data --exclude /tmp/data/sub[1-2]
tar -zvcf /tmp/_data.tar /tmp/data --exclude /tmp/data/sub1 /tmp/data/sub2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句