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

前端 未结 10 491
旧巷少年郎
旧巷少年郎 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:17

    If you want to extract the files to the respective folder you can try this

    find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done;
    

    A multi-processed version for systems that can handle high I/O:

    find . -name "*.zip" | xargs -P 5 -I fileName sh -c 'unzip -o -d "$(dirname "fileName")/$(basename -s .zip "fileName")" "fileName"'
    
    0 讨论(0)
  • 2020-12-04 07:19

    Here's one solution that extracts all zip files to the working directory and involves the find command and a while loop:

    find . -name "*.zip" | while read filename; do unzip -o -d "`basename -s .zip "$filename"`" "$filename"; done;
    
    0 讨论(0)
  • 2020-12-04 07:30
    unzip name_of_the_zipfile.zip
    

    worked fine for me, after installing the zip package from Info-ZIP:

    sudo apt install -y zip
    

    The above installation is for Debian/Ubuntu/Mint. For other Linux distros, see the second reference below.

    References:
    http://infozip.sourceforge.net/
    https://www.tecmint.com/install-zip-and-unzip-in-linux/

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

    You could use find along with the -exec flag in a single command line to do the job

    find . -name "*.zip" -exec unzip {} \;
    
    0 讨论(0)
  • 2020-12-04 07:32

    This works perfectly as we want:

    Unzip files:

    find . -name "*.zip" | xargs -P 5 -I FILENAME sh -c 'unzip -o -d "$(dirname "FILENAME")" "FILENAME"'
    

    Above command does not create duplicate directories.

    Remove all zip files:

    find . -depth -name '*.zip' -exec rm {} \;
    
    0 讨论(0)
  • 2020-12-04 07:34

    A solution that correctly handles all file names (including newlines) and extracts into a directory that is at the same location as the file, just with the extension removed:

    find . -iname '*.zip' -exec sh -c 'unzip -o -d "${0%.*}" "$0"' '{}' ';'
    

    Note that you can easily make it handle more file types (such as .jar) by adding them using -o, e.g.:

    find . '(' -iname '*.zip' -o -iname '*.jar' ')' -exec ...
    
    0 讨论(0)
提交回复
热议问题