【问题标题】:tar only the file in the directory仅 tar 目录中的文件
【发布时间】:2013-03-17 06:42:32
【问题描述】:

如何仅压缩目录中存在的文件。我曾尝试将tar -cvf /tmp/test.tar /tmp/mylog.doc 放在主目录中,但是当我解压它时,内容也包括tmpdirectory。我只需要文件mylog.doc 出现在tar 文件中。我看到类似的查询 Tarring only the files of a directory。但无法从中获得太多帮助。

【问题讨论】:

    标签: linux archive tar


    【解决方案1】:
    cd sourcedir ; tar cvf archive.tar ./file_to_tar
    

    因为使用“/path/to/file”或“./path/to/file”:将在生成的 tar 文件中添加“path/to/”(除非特定(也可能是不可移植的)选项告诉否则焦油)

    注意:一些不太老的生产系统在提取时仍然无法摆脱前导的“/”。

    在那些“不太老”的系统上:

    tar cvf /tmp/hosts-backup.tar /etc/hosts
    cd /secure/location
    tar xvf /tmp/hosts-backup.tar
    

    覆盖文件 /etc/hosts,而不是在 /secure/location 下输出“./etc/hosts”文件! (就像大多数“最近”的 tar 现在所做的那样:他们现在都默默地忽略尾随的“/”,使路径相对而不是绝对,这是 99.9% 的人想要的)

    我最近被这个烧毁了:想要提取一个日志,而那个日志最终覆盖了原始位置(因此覆盖了那里更新的日志文件)。在系统上第一次使用 tar 之前,请务必检查这一点。 (有一些解决方法,但不太简单:chroot 和其他命令,例如“sar”)

    【讨论】:

      【解决方案2】:

      使用-C 命令行参数来指示您要工作的目录。然后,您可以使用您包含的相对于该目录的文件名。

      tar -cvf /tmp/test.tar -C /tmp mylog.doc
      

      请注意,这不适用于 tar 文件本身。

      【讨论】:

      • 它没有用。 tar: Cowardly refusing to create an empty archiveTry tar --help or tar --usage for more information
      • tar 只是说它在目录/tmp 中找不到文件mylog.doc。文件真的存在吗?
      猜你喜欢
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多