【问题标题】:Is there a way to specifically un-tar files by themselves in Unix?有没有办法在 Unix 中自己专门解压缩文件?
【发布时间】:2014-07-19 02:46:58
【问题描述】:

我正在编写一个脚本,该脚本用于在 tar 中搜索字符串并将包含这些字符串的文件输出到文件夹 - 这样做是为了我不必解压一个大文件然后再搜索它删除不需要的文件

这是我目前所拥有的:

#!/bin/bash

tarname=$1
pattern=$2

count=1
tar -tf  $tarname | while read -r FILE
do

tar -xf  $tarname $FILE

##grep the pattern with the file here...

done

所以我遇到的问题是它假设 tar 文件中的 tar 文件都是文件,但它们也可以很容易地成为目录——有没有办法用这段代码递归地 tar 文件?

顺便说一句,这都是在 Unix 中,所以有些东西可能已经过时并且可能无法访问更新版本的命令

【问题讨论】:

    标签: bash unix scripting solaris tar


    【解决方案1】:

    是的,您可以使用以下命令单独提取文件或目录:

    tar xvf archive_file.tar /path/to/file tar xvf archive_file.tar /path/to/dir/

    从这里: http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/

    【讨论】:

      【解决方案2】:

      您可以通过假设所有目录末尾都有“/”来跳过提取目录,例如:

      if [ $(echo $FILE|grep -c /$) == "1" ]; then
      
      echo "skipping directory"
      
      else 
      
      tar -xf $tarname $FILE
      
      fi
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-08
        • 1970-01-01
        • 2020-09-14
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多