This question already has answers here:
How do I prevent tar from overwriting an existing archive?

(2个答案)


2年前关闭。




我犯了一个错误,忘记了在使用tar命令时忘记分配文件名的参数,如下所示:
[john@foobar foo]$ ll
total 0
-rw-rw-r-- 1 john john 0  7月  4 19:20 2018 file1
-rw-rw-r-- 1 john john 0  7月  4 19:20 2018 file2
-rw-rw-r-- 1 john john 0  7月  4 19:20 2018 file3
[john@foobar foo]$ tar -cvzf file1 file2 file3
file2
file3
[john@foobar foo]$ ll
total 4
-rw-rw-r-- 1 john john 130  7月  4 19:21 2018 file1
-rw-rw-r-- 1 john john   0  7月  4 19:20 2018 file2
-rw-rw-r-- 1 john john   0  7月  4 19:20 2018 file3

当忘记分配存档文件名时,tar会覆盖并创建存档file1

我检查了man tar,但是似乎没有选项,例如cp在同名文件已经存在时显示提示。

创建万无一失的脚本是可行的方法吗?

最佳答案

man tar:

-k, --keep-old-files
       don’t replace existing files when extracting, treat them as errors

--skip-old-files
       don’t replace existing files when extracting, silently skip over them

关于linux - 使用tar命令时如何防止覆盖现有文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51171600/

10-16 06:15