我正在尝试创建一个档案,例如
archive.tar.gz
在当前工作目录中,例如/builds/project/
不保存 archive.tar.gz
内的 archive.tar.gz
。
为了防止这种情况,我尝试使用 --exclude=PATTERN 选项。
这应该是一项简单的任务。但事实并非如此。
最后,我想以设置的变量命名存档并将其排除。
版本:
$ bash --version
GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ tar --version
tar (GNU tar) 1.34
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by John Gilmore and Jay Fenlason.
我尝试了多个版本,带引号,不带引号,在不同位置排除,带有 ./ 和不带引号的模式,但没有任何效果:
$ tar -czf --exclude=archive1.tar.gz archive1.tar.gz . || true
tar: archive1.tar.gz: Cannot stat: No such file or directory
tar: .: file changed as we read it
tar: Exiting with failure status due to previous errors
$ tar -czf --exclude="archive2.tar.gz" archive2.tar.gz . || true
tar: archive2.tar.gz: Cannot stat: No such file or directory
tar: .: file changed as we read it
tar: Exiting with failure status due to previous errors
$ tar -czf --exclude=./archive3.tar.gz archive3.tar.gz . || true
tar: archive3.tar.gz: Cannot stat: No such file or directory
tar (child): --exclude=./archive3.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: --exclude=./archive3.tar.gz: Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
$ tar -czf --exclude="./archive4.tar.gz" archive4.tar.gz . || true
tar: archive4.tar.gz: Cannot stat: No such file or directory
tar (child): --exclude=./archive4.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: --exclude=./archive4.tar.gz: Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
$ tar -czf archive5.tar.gz --exclude=archive5.tar.gz . || true
tar: .: file changed as we read it
$ tar -czf archive6.tar.gz --exclude="archive6.tar.gz" . || true
tar: .: file changed as we read it
$ tar -czf archive7.tar.gz --exclude=./archive7.tar.gz . || true
tar: .: file changed as we read it
$ tar -czf archive8.tar.gz --exclude="./archive8.tar.gz" . || true
tar: .: file changed as we read it
$ TAR_FILE=archive9.tar.gz ; tar -czf --exclude=$TAR_FILE $TAR_FILE . || true
tar: archive9.tar.gz: Cannot stat: No such file or directory
tar: .: file changed as we read it
tar: Exiting with failure status due to previous errors
$ TAR_FILE=archive10.tar.gz ; tar -czf --exclude="$TAR_FILE" $TAR_FILE . || true
tar: archive10.tar.gz: Cannot stat: No such file or directory
tar: .: file changed as we read it
tar: Exiting with failure status due to previous errors
$ TAR_FILE=archive11.tar.gz ; tar -czf --exclude=./$TAR_FILE $TAR_FILE . || true
tar: archive11.tar.gz: Cannot stat: No such file or directory
tar (child): --exclude=./archive11.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: --exclude=./archive11.tar.gz: Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
$ TAR_FILE=archive12.tar.gz ; tar -czf --exclude="./$TAR_FILE" $TAR_FILE . || true
tar: archive12.tar.gz: Cannot stat: No such file or directory
tar (child): --exclude=./archive12.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: --exclude=./archive12.tar.gz: Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
$ TAR_FILE=archive13.tar.gz ; tar -czf $TAR_FILE --exclude=$TAR_FILE . || true
tar: .: file changed as we read it
$ TAR_FILE=archive14.tar.gz ; tar -czf $TAR_FILE --exclude="$TAR_FILE" . || true
tar: .: file changed as we read it
$ TAR_FILE=archive15.tar.gz ; tar -czf $TAR_FILE --exclude=./$TAR_FILE . || true
tar: .: file changed as we read it
$ TAR_FILE=archive16.tar.gz ; tar -czf $TAR_FILE --exclude="./$TAR_FILE" . || true
tar: .: file changed as we read it
非常简单。之前创建文件或连续执行命令2次:
# cd /tmp/dir
# touch 1.tar.gz
# tar -czf 1.tar.gz --exclude=1.tar.gz .
# ls -l
1
1.tar.gz
2
3
# tar -tf 1.tar.gz
./
./1
./2
./3
或
# rm 1.tar.gz
# tar -czf 1.tar.gz --exclude=1.tar.gz .
tar: .: file changed as we read it
# tar -czf 1.tar.gz --exclude=1.tar.gz .
# tar -tf 1.tar.gz
./
./1
./2
./3