DEV Community

abbazs
abbazs

Posted on

How to unzip all the zip files in linux?

Here is the code that can unzip all the zip files in linux

extract_zips() {
    shopt -s globstar  # Enable recursive globbing
    for file in **/*.zip; do
        folder="${file%.*}"
        mkdir -p "$folder"
        unzip "$file" -d "$folder"
        echo "Done $folder"
    done
}

alias ez="extract_zips"
Enter fullscreen mode Exit fullscreen mode

Add this code to the .bashrc file and open a new terminal to get ez alias working.

Explanation

  • The line shopt -s globstar enables the globstar option, which allows the use of ** to match files and directories recursively.
  • The for loop uses the pattern **/*.zip to match zip files in the current directory and all its subdirectories.
  • In each iteration, the base name of the zip file (without the .zip extension) is extracted using ${file%.*} and stored in the folder variable.
  • The mkdir -p "$folder" command creates a directory with the name stored in folder, ensuring that parent directories are created if necessary.
  • The unzip "$file" -d "$folder" command extracts the contents of the zip file into the directory specified by folder.
  • The echo "Done $folder" command prints a message indicating the completion of the extraction for the current zip file.
  • The loop continues until all zip files have been processed.
  • The alias ez remains unchanged and can be used to invoke the extract_zips function.

Top comments (0)