【问题标题】:How to find all tar files in various sub-folders, then extract them in the same folder they were found?如何在各个子文件夹中找到所有 tar 文件,然后将它们解压缩到找到的同一文件夹中?
【发布时间】:2015-05-30 12:18:33
【问题描述】:

我有很多子文件夹,只有一些包含 tar 文件。即:

folder1/
folder2/this-is-a.tar
folder3/
folder4/this-is-another.tar

我可以通过简单地执行ls */*.tar 来找到哪些目录有 tar。

我想要实现的是以某种方式找到所有 .tar 文件,然后将它们解压缩到找到的同一目录中,然后删除 .tars。

我尝试过ls */*.tar | xargs -n1 tar xvf,但这会将焦油提取到我所在的目录中,而不是在找到焦油的目录中。

任何帮助将不胜感激。

【问题讨论】:

    标签: linux bash automation centos tar


    【解决方案1】:
    for file in */*.tar; do
        (cd `dirname $file`; tar xvf `basename $file`)
        unlink $file
    done
    

    【讨论】:

      【解决方案2】:
      for i in */*.tar ; do pushd `dirname $i` ; tar xf `basename $i` && rm `basename $i` ; popd ; done
      

      编辑:这可能是更好的方法:

      find . -type f -iname "*.tar" -print0 -execdir tar xf {} \; -delete
      

      【讨论】:

      • 发挥了作用。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2015-09-29
      • 2011-08-04
      • 1970-01-01
      • 2015-10-03
      • 2013-05-18
      • 2021-12-20
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多