简体   繁体   中英

Unix:Recursively unzipping .zip file in respective folder

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.?

Should not be much hard (untested!):

#!/bin/bash

for zip in *.zip ; do
    dir=${zip%.zip}
    mkdir "$dir"
    unzip -rd "$dir" "$zip"
done

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM