Unzip All Files in a Directory

Unzip All Files In A Directory

This works in bash, according to this link:

unzip \*.zip

How to unzip all files in directory using shell script?

Install unzip:

sudo apt install unzip or yum install unzip

Use this in the same directory you want to unzip the files:

unzip ‘*.zip’

If you want to put the uncompressed files in other directory, then use this:

unzip ‘*.zip’ -d /usr/sampleZip/ExampleDir

To put it into a shell script:

vim shellscript.sh

Then the script could be something like:

#!/bin/bash

unzip ‘*.zip’

After saving the script, to execute it:

./shellscript.sh

Recursively unzip all subdirectories while retaining file structure

You could try this:

#!/bin/bash

while :; do
mapfile -td '' archives \
< <(find . -type f -name '*.zip' -o -name '*.7z' -print0)

[[ ${#archives[@]} -eq 0 ]] && break

for i in "${archives[@]}"; do
case $i in
*.zip) unzip -d "$(dirname "$i")" -- "$i";;
*.7z) 7z x "-o$(dirname "$i")" -- "$i";;
esac
done

rm -rf "${archives[@]}" || break
done
  • Every archive is listed by find. That list is extracted in the correct location and the archives removed. This repeats, until zero archives are found.

  • You can add an equivalent unrar command (I'm not familiar with it).

  • Add -o -name '*.rar' to find, and another case to case. If there's no option to specify a target directory with unrar, you could use cd "$(dirname "$i")" && unrar "$i".

  • There are some issues with this script. In particular, if extraction fails, the archive is still removed. Otherwise it would cause an infinite loop. You can use unzip ... || exit 1 to exit if extraction fails, and deal with that manually.

  • It's possible to both avoid removal and also an infinite loop, by counting files which aren't removed, but hopefully not necessary.

  • I couldn't test this properly. YMMV.

How to extract all zip files in subfolders in same folder using powershell

I found the solution in case someone else has same issue

Get-ChildItem '.' -R -Filter *.zip | ForEach-Object {
Expand-Archive $_.FullName "$($_.DirectoryName)/$($_.Basename)" -Force
Remove-Item $_.FullName
}

From batch file, I Need to recurse directories (and sub directories) and unzip every zip file found into their current directory w/delete of archive


... do (
... 7z x ....
if errorlevel 1 (echo fail %%s) else (echo del %%s)
)

should fix your problem. 7zip appears to follow the rules and return errorlevel as 0 for success and non-zero otherwise. if errorlevel operates on the run-time value of errorlevel.



Related Topics



Leave a reply



Submit