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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay