解压缩目录和子目录中的所有.gz

问题描述 投票:1回答:3

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

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

Unzipping multiple zip files in a directory?

我有一个目录,其中包含.gz文件的子目录,并希望将所有文件提取到一个文件夹中或只是将它们保存在其文件夹中。先感谢您

linux terminal tar
3个回答
3
投票

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

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

2
投票

Problem

您希望解压缩目录及其所有子目录中的所有压缩文件。

Solution

使用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

Discussion

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

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

#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

Context

我使用或在以下方面测试了这些片段:

* 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


-1
投票

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

for subdir in `find . -type d`
do
  for f in $subdir/*.tar.gz
  do
    tar -xzf $f -C <destination>
  done
done
© www.soinside.com 2019 - 2024. All rights reserved.