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"
Add this code to the .bashrc
file and open a new terminal to get ez
alias working.
Explanation
- The line
shopt -s globstar
enables theglobstar
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 itssubdirectories
. - 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 theextract_zips
function.
Top comments (0)