shell脚本: 逐个压缩文件夹内所有文件和文件夹

zHeng

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. /
阅读 105
1 声望
1 粉丝
0 条评论
你知道吗?

1 声望
1 粉丝
宣传栏