Unix解压缩:如何将文件夹中的zip文件解压缩并保存在子文件夹中? [英] Unix unzip: how to batch unzip zip files in a folder and save in subfolders?

查看:306
本文介绍了Unix解压缩:如何将文件夹中的zip文件解压缩并保存在子文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文件夹"images",并且在其中有0001.zip到9999.zip,我想将它们全部解压缩并保存在具有其文件名的子文件夹中,例如0001.zip将解压缩并保存到/0001,0002.zip将解压缩并保存到/0002,我尝试这样做

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.

推荐答案

您可以执行以下操作:

 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

如果您需要/想要保留原始的.zip文件,只需删除&& rm ../"$file"位.

If you need/want to keep the original .zip files, just remove the && rm ../"$file" bit.

这篇关于Unix解压缩:如何将文件夹中的zip文件解压缩并保存在子文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆