使用 tar 命令时如何防止覆盖现有文件

Roh*_*ibe 7 linux bash tar

tar我犯了一个错误,在使用如下命令时忘记分配文件名参数:

\n\n
[john@foobar foo]$ ll\ntotal 0\n-rw-rw-r-- 1 john john 0  7\xe6\x9c\x88  4 19:20 2018 file1\n-rw-rw-r-- 1 john john 0  7\xe6\x9c\x88  4 19:20 2018 file2\n-rw-rw-r-- 1 john john 0  7\xe6\x9c\x88  4 19:20 2018 file3\n[john@foobar foo]$ tar -cvzf file1 file2 file3\nfile2\nfile3\n[john@foobar foo]$ ll\ntotal 4\n-rw-rw-r-- 1 john john 130  7\xe6\x9c\x88  4 19:21 2018 file1\n-rw-rw-r-- 1 john john   0  7\xe6\x9c\x88  4 19:20 2018 file2\n-rw-rw-r-- 1 john john   0  7\xe6\x9c\x88  4 19:20 2018 file3\n
Run Code Online (Sandbox Code Playgroud)\n\n

当忘记指定存档文件名时,tar将覆盖并创建存档file1

\n\n

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

\n\n

创建一个万无一失的脚本是一种可能的方法吗?

\n

Pos*_*shi 21

man tar

\n\n
-k, --keep-old-files\n       don\xe2\x80\x99t replace existing files when extracting, treat them as errors\n\n--skip-old-files\n       don\xe2\x80\x99t replace existing files when extracting, silently skip over them\n
Run Code Online (Sandbox Code Playgroud)\n

  • 该选项用于提取。创建存档时,此选项不起作用。 (4认同)
  • 哎哟! 抱歉,我知道您在谈论提取文件。在这种情况下,您应该事先检查:/sf/ask/1517642101/ (3认同)