Unix unzip: how to batch unzip zip files in a folder and save in subfolders?
You could do something like this:
for file in *.zip; do
dir=$(basename "$file" .zip) # remove the .zip from the filename
mkdir "$dir"
cd "$dir" && unzip ../"$file" && rm ../"$file" # unzip and remove file if successful
cd ..
done
or, run it together on one line:
for file in *.zip; do dir=$(basename "$file" .zip); mkdir "$dir"; cd "$dir"; unzip ../"$file" && rm ../"$file"; cd ..; done
If you need/want to keep the original .zip files, just remove the && rm ../"$file"
bit.
Unzip All Files In A Directory
This works in bash, according to this link:
unzip \*.zip
How to unzip different compression formats in subfolders in Linux terminal
Yes, that code fragment looks like a
bash
script. If it is namedfilename.bat
, you should be able to usesudo bash filename.bat
to run it.The code assumes that the current directory is the 'parent folder' which contains all the zipped files. You'll need to modify the code to handle sub-directories containing
.zip
files. There are numerous ways to do that.Given a requirement to handle formats other than just
.zip
files, you'd probably revise the code to use the file names it is given as arguments as the files to uncompress.
This code might work:
for file in "$@"
do
dir=$(dirname "$file")
extn=${base##*.}
base=$(basename "$file" .$extn)
mkdir -p "$dir/$base"
(
cd "$dir/$base"
case $extn in
zip) unzip "../$base.$extn";;
esac
)
done
Now, in theory, you could extend the list of extensions in the case
statement to include other file formats. However, you should be aware that not all compressors also package multiple files. Typically, you have a composite format such as .tar.gz
or .tar.xz
or .tar.bz2
. The corresponding compressors (or decompressors), simply decompress the file (losing the compression suffix) without extracting the data from the .tar
file inside. However, if rar
and 7z
do behave like zip
, then you can use:
case $extn in
(zip) unzip "../$base.$extn";;
(rar) unrar "../$base.$extn";; # Or whatever the command is
(7z) un7z "../$base.$extn";; # Or whatever the command is
(*) echo "$0: unrecognized extension $extn on $file" >&2;;
esac
You can also reinstate the code to delete the compressed form of the file if that seems appropriate to you.
Batch script to unzip files into a certain folder
When reading the help for 7z
in your cmd window, you will notice that the -o switch is the output directory option. Currently you are telling it to be %%~dpI
which is in fact Drive and Path of the current of the zip files. So you would want to change the output directory:
for /R "C:\root\folder" %%I in ("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -oc"C:\root\folder2" "%%~fI"
)
I do not currently have 7zip installed, but I am almost 100% sure that it has a recursive function builtin, if so, you do not even need the for loop, you can just try:
7z.exe x -y "C:\root\folder\*.zip" -oc:"C:\root\folder2" -r
If it does not work, I will remove this section from the answer.
How do I unzip all files in a folder using 7-zip in batch?
This will unzip all zip files in the current folder(into the same folder), assuming you have installed 7zip into C:\Program Files\7-Zip
location.
If you have added your 7zip folder into the path, you can just enter 7z instead of the fullpath
"C:\Program Files\7-Zip\7z.exe" e *.zip
Related Topics
Sdl Configuration in Eclipse Ide
Arm Performance Counters Vs Linux Clock_Gettime
Openldap + Dynlist + Posixgroup
How to Find All Image Tags of a Running Docker Container
Install Lisp on My Linux Machine
Linux Script with Netcat Stops Working After X Hours
Batch Crop and Resize Images to Create Thumbnails
Scp Command Between 2 Servers with 2 Different .Pem Keys
Git Post-Receive Checkout to Remote Machine
Diff Files Inside of Zip Without Extracting It
How to Check If a String Contains a Special Character (!@#$%^&*()_+)
Why Doesn't Time() from Time.H Have a Syscall to Sys_Time