问题
I have a number of zipped files say a.zip b.zip
etc in a folder. I would like to unzip those and put into a respective directory like a,b
.Can you suggest me some unix script for it.?
回答1:
Should not be much hard (untested!):
#!/bin/bash
for zip in *.zip ; do
dir=${zip%.zip}
mkdir "$dir"
unzip -rd "$dir" "$zip"
done
回答2:
You can use unzip utility in unix, as follows:
#!/bin/bash
for f in *.zip
do
echo "unzipping $f"
unzip $f -d ${f%.*}
done
Running this script in directory will unzip all the zip files in it as you wanted, say a.zip, b.zip will be unzipped to directories a and b respectively.
回答3:
This previous post has helped me to achieve this same function; I even created a script to help me remember on my computer:
$ ls *.zip|awk -F'.zip' '{print "unzip "$0" -d "$1}'|sh
Similarly, you can create an alias
to execute a bash
function:
$ alias munzip='for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done'
and for a dry-run, to test it beforehand:
$ alias testmunzip='for f in *.zip; do echo unzip -d "${f%*.zip}" "$f"; done'
Just thought it might help to keep this related info on one page, in case someone else is looking for the same effects.
来源:https://stackoverflow.com/questions/11628538/unixrecursively-unzipping-zip-file-in-respective-folder