How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

前端 未结 10 492
旧巷少年郎
旧巷少年郎 2020-12-04 07:15

The unzip command doesn\'t have an option for recursively unzipping archives.

If I have the following directory structure and archives:

/Moth         


        
相关标签:
10条回答
  • 2020-12-04 07:35

    I realise this is very old, but it was among the first hits on Google when I was looking for a solution to something similar, so I'll post what I did here. My scenario is slightly different as I basically just wanted to fully explode a jar, along with all jars contained within it, so I wrote the following bash functions:

    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"
    }
    

    Note the <file-where-this-function-is-stored> which is needed if you're storing this in a file that is not read for a non-interactive shell as I happened to be. If you're storing the functions in a file loaded on non-interactive shells (e.g., .bashrc I believe) you can drop the whole source statement. Hopefully this will help someone.

    A little warning - explodeFile also deletes the ziped file, you can of course change that by commenting out the last line.

    0 讨论(0)
  • 2020-12-04 07:36

    Another interesting solution would be:

    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;
    
    0 讨论(0)
  • 2020-12-04 07:37

    Something like gunzip using the -r flag?....

    Travel the directory structure recursively. If any of the file names specified on the command line are directories, gzip will descend into the directory and compress all the files it finds there (or decompress them in the case of gunzip ).

    http://www.computerhope.com/unix/gzip.htm

    0 讨论(0)
  • 2020-12-04 07:40

    If you're using cygwin, the syntax is slightly different for the basename command.

    find . -name "*.zip" | while read filename; do unzip -o -d "`basename "$filename" .zip`" "$filename"; done;
    
    0 讨论(0)
提交回复
热议问题