Unix unzip: how to batch unzip zip files in a folder and save in subfolders?

允我心安 提交于 2019-12-05 03:15:55

问题


Say if I have a folder 'images' and inside it there are 0001.zip to 9999.zip, I want to unzip all of them and save them in subfolder which has their file name, for example, 0001.zip will be unzipped and saved to /0001, 0002.zip will be unzipped and saved to /0002, I tried to do

unzip '*.zip'

but that extracts all files in current folder.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/11804438/unix-unzip-how-to-batch-unzip-zip-files-in-a-folder-and-save-in-subfolders

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