我正在使用选项 --keep-old-files 解压缩 tar 文件,这样我就不会破坏已经存在的文件。令人讨厌的是,当文件已经存在时,这会导致 tar 产生错误代码 #2(致命错误)。
对我来说这不应该被视为错误的原因很重要,因为我的脚本会在文件成功解包时记录日志,然后使用此日志来不重新解压缩该 tar 文件。
我可以完全忽略这个错误,而不是记录,并相信不会覆盖任何文件,但这有两个问题:它会生成很多错误消息,而且文件很多,所以我打开 tar 一次效率很低对于每个文件。
有什么办法可以使用--keep-old-files,忽略现有文件生成的错误,而不忽略其他错误?
这显然是设计使然-来自文档:
要更加谨慎并防止替换现有文件,请使用
--keep-old-files
(-k
) 选项。它会导致 tar 拒绝替换或更新已经存在的文件,即与存档成员同名的文件会阻止提取该存档成员。相反,它会报告错误。
git 存储库确实包含一个引入参数的补丁--skip-old-files
,因此可以在未来的 GNUtar
版本中解决这个问题:
Run Code Online (Sandbox Code Playgroud)+A new option --skip-old-files is introduced, which acts exactly as +--keep-old-files, except that it does not treat existing files as +errors. Instead it just silently skips them. An additional level of +verbosity can be obtained by using the option --warning=existing-file +together with this option.
不过,该补丁尚未正式发布。