Linux 中 find 查找命令還能這樣用

‍來自公衆號:入門小站

Linux 中的 find 命令是根據給定條件查找文件和目錄的常用命令, 有很多的技巧。下面我們講一下使用exec配合使用的一些查找技巧, 保證你的查找效率更上一層樓。

將 exec 命令與文件命令的輸出一起使用

用 exec 執行 find 的基本語法如下:

find [path] [arguments] -exec [command] {} \;

參數講解:

注意: {} 和\ 之間必須有空格;

還有另一種語法與上述略有不同,如下所示:

find [path] [arguments] -exec [command] {} +

在這裏+表示對於 find 命令的每個結果,[command] 只執行一次。所有結果都作爲參數一起傳遞給 [command]+ 不需要用\+轉義。

{} \;表示爲每個找到的結果執行該命令:

ls filetxt
ls filetxt
ls filetxt

{} +表示爲所有結果參數執行一次命令

ls filetxt filetxt filetxt

雖然在這裏使用{} +似乎是更好的選擇,但如果 find 命令拋出 50 個結果, 會導致參數過長出現錯誤。

find 和 exec 命令的實戰案例

1. 查找並顯示文件屬性

顯示 /tmp 目錄下的所有lock文件並顯示它們的屬性。

sudo find /tmp/ -type f -name *lock -exec ls -l {} \;

結果如下

root@rumenz:~$ sudo find /tmp/ -type f -name *lock -exec ls -l {} \; 
-r--r--r-- 1 gdm gdm 11 Jul 17 08:01 /tmp/.X1024-lock
-r--r--r-- 1 gdm gdm 11 Jul 17 08:01 /tmp/.X1025-lock

2. 查找和重命名文件

使用 find 和 exec 可以輕鬆重命名文件。

sudo find /home/sagar/Downloads/ -type f -name 'ubuntu*' -exec mv {} {}_renamed \;

上面的命令查找以名稱 ubuntu 開頭的文件並將它們存儲在佔位符{}中。一旦將結果存儲在佔位符中,它將在佔位符中存儲的每個文件的末尾添加_renamed。

3. 收集和存儲文件大小

將 /tmp 目錄下每個文件的大小,並將輸出保存在 /root 目錄下,文件名爲 rumenz.out

sudo find /tmp/ -type f -exec du -sh {} \; > /root/rumenz.out

4. 刪除帶有特定參數的文件

自動刪除文件時請格外小心rm -i要麼先查看 find 命令的結果。

find exec 命令組合的另一個常見示例是查找大於特定大小的文件並將其刪除。比如: 清理舊日誌。

刪除大於 100 MB 的文件

find ~/Desktop -size +100M -exec rm {} \;

同樣, 你也可以根據文件的創建修改時間來刪除文件。例如,讓我們刪除超過 10 天的文件。

sudo find /tmp/ -type f -mtime +10 -exec rm {} \;

在這使用了 -mtime 來識別過去 24 小時內修改的數據,當與它配對 +10 時,它會查找並刪除超過 10 天的文件。

5. 執行指定的命令

有些情況下, 我們希望對查找到的文件執行某些命令。

例如當我搜索任何 mp3 文件時,我想運行 id3v2 它將帶來有關找到的 mp3 文件的詳細信息。

find . -name "*.mp3" -exec id3v2 -l {} \;

id3v2 是顯示 mp3 文件詳細信息的包,-l 用於顯示與找到的 mp3 關聯的每個 mp3 標籤。

6. 更改文件和目錄的所有權

更改文件和目錄的所有權, 將用戶deploy擁有的文件, 改成rumenz

sudo find /home/sagar/disk/Downloads -user deploy -type f -exec chown rumenz {} \;

7. 更改文件的權限

那麼如何使用 find 和 exec 更改文件的權限呢?

sudo find /home/sagar/disk/Downloads -type f -exec chmod 644 {} \;

在上面的命令中,使用了-type f 所以該命令只會應用於給定目錄下的文件。

8. 查看每個文件的 md5sum

如何查看/tmp目錄下每個文件的md5值。

sudo find /tmp/ -type f -exec md5sum {} \;

可以將結果保存到指定文件中。

sudo find /tmp/ -type f -exec md5sum {} \; > /tmp/rumenz.out

9. 結合 exec 和 grep 命令

下面的命令搜索所有具有.txt擴展名的文件。使用 grep 它會在這些 .txt 文件的內容中搜索字符串 excerpt。

find . -type f -name "*.txt" -exec grep -iH excerpt {} \;

使用該-H選項,grep 命令將顯示每個匹配項的文件名。

root@rumenz# find . -type f -name "*.txt" -exec grep -iH excerpt {} \;
./author.txt:                <div class="post-card-excerpt">{{bio}}</div>
./partials/post-card.txt:            {{#if excerpt}}
./partials/post-card.txt:                <div class="post-card-excerpt">{{excerpt}}</div>
./post.txt:        {{#if custom_excerpt}}
./post.txt:            <p class="article-excerpt">{{custom_excerpt}}</p>
./tag.txt:                <div class="post-card-excerpt">

使用多個 exec 命令查找

如果我們想多次處理查找出來的文件, 可以使用多個exec聯合處理。

find . -type f -name "*.txt" -exec echo {} \; -exec grep excerpt {} \;

它將首先搜索 .txt 文件,然後使用第一個 exec 命令回顯它們的名稱。然後,將在這些文件中搜索excerpt字符串。

root@rumenz# find . -type f -name "*.txt" -exec echo {} \; -exec grep excerpt {} \;
./index.txt
./page.txt
./default.txt
./author.txt
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/R7ltYz4-8-we_7IbNwhbDA