Linux 使用 find、read 對目錄內的指定檔案做處理

Linux 於 Bash shell (CLI) 要把檔案列出來,再來要抓出每個檔名,再分別處理,script 要怎麼寫呢?

Linux 使用 find、read 對目錄內的指定檔案做處理

在此使用 echo 'test' 做測試,在那行可以用 | 無限串接下去。

下面兩個 script 做的事情都一樣,一個是 find 印出來後,read -d 切割,在下面那個是直接讀取。

file 的變數可自行更換,於 while read -r 後面記得一起改掉即可。

範例程式

find ./ -name '.txt' -print0 | while read -d '' -r file; do                                                                         
    echo 'test' >> $file;                                                                                                                                                   
done
find ./ -name '.txt' | while read -r file; do                                                                                       
    echo 'test' >> $file;                                                                                                                                                   
done

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料