bash 如何使用单个命令将目录中的每个文件解压缩到与文件同名的新目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6220060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a single command to unzip every file in a directory, into a new unique directory with the same name as the file
提问by bob
I have a directory full of zip files. Each called something like 'files1.zip'. My instinct is to use a bash for loop to unzip each file.
我有一个装满 zip 文件的目录。每个都称为“files1.zip”之类的东西。我的直觉是使用 bash for 循环来解压缩每个文件。
Trouble is, many of the files will unzip their contents straight into the parent directory, rather then unfolding everything into their own unique directory. So, I get file soup.
问题是,许多文件会将它们的内容直接解压缩到父目录中,而不是将所有内容展开到它们自己的唯一目录中。所以,我得到了文件汤。
I'd like to ensure that 'files1.zip' pours all of it's files into a dir called 'files1', and so on.
我想确保“files1.zip”将所有文件都倒入名为“files1”的目录中,依此类推。
As an added complication, some of the filenames have spaces.
作为一个额外的复杂因素,一些文件名有空格。
How can I do this?
我怎样才能做到这一点?
Thanks.
谢谢。
回答by Roland Illig
for f in *.zip; do
dir=${f%.zip}
unzip -d "./$dir" "./$f"
done
回答by user3019558
Simple one liner
简单的一个班轮
$ for file in `ls *.zip`; do unzip $file -d `echo $file | cut -d . -f 1`; done
回答by Kim Stebel
you can use -d to unzip to a different directory.
您可以使用 -d 解压缩到不同的目录。
for file in `echo *.zip`; do
[[ $file =~ ^(.*)\.zip$ ]]
unzip -d ${BASH_REMATCH[1]} $file
done

