linux - 获取 tar 存档中特定文件的内容

标签 linux bash shell tar

此脚本列出包含模式的文件(在 tar 存档中)的名称。

tar tf myarchive.tar | while read -r FILE
do
    if tar xf test.tar $FILE  -O | grep "pattern" ;then
        echo "found pattern in : $FILE"
    fi
done

我的问题是:

此功能在哪里记录,其中 $FILE 是存档中的文件之一:

tar xf test.tar $FILE

最佳答案

这通常记录在手册页中,尝试运行此命令:

man tar

不幸的是,Linux 没有最好的手册页集。有来自此操作系统的 tar 联机帮助页的联机副本:http://linux.die.net/man/1/tar这太可怕了。但它链接到 info man 命令,该命令用于访问 GNU 世界中广泛使用的“信息”系统(linux 用户空间中的许多程序都来自 GNU 项目,例如 gcc)。关于提取特定文件的在线 info tar 部分有一个确切的链接:http://www.gnu.org/software/tar/manual/html_node/extracting-files.html#SEC27

我可能还会推荐来自 BSD(例如 FreeBSD)或 opengroup.org 的文档。实用程序在细节上可能有所不同,但总体上表现相同。

例如,来自 opengroup(XCU 的意思是 Single UNIX Specification,Version 2, 1997 的“Commands and Utilities”)的一位相当年长但很好的人: http://pubs.opengroup.org/onlinepubs/7908799/xcu/tar.html

tar key [file...]

The following operands are supported:

key -- The key operand consists of a function letter followed immediately by zero or more modifying letters. The function letter is one of the following:

x -- Extract the named file or files from the archive. If a named file matches a directory whose contents had been written onto the archive, this directory is (recursively) extracted. If a named file in the archive does not exist on the system, the file is created with the same mode as the one in the archive, except that the set-user-ID and set-group-ID modes are not set unless the user has appropriate privileges. If the files exist, their modes are not changed except as described above. The owner, group, and modification time are restored (if possible). If no file operand is given, the entire content of the archive is extracted. Note that if several files with the same name are in the archive, the last one overwrites all earlier ones.

要完全理解命令 tar xf test.tar $FILE,您还应该阅读 f 选项:

f -- Use the first file operand (or the second, if b has already been specified) as the name of the archive instead of the system-dependent default.

因此,您命令中的test.tar 将被f 键用作存档名称;然后 x 将使用第二个参数 ($FILE) 作为要从存档中提取的文件或目录的名称。

关于linux - 获取 tar 存档中特定文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13041345/

相关文章:

arrays - 神秘 IFS 行为

shell - 更改文件名后缀(使用 sed ?)

python - 在大文本文件中查找重复记录

c - 如何从汇编代码中调用 C 函数 printf 获取整数

linux - 如何通过 linux shell 检查 url 是否正常工作

linux - Electron 上的自定义 Linux 桌面环境

linux - 如何只对每个地址中的一个进行 grep。 Linux

linux - sed 字符串大小写与 "\<>"的匹配问题?

bash - 如何使用bash命令输出没有冒号的日期

shell - 如何将文本与扩展变量组合成 Fish 中的变量扩展