Shell-Script
給定父目錄的所有子目錄中的 zip 文件
假設我有一個
parent_directory/
帶有以下子目錄的child1/
,child2/
和child3/
.我如何將
zip
所有*.txt
文件childx/
寫入腳本childx.zip
我也希望它childx.zip
位於其childx/
目錄中。
樣本數據
$ mkdir -p parent_directory/child{1..3} $ touch parent_directory/child{1..3}/file1.txt $ touch parent_directory/child{1..3}/file2.txt $ tree parent_directory/ parent_directory/ ├── child1 │ ├── file1.txt │ └── file2.txt ├── child2 │ ├── file1.txt │ └── file2.txt └── child3 ├── file1.txt └── file2.txt 3 directories, 6 files
解決方案
現在編寫 zip 文件的腳本:
$ cd parent_directory/; for i in *; do find ${i} -name "*.txt" -print | zip ${i}.zip -@; mv ${i}.zip ${i}; done; cd - adding: child1/file2.txt (stored 0%) adding: child1/file1.txt (stored 0%) adding: child2/file2.txt (stored 0%) adding: child2/file1.txt (stored 0%) adding: child3/file2.txt (stored 0%) adding: child3/file1.txt (stored 0%)
結果
結果:
$ tree parent_directory/ parent_directory/ ├── child1 │ ├── child1.zip │ ├── file1.txt │ └── file2.txt ├── child2 │ ├── child2.zip │ ├── file1.txt │ └── file2.txt └── child3 ├── child3.zip ├── file1.txt └── file2.txt 3 directories, 9 files $ unzip -l parent_directory/child1/child1.zip Archive: parent_directory/child1/child1.zip Length Date Time Name --------- ---------- ----- ---- 0 07-05-2018 10:08 child1/file2.txt 0 07-05-2018 10:08 child1/file1.txt --------- ------- 0 2 files $ unzip -l parent_directory/child2/child2.zip Archive: parent_directory/child2/child2.zip Length Date Time Name --------- ---------- ----- ---- 0 07-05-2018 10:08 child2/file2.txt 0 07-05-2018 10:08 child2/file1.txt --------- ------- 0 2 files $ unzip -l parent_directory/child3/child3.zip Archive: parent_directory/child3/child3.zip Length Date Time Name --------- ---------- ----- ---- 0 07-05-2018 10:08 child3/file2.txt 0 07-05-2018 10:08 child3/file1.txt --------- ------- 0 2 files
參考