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

喜你入骨 提交于 2019-12-18 09:59:20

问题


If I choose a zip file and right click "extract here" a folder with the zip filename is created and the entire content of the zip file is extracted into it.

However, I would like to convert several zip files via shell. But when I do

unzip filename.zip

the folder "filename" is not created but all the files are extracted into the current directory.

I have looked at the parameters but there is no such parameter. I also tried

for zipfile in \*.zip; do mkdir $zipfile; unzip $zipfile -d $zipfile/; done

but the .zip extension of the 2. $zipfile and 4. $zipfile have to be removed with sed. If I do

for zipfile in \*.zip; do mkdir sed 's/\.zip//i' $zipfile; unzip $zipfile -d sed 's/\.zip//i' $zipfile/; done 

it is not working.

How do I replace the .zip extension of $zipfile properly?

Is there an easier way than a shell script?


回答1:


"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.




回答2:


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 -l
total 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.zip

kent$  ls *.zip|awk -F'.zip' '{print "unzip "$0" -d "$1}'
unzip 001.zip -d 001
unzip 002.zip -d 002
unzip 003.zip -d 003
unzip 004.zip -d 004
unzip 005.zip -d 005
unzip 006.zip -d 006
unzip 007.zip -d 007



回答3:


If you have p7zip, the command line version of 7zip installed, then you can use:

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



回答4:


Add the folder name and slash after the switch, example:

unzip -d newfolder/ zipfile.zip

zip will create the folder 'newfolder' and extract the archive into it.

Note that the trailing slash is not required but I guess it's just from old habit (I use Debian and Ubuntu).




回答5:


aunpack from atool will do this by default for all sorts of archives.



来源:https://stackoverflow.com/questions/8107886/create-a-dedicated-folder-for-every-zip-files-in-a-directory-and-extract-zip-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!