关于shell-script:shell脚本-逐个压缩文件夹内所有文件和文件夹

33次阅读

共计 825 个字符,预计需要花费 3 分钟才能阅读完成。

Linux 下通过 zip 命令一一压缩某文件夹下的子文件夹和文件(反对文件名中带空格场景):

filename : zipfolder

#!/bin/bash
IFS=$'\n'
for file in ` ls -1 $1 `
do
 if [[${file} == *".zip" ]]
 then
   echo "skipped: ${file}"
 else
   zip -r "$1/${file}.zip" "$1/${file}"
 fi
done

示例:

执行前:

MyMacBook-Pro:~ root$ find /Users/root/targetfolder -print | sed -e ‘s;1*/;|____;g;s;____|; |;g’
| | |____targetfolder
| | | |____file1.txt
| | | |____folder1
| | | | |____2.txt
| | | |____zipfile.zip

执行:

MyMacBook-Pro:~ root$ ./zipfolder /Users/root/targetfolder
adding: /Users/root/targetfolder/file1.txt (deflated 33%)
adding: /Users/root/targetfolder/folder1/ (stored 0%)
adding: /Users/root/targetfolder/folder1/2.txt (deflated 33%)
Skipped: zipfile.zip

执行后:

MyMacBook-Pro:~ root$ find /Users/root/targetfolder -print | sed -e ‘s;1*/;|____;g;s;____|; |;g’
| | |____targetfolder
| | | |____file1.txt
| | | |____file1.txt.zip
| | | |____folder1
| | | | |____2.txt
| | | |____folder1.zip
| | | |____zipfile.zip


  1. / ↩

正文完
 0