unzip
命令没有递归解压缩档案的选项。
如果我有以下目录结构和档案:
/Mother/Loving.zip /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes.zip
我想将所有档案解压缩到与每个档案同名的目录中:
/Mother/Loving/1.txt /Mother/Loving.zip /Scurvy/Sea Dogs/2.txt /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes/3.txt /Scurvy/Cures/Limes.zip
我会发出什么命令或命令?
重要的是,这不会阻塞其中包含空格的文件名。
答案 0 :(得分:86)
如果要将文件解压缩到相应的文件夹,可以试试这个
find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done;
可处理高I / O的系统的多处理版本:
find . -name "*.zip" | xargs -P 5 -I fileName sh -c 'unzip -o -d "$(dirname "fileName")/$(basename -s .zip "fileName")" "fileName"'
答案 1 :(得分:30)
这是一个涉及find命令和while循环的解决方案:
find . -name "*.zip" | while read filename; do unzip -o -d "`basename -s .zip "$filename"`" "$filename"; done;
答案 2 :(得分:15)
正确处理所有文件名(包括换行符)并提取到与文件位于同一位置的目录的解决方案,只需删除扩展名:
find . -name '*.zip' -exec sh -c 'unzip -o -d "${0%.*}" "$0"' '{}' ';'
请注意,您可以通过使用.jar
添加文件来轻松处理更多文件类型(例如-o
),例如:
find . '(' -name '*.zip' -o -name '*.jar' ')' -exec ...
答案 3 :(得分:5)
您可以在单个命令行中使用find和-exec标志来执行作业
find . -name "*.zip" -exec unzip {} \;
答案 4 :(得分:2)
使用-r标志的gunzip之类的东西?....
递归地传播目录结构。如果在命令行中指定的任何文件名是目录,gzip将下降到目录并压缩它在那里找到的所有文件(或者在gunzip的情况下解压缩它们)。
答案 5 :(得分:1)
这完全符合我们的要求:
解压缩文件:
find . -name "*.zip" | xargs -P 5 -I FILENAME sh -c 'unzip -o -d "$(dirname "FILENAME")" "FILENAME"'
上面的命令不会创建重复的目录。
删除所有zip文件:
find . -depth -name '*.zip' -exec rm {} \;
答案 6 :(得分:0)
如果您使用的是cygwin,则basename命令的语法略有不同。
find . -name "*.zip" | while read filename; do unzip -o -d "`basename "$filename" .zip`" "$filename"; done;
答案 7 :(得分:0)
我意识到这已经很老了,但是当我在寻找类似的解决方案时,它是谷歌的第一次点击,所以我会发布我在这里做的。我的情况略有不同,因为我基本上只想完全爆炸一个jar,以及其中包含的所有jar,所以我编写了以下bash函数:
function explode {
local target="$1"
echo "Exploding $target."
if [ -f "$target" ] ; then
explodeFile "$target"
elif [ -d "$target" ] ; then
while [ "$(find "$target" -type f -regextype posix-egrep -iregex ".*\.(zip|jar|ear|war|sar)")" != "" ] ; do
find "$target" -type f -regextype posix-egrep -iregex ".*\.(zip|jar|ear|war|sar)" -exec bash -c 'source "<file-where-this-function-is-stored>" ; explode "{}"' \;
done
else
echo "Could not find $target."
fi
}
function explodeFile {
local target="$1"
echo "Exploding file $target."
mv "$target" "$target.tmp"
unzip -q "$target.tmp" -d "$target"
rm "$target.tmp"
}
请注意,如果您将此文件存储在非交互式shell中未读取的文件中,则需要<file-where-this-function-is-stored>
。如果您将函数存储在非交互式shell上加载的文件中(例如,我相信.bashrc
),您可以删除整个source
语句。希望这会对某人有所帮助。
一点警告 - explodeFile
也会删除ziped文件,你当然可以通过注释掉最后一行来改变它。
答案 8 :(得分:0)
另一个有趣的解决方案是:
DESTINY=[Give the output that you intend]
# Don't forget to change from .ZIP to .zip.
# In my case the files were in .ZIP.
# The echo were for debug purpose.
find . -name "*.ZIP" | while read filename; do
ADDRESS=$filename
#echo "Address: $ADDRESS"
BASENAME=`basename $filename .ZIP`
#echo "Basename: $BASENAME"
unzip -d "$DESTINY$BASENAME" "$ADDRESS";
done;
答案 9 :(得分:0)
unzip name_of_the_zipfile.zip
从Info-ZIP安装zip
软件包后,对我来说工作正常:
sudo apt install -y zip
以上安装是针对Debian / Ubuntu / Mint的。对于其他Linux发行版, 请参阅下面的第二参考。
参考文献:
http://infozip.sourceforge.net/
https://www.tecmint.com/install-zip-and-unzip-in-linux/
答案 10 :(得分:-1)
这对我有用
def unzip(zip_file, path_to_extract):
"""
Decompress zip archives recursively
Args:
zip_file: name of zip archive
path_to_extract: folder where the files will be extracted
"""
try:
if is_zipfile(zip_file):
parent_file = ZipFile(zip_file)
parent_file.extractall(path_to_extract)
for file_inside in parent_file.namelist():
if is_zipfile(os.path.join(os.getcwd(),file_inside)):
unzip(file_inside,path_to_extract)
os.remove(f"{zip_file}")
except Exception as e:
print(e)