Linux find命令中-exec參數(shù)的作用介紹
我們都知道,Linux命令加上不同的參數(shù)其效果也不同,下面小編將針對Linux fing命令中的-exec 參數(shù)給大家做個詳細(xì)介紹,以便你有個了解。

exec解釋:
-exec 參數(shù)后面跟的是command命令,它的終止是以;為結(jié)束標(biāo)志的,所以這句命令后面的分號是不可缺少的,考慮到各個系統(tǒng)中分號會有不同的意義,所以前面加反斜杠。
{} 花括號代表前面find查找出來的文件名。
使用find時,只要把想要的操作寫在一個文件里,就可以用exec來配合find查找,很方便的。在有些操作系統(tǒng)中只允許-exec選項執(zhí)行諸如l s或ls -l這樣的命令。大多數(shù)用戶使用這一選項是為了查找舊文件并刪除它們。建議在真正執(zhí)行rm命令刪除文件之前,最好先用ls命令看一下,確認(rèn)它們是所要刪除的文件。 exec選項后面跟隨著所要執(zhí)行的命令或腳本,然后是一對兒{ },一個空格和一個\,最后是一個分號。為了使用exec選項,必須要同時使用print選項。如果驗證一下find命令,會發(fā)現(xiàn)該命令只輸出從當(dāng)前路徑起的相對路徑及文件名。
實例1:ls -l命令放在find命令的-exec選項中
命令:
find 。 -type f -exec ls -l {} \;
輸出:
代碼如下:
?。踨oot@localhost test]# find 。 -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 127 10-28 16:51 。/log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 。/test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 。/test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 。/test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 。/log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 。/log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 。/log.log
-rw-r--r-- 1 root root 37 10-28 17:07 。/log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 。/test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 。/test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 。/test3/log3-1.log
[root@localhost test]#
說明:
上面的例子中,find命令匹配到了當(dāng)前目錄下的所有普通文件,并在-exec選項中使用ls -l命令將它們列出。
實例2:在目錄中查找更改時間在n日以前的文件并刪除它們
命令:
find 。 -type f -mtime +14 -exec rm {} \;
輸出:
代碼如下:
?。踨oot@localhost test]# ll
總計 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
?。踨oot@localhost test]# find 。 -type f -mtime +14 -exec rm {} \;
?。踨oot@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
?。踨oot@localhost test]#
說明:
在shell中用任何方式刪除文件之前,應(yīng)當(dāng)先查看相應(yīng)的文件,一定要小心!當(dāng)使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的文件進(jìn)行操作之前提示你。
實例3:在目錄中查找更改時間在n日以前的文件并刪除它們,在刪除之前先給出提示
命令:
find 。 -name “*.log” -mtime +5 -ok rm {} \;
輸出:
代碼如下:
?。踨oot@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find 。 -name “*.log” -mtime +5 -ok rm {} \;
《 rm 。。。 。/log_link.log 》 ? y
《 rm 。。。 。/log2012.log 》 ? n
?。踨oot@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
?。踨oot@localhost test]#
說明:
在上面的例子中, find命令在當(dāng)前目錄中查找所有文件名以.log結(jié)尾、更改時間在5日以上的文件,并刪除它們,只不過在刪除之前先給出提示。按y鍵刪除文件,按n鍵不刪除。
實例4:-exec中使用grep命令
命令:
find /etc -name “passwd*” -exec grep “root” {} \;
輸出:
代碼如下:
?。踨oot@localhost test]# find /etc -name “passwd*” -exec grep “root” {} \;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
說明:
任何形式的命令都可以在-exec選項中使用。在上面的例子中我們使用grep命令。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個root用戶。
上一頁123下一頁共3頁
實例5:查找文件移動到指定目錄
命令:
find 。 -name “*.log” -exec mv {} 。。 \;
輸出:
代碼如下:
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
?。踨oot@localhost test]# cd test3/
?。踨oot@localhost test3]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
?。踨oot@localhost test3]# find 。 -name “*.log” -exec mv {} 。。 \;
?。踨oot@localhost test3]# ll
總計 0[root@localhost test3]# cd 。。
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
?。踨oot@localhost test]#
實例6:用exec選項執(zhí)行cp命令
命令:
find 。 -name “*.log” -exec cp {} test3 \;
輸出:
代碼如下:
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd 。。
?。踨oot@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
?。踨oot@localhost test]# find 。 -name “*.log” -exec cp {} test3 \;
cp: “。/test3/log2014.log” 及 “test3/log2014.log” 為同一文件
cp: “。/test3/log2013.log” 及 “test3/log2013.log” 為同一文件
cp: “。/test3/log2012.log” 及 “test3/log2012.log” 為同一文件
?。踨oot@localhost test]# cd test3
?。踨oot@localhost test3]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test3]#
上面就是Linux find命令中-exec參數(shù)的用法介紹了,find命令的參數(shù)還有很多,如果你還想了解其他參數(shù)的使用,詳見Linux find命令中-path -prune參數(shù)的作用介紹。
相關(guān)文章

集成系統(tǒng)級Claw模式! Deepin 官宣發(fā)布 25.1 版本
deepin操作系統(tǒng)發(fā)布了最新的 25.1 版本更新,該版本基于 deepin 25 正式版積累的多輪內(nèi)測成果,在 AI 能力、內(nèi)核版本、桌面環(huán)境、文件管理器以及系統(tǒng)安全等方面進(jìn)行了更新2026-04-13
又一代老硬件退場! Linux 內(nèi)核正式放棄Intel 486 CPU
在過去的幾十年間,CPU 的架構(gòu)已經(jīng)經(jīng)歷了飛速發(fā)展,x86 系列就是其中之一,而 i486 則屬于該系列中的一個,當(dāng)前,i486 的CPU處理器已經(jīng)夠老,從 Linux 7.1 開始將不再有對2026-04-09
趕緊收藏! 全網(wǎng)最全 Linux 命令總結(jié)
我把 Linux 中最常用、最實用、最常被問到的命令按照實際使用場景分類整理,方便你快速查閱和記憶,內(nèi)容覆蓋日常運維、開發(fā)調(diào)試、性能分析、文件處理、網(wǎng)絡(luò)、安全、系統(tǒng)管2026-04-08
一分鐘內(nèi)檢查Linux服務(wù)器性能? 9個性能檢測常用的基本命令
今天我們來看看Linux系統(tǒng)中用于性能監(jiān)控的一系列命令,這些命令可以快速查看機(jī)器的負(fù)載情況,詳細(xì)請看下文介紹2026-03-18
從零基礎(chǔ)到精通! 適合高級用戶的15款Linux發(fā)行版推薦
Linux作為操作系統(tǒng)領(lǐng)域靈活性和可定制性的基石,提供了大量滿足不同用戶需求的發(fā)行版,今天分享適合高級用戶的15款Linux發(fā)行版2026-03-10
開箱即用? 這4個高手級Linux發(fā)行版遠(yuǎn)沒你想象的那么安全易用
如果你正在糾結(jié)用哪個發(fā)行版?零基礎(chǔ)新手別被“高端”“極客”“聲明式”這些詞沖昏頭腦,先用好用的,再慢慢進(jìn)階2026-03-10
這幾款SSH工具真的夠用了! Linux好用的ssh工具推薦
在Linux上使用SSH,您需要安裝一個SSH客戶端,今天整理找到的8 款 SSH / 終端工具,從免費開源到企業(yè)級商用,從輕量化命令行到一站式工具箱,每款都做了介紹與對比,希望能2026-03-09
在Linux系統(tǒng)下有兩種用戶,即高級用戶root,普通用戶,高級用戶root可以在系統(tǒng)中做任何事情,普通用戶僅可在Linux系統(tǒng)中做有限的事情,下面我們就來看看切換方法2026-02-28
揭秘當(dāng)前登錄用戶的身份! Linux中使用logname命令的技巧
logname命令就是這樣一個簡單但強(qiáng)大的工具,它能幫助我們輕松獲取當(dāng)前登錄用戶的用戶名,今天,我們就來深入探索一下這個命令的工作原理、使用方法和最佳實踐2026-02-26
在 Linux 系統(tǒng)中,DNS 緩存是一種將域名和 IP 地址映射關(guān)系緩存在本地的機(jī)制,可以加快域名解析速度,并減輕 DNS 服務(wù)器的負(fù)載2026-02-26



