動態搜尋特定路徑中的 zip 檔案並將其解壓縮到與解壓縮檔案相同的資料夾中?

動態搜尋特定路徑中的 zip 檔案並將其解壓縮到與解壓縮檔案相同的資料夾中?

如何動態搜尋特定路徑(例如:/opt/uploading/"*"/multiple .zip檔案)中的 zip 檔案並將其解壓縮到與 zip 檔案相同的資料夾中?

下面的功能是解壓縮多個zip檔並刪除zip檔。但我希望 zip 檔案與解壓縮檔案一起存在。

while true; do
   find -iname '*.zip' > zipindex
   test -s zipindex || break
   for zip in $(cat zipindex); do unzip -o $zip && rm $zip; done
done

答案1

嗯,rm $zip正在刪除 .Zip 文件,因此將其刪除。

答案2

這比 while 迴圈更適合 for 迴圈。透過這種方式,您還可以擺脫不必要的結果保存find -iname '*.zip' > zipindex

做這樣的事情:

#!/bin/bash

for zip in $(find -iname '*.zip'); do
    unzip -o $zip
done

這將迭代所有找到產生的行。

當然,您應該完全放棄 bash 腳本並創建一個 find oneliner,如下所示:

find -iname '*.zip' -execdir unzip {} \;

編輯:感謝@don_crissti 和@ilkkachu,我成功地擺脫了另一個在-exec 中呼叫shell 的實例。 -execdir 很高興知道!

答案3

使用 GNU Parallel 你可以這樣做:

find my_dir | grep -E '\.zip$' | parallel unzip
inotifywait -qmre MOVED_TO -e CLOSE_WRITE --format %w%f my_dir | grep  -E '\.zip$' | parallel -u unzip

這樣您就不需要忙著等待下一個 zip 檔案的出現。

相關內容