unix unzip:如何批量解压缩文件夹中的zip文件并保存在子文件夹中?

hzx*_*zxu 3 unix linux zip unzip

假设我有一个文件夹'images',里面有0001.zip到9999.zip,我想解压缩所有这些并将它们保存在具有文件名的子文件夹中,例如,0001.zip将被解压缩并且保存到/ 0001,0002.zip将解压缩并保存到/ 0002,我试着这样做

unzip '*.zip'
Run Code Online (Sandbox Code Playgroud)

但是它会提取当前文件夹中的所有文件.

BRP*_*ock 7

你可以这样做:

 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
Run Code Online (Sandbox Code Playgroud)

或者,在一行上一起运行:

  for file in *.zip; do dir=$(basename "$file" .zip); mkdir "$dir"; cd "$dir"; unzip ../"$file" && rm ../"$file"; cd ..; done
Run Code Online (Sandbox Code Playgroud)

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


Nei*_*eil 5

for zip in *.zip
do
    unzip "$zip" -d "${zip%.zip}"
done
Run Code Online (Sandbox Code Playgroud)