首先,我已经检查了大部分可用的解决方案
我有一个带有.gz文件子目录的目录,并想将它们全部提取到一个文件夹中或者只是保留在它们的文件夹中。 谢谢您的帮助。
我有一个带有.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 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
for file in `find . -name *.tar.gz`; do tar -xvf "${file}" -C "$(dirname "${file}")" ; done
这将仅过滤.tar.gz文件,并通过获取正在解压缩的文件的目录(dirname
)将每个文件解压到其自己的目录中(-C
命令)。 - jasoncoDEST=<Destination Folder>
SRC=<Src Folder>
find $SRC -name "*.tar.gz" -or -name "*.tgz" -exec tar xzvvf -C $DEST {} \;
find . -name "*.bz2" | while read filename; do tar -xvzf -C "
dirname "$filename"" "$filename"; done;
无法正常工作。 - Louisz
(代表gzip)更改为j
(代表bzip2)tar -xvjf
。 - Elliott Frisch如果您想将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)
这应该递归地提取当前目录及所有子目录中的所有tar.gz文件。
for subdir in `find . -type d`
do
for f in $subdir/*.tar.gz
do
tar -xzf $f -C <destination>
done
done