【问题标题】:untar all .gz in directories and subdirectories解压目录和子目录中的所有 .gz
【发布时间】:2014-07-30 16:56:55
【问题描述】:

对于初学者,我已经检查了大多数可用的解决方案,没有

How to untar all .tar.gz with shell-script?

Unzipping multiple zip files in a directory?

我有一个目录,其中包含带有 .gz 文件的子目录,我想将所有文件解压缩到一个文件夹中,或者将它们保存在各自的文件夹中。 提前谢谢你

【问题讨论】:

  • 您尝试了什么,又是如何失败的?
  • 感谢您帮助我解决了问题

标签: linux terminal tar


【解决方案1】:

问题

您想解压缩一个目录及其所有子目录中的所有个压缩文件。

解决方案

使用 bash 和实用程序 find 将当前目录中所有内容的列表输出到控制台。使用循环结构来解压缩每个文件。

解压当前目录下所有文件:

$ for file in `ls -1`; do
       sudo tar -xvf "${file}" ; done

解压当前目录和任意子目录下的所有档案(我个人最喜欢的):

$ for file in `find *`; do
       sudo tar -xvf "${file}" ; done

递归解压所有档案,并对剩余的档案再次执行相同操作:

# Make the above loop a function to be called more than once 
# In case of compressed files inside compressed files this will 
# run twice the previous snippet.

$ function decompress_all_complete () {
     function decompress_all () {
          for file in `find *`; do
                sudo tar -xvf "${file}" ; done
     } ;
    for i in `echo {1..2}`; do
         decompress_all_complete; done
}

如果你喜欢冒险,你可以使用这个 for 循环的变体 :-)



    $ for program in tar unzip untar ; do      # You could simply add to this list...
         for file in `find *`; do
               sudo `which "${program}"` -xvf "${file}" ; 
         done ;
    done

讨论

实用程序find 列出了您当前目录中的所有内容,而且速度很快。下面的 sn-p 每次将文件解压到一个目录,但说明了以下所有代码的简单逻辑。以上是解决此问题的选项和变体;起初我假设您当前的工作目录包含您要解压缩以使用最简单版本的 sn-p 的所有文件。

这个伪代码试图简单地传达我的解决方案背后的逻辑:

#Use all decompressing programs locally installed :: apropos compress
for --temp_container_1-- in --list_of_programs_to_decompress_files-- ; do

# run each program through every file in the current directory
for --temp_container_2-- in --list_of_files-- ; do

    # use program with x options on y file
    sudo "${temp_container_1}" [TYPE COMMON OPTIONS] "${temp_container_2} ;

# Successfully decompressed some files. Try another program.
done ;

# There is nothing else to do.
done

上下文

我使用 or 测试了这些 sn-ps:

* Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt9-3 (2015-04-23) x86_64 GNU/Linux
* find (GNU findutils) 4.4.2
* GNU bash, version 4.3.33(1)-release (x86_64-pc-linux-gnu)
* tar (GNU tar) 1.27.1


【讨论】:

  • 迄今为止的最佳答案。
  • 很好的答案,“解压缩当前目录和任何子目录中的所有档案”的改进可能是这样的:for file in `find . -name *.tar.gz`; do tar -xvf "${file}" -C "$(dirname "${file}")" ; done 这将仅过滤 .tar.gz 文件,并解压缩每个文件通过抓取正在解压缩的文件的目录 (dirname),将文件复制到自己的目录(-C 命令)。
【解决方案2】:

如果我理解你的问题,你可以使用类似

DEST=<Destination Folder>
SRC=<Src Folder>
find $SRC -name "*.tar.gz" -or -name "*.tgz" -exec tar xzvvf -C $DEST {} \;

【讨论】:

  • 如何对 tar xvzf 做同样的事情? find . -name "*.bz2" | while read filename; do tar -xvzf -C "dirname "$filename"" "$filename"; done; 不起作用
  • z(用于gzip)更改为j(用于bzip2)tar -xvjf
【解决方案3】:

如果您想递归解压到存档所在的同一文件夹中,您可以执行以下操作:

for file in `find . -name '*.tar.gz'`; do \
  DRN_NAME=$(dirname $file)
  sudo tar -xzvf "${file}" -C $DRN_NAME ;
  sudo rm $file
done

for file in `find . -name '*.tgz'`; do \
  DRN_NAME=$(dirname $file)
  sudo tar -xzvf "${file}" -C $DRN_NAME ;
  sudo rm $file
done

适用于:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal

$ tar --version
tar (GNU tar) 1.30

$ find --version
find (GNU findutils) 4.7.0

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

【讨论】:

    【解决方案4】:

    这应该递归提取当前目录和所有子目录中的所有 tar.gz 文件。

    for subdir in `find . -type d`
    do
      for f in $subdir/*.tar.gz
      do
        tar -xzf $f -C <destination>
      done
    done
    

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 2016-11-07
      • 2017-01-06
      • 2011-01-23
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多