Create a dedicated folder for every zip files in a directory and extract zip files Create a dedicated folder for every zip files in a directory and extract zip files linux linux

Create a dedicated folder for every zip files in a directory and extract zip files


unzip file.zip -d xxx will extract files to directory xxx, and xxx will be created if it is not there. You can check the man page for details.

The awk line below should do the job:

ls *.zip|awk -F'.zip' '{print "unzip "$0" -d "$1}'|sh

See the test below,

note that I removed |sh at the end, since my zips are fake archives; I just want to show the generated command line here.

kent$  ls -ltotal 0-rw-r--r-- 1 kent kent 0 Nov 12 23:10 001.zip-rw-r--r-- 1 kent kent 0 Nov 12 23:10 002.zip-rw-r--r-- 1 kent kent 0 Nov 12 23:10 003.zip-rw-r--r-- 1 kent kent 0 Nov 12 23:10 004.zip-rw-r--r-- 1 kent kent 0 Nov 12 23:10 005.zip-rw-r--r-- 1 kent kent 0 Nov 12 23:10 006.zip-rw-r--r-- 1 kent kent 0 Nov 12 23:10 007.zipkent$  ls *.zip|awk -F'.zip' '{print "unzip "$0" -d "$1}'unzip 001.zip -d 001unzip 002.zip -d 002unzip 003.zip -d 003unzip 004.zip -d 004unzip 005.zip -d 005unzip 006.zip -d 006unzip 007.zip -d 007


"extract here" is merely a feature of whatever unzip wrapper you are using. unzip will only extract what actually is in the archive. There is probably no simpler way than a shell script. But sed, awk etc. are not needed for this if you have a POSIX-compliant shell:

for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done

(You MUST NOT escape the * or pathname expansion will not take place.) Be aware that if the ZIP archive contains a directory, such as with Eclipse archives (which always contain eclipse/), you would end up with ./eclipse*/eclipse/eclipse.ini in any case. Add echo before unzip for a dry run.


p7zip, the command line version of 7zip does the job

7z x '*.zip' -o'*'

On Debian and Ubuntu, you have to install p7zip-full.