gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:40 26 4
gpt4 key购买 nike

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

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

Unzipping multiple zip files in a directory?

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

最佳答案

问题

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

解决方案

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

The utility find lists everything from your present directory, and is fast. The snippet below will decompress the files one directory at a time each time but illustrates the simple logic of all the following code. Above are options and variations that do solve this problem; I was assuming at first that your present working directory contains all the files that you want to decompress to use the simplest version of the snippet.

This pseudo code tries to communicate the logic behind my solution briefly:

#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 测试了这些片段:

* 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


关于linux - 解压目录和子目录中的所有 .gz,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com