在目录和子目录中解压所有的 .gz 文件

6

1
你尝试了什么,它是如何失败的? - that other guy
谢谢你的帮助,我已经解决了这个问题。 - user3862515
4个回答

9

问题

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

解决方案

使用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会列出您当前目录中的所有内容,速度很快。下面的代码段将一次一个目录地解压缩文件,但说明了以下所有代码的简单逻辑。上面的选项和变体确实解决了这个问题;起初我假设您的当前工作目录包含您想要解压缩的所有文件,以使用代码片段的最简版本。

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

#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

背景

我使用以下环境测试了这些代码片段:

* 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



迄今为止最佳答案。 - Hashim Aziz
1
很好的答案。对于“解压当前目录及其子目录中的所有归档文件”的改进,可以这样写:for file in `find . -name *.tar.gz`; do tar -xvf "${file}" -C "$(dirname "${file}")" ; done这将仅过滤.tar.gz文件,并通过获取正在解压缩的文件的目录(dirname)将每个文件解压到其自己的目录中(-C命令)。 - jasonco

4
如果我理解您的问题,您可以使用类似以下的东西:
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; 无法正常工作。 - Louis
1
z(代表gzip)更改为j(代表bzip2)tar -xvjf - Elliott Frisch

2

如果您想将tar包递归解压缩到与该档案相同的文件夹中,可以执行以下操作:

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)

这对我有用,谢谢你。 - Sahib Yar

-1

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接