Linux find命令中-exec參數(shù)的作用介紹

我們都知道,Linux命令加上不同的參數(shù)其效果也不同,下面小編將針對Linux fing命令中的-exec 參數(shù)給大家做個(gè)詳細(xì)介紹,以便你有個(gè)了解。
exec解釋:
-exec 參數(shù)后面跟的是command命令,它的終止是以;為結(jié)束標(biāo)志的,所以這句命令后面的分號(hào)是不可缺少的,考慮到各個(gè)系統(tǒng)中分號(hào)會(huì)有不同的意義,所以前面加反斜杠。
{} 花括號(hào)代表前面find查找出來的文件名。
使用find時(shí),只要把想要的操作寫在一個(gè)文件里,就可以用exec來配合find查找,很方便的。在有些操作系統(tǒng)中只允許-exec選項(xiàng)執(zhí)行諸如l s或ls -l這樣的命令。大多數(shù)用戶使用這一選項(xiàng)是為了查找舊文件并刪除它們。建議在真正執(zhí)行rm命令刪除文件之前,最好先用ls命令看一下,確認(rèn)它們是所要?jiǎng)h除的文件。 exec選項(xiàng)后面跟隨著所要執(zhí)行的命令或腳本,然后是一對兒{ },一個(gè)空格和一個(gè)\,最后是一個(gè)分號(hào)。為了使用exec選項(xiàng),必須要同時(shí)使用print選項(xiàng)。如果驗(yàn)證一下find命令,會(huì)發(fā)現(xiàn)該命令只輸出從當(dāng)前路徑起的相對路徑及文件名。
實(shí)例1:ls -l命令放在find命令的-exec選項(xiàng)中
命令:
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
?。踨oot@localhost test]#
說明:
上面的例子中,find命令匹配到了當(dāng)前目錄下的所有普通文件,并在-exec選項(xiàng)中使用ls -l命令將它們列出。
實(shí)例2:在目錄中查找更改時(shí)間在n日以前的文件并刪除它們
命令:
find 。 -type f -mtime +14 -exec rm {} \;
輸出:
代碼如下:
?。踨oot@localhost test]# ll
總計(jì) 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
總計(jì) 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]#
說明:
在shell中用任何方式刪除文件之前,應(yīng)當(dāng)先查看相應(yīng)的文件,一定要小心!當(dāng)使用諸如mv或rm命令時(shí),可以使用-exec選項(xiàng)的安全模式。它將在對每個(gè)匹配到的文件進(jìn)行操作之前提示你。
實(shí)例3:在目錄中查找更改時(shí)間在n日以前的文件并刪除它們,在刪除之前先給出提示
命令:
find 。 -name “*.log” -mtime +5 -ok rm {} \;
輸出:
代碼如下:
?。踨oot@localhost test]# ll
總計(jì) 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
總計(jì) 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
[root@localhost test]#
說明:
在上面的例子中, find命令在當(dāng)前目錄中查找所有文件名以.log結(jié)尾、更改時(shí)間在5日以上的文件,并刪除它們,只不過在刪除之前先給出提示。按y鍵刪除文件,按n鍵不刪除。
實(shí)例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選項(xiàng)中使用。在上面的例子中我們使用grep命令。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個(gè)root用戶。
上一頁123下一頁共3頁
實(shí)例5:查找文件移動(dòng)到指定目錄
命令:
find 。 -name “*.log” -exec mv {} 。。 \;
輸出:
代碼如下:
?。踨oot@localhost test]# ll
總計(jì) 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/
[root@localhost test3]# ll
總計(jì) 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 {} 。。 \;
[root@localhost test3]# ll
總計(jì) 0[root@localhost test3]# cd 。。
?。踨oot@localhost test]# ll
總計(jì) 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]#
實(shí)例6:用exec選項(xiàng)執(zhí)行cp命令
命令:
find 。 -name “*.log” -exec cp {} test3 \;
輸出:
代碼如下:
?。踨oot@localhost test3]# ll
總計(jì) 0[root@localhost test3]# cd 。。
?。踨oot@localhost test]# ll
總計(jì) 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
[root@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” 為同一文件
[root@localhost test]# cd test3
?。踨oot@localhost test3]# ll
總計(jì) 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
?。踨oot@localhost test3]#
上面就是Linux find命令中-exec參數(shù)的用法介紹了,find命令的參數(shù)還有很多,如果你還想了解其他參數(shù)的使用,詳見Linux find命令中-path -prune參數(shù)的作用介紹。
相關(guān)文章
Fedora Linux 42 穩(wěn)定版發(fā)布: 帶來大量新功能和軟件更新
Fedora 42昨日發(fā)布,這是 Red Hat 贊助開發(fā)的杰出前沿 Linux 發(fā)行版的最新版,包含大量新功能和軟件更新,使其成為 2025 年上半年發(fā)布的一款出色的 Linux 操作系統(tǒng)之一,內(nèi)2025-04-16如何在Linux查看硬盤信息? 查看Linux硬盤大小類型和硬件信息的5種方法
使用Linux系統(tǒng)的過程中,查看和了解硬盤信息是非常重要的工作,尤其是對于系統(tǒng)管理員而言,那么在Linux系統(tǒng)中如何查看硬盤信息?以下是具體內(nèi)容介紹2025-03-12如何在 Linux 中查看 CPU 詳細(xì)信息? 3招輕松查看CPU型號(hào)、核心數(shù)和溫度
在日常運(yùn)維工作中,獲取 CPU 信息是系統(tǒng)運(yùn)維管理員常見的工作內(nèi)容,無論是為了性能調(diào)優(yōu)、硬件升級(jí)還是僅僅滿足好奇心2025-03-11什么是 Arch Linux? 獨(dú)樹一幟的Arch Linux發(fā)行版分析
Arch Linux是為簡化,優(yōu)化,現(xiàn)代化,實(shí)用主義,用戶中心和多功能性而創(chuàng)建Linux發(fā)行版,究竟是什么讓 Arch 與眾不同?下面我們就來簡要解讀2025-02-19如何在Linux環(huán)境下制作 Win11裝機(jī)U盤?
一直用的linux辦公,想要將筆記本電腦從 Linux 系統(tǒng)切換回 Windows 11,我們可以制作一個(gè)win11裝機(jī)u盤,詳細(xì)如下2025-02-17Rsnapshot怎么用? 基于Rsync的強(qiáng)大Linux備份工具使用指南
Rsnapshot 不僅可以備份本地文件,還能通過 SSH 備份遠(yuǎn)程文件,接下來詳細(xì)介紹如何安裝、配置和使用 Rsnapshot,包括創(chuàng)建每小時(shí)、每天、每周和每月的本地備份,以及如何進(jìn)2025-02-06Linux Kernel 6.13發(fā)布:附更新內(nèi)容及新特性解讀
Linux 內(nèi)核 6.13 正式發(fā)布,新版本引入了惰性搶占支持,簡化內(nèi)核搶占邏輯,通過減少與調(diào)度器相關(guān)的調(diào)用次數(shù),讓內(nèi)核在運(yùn)行時(shí)表現(xiàn)更優(yōu),從而提高效率2025-01-23五大特性引領(lǐng)創(chuàng)新! 深度操作系統(tǒng) deepin 25 Preview預(yù)覽版發(fā)布
今日,深度操作系統(tǒng)正式推出deepin 25 Preview版本,該版本集成了五大核心特性:磐石系統(tǒng)、全新DDE、Treeland窗口合成器、AI For OS以及Distrobox子系統(tǒng)2025-01-18Linux Mint Xia 22.1重磅發(fā)布: 重要更新一覽
Beta 版 Linux Mint“Xia” 22.1 發(fā)布,新版本基于 Ubuntu 24.04,內(nèi)核版本為 Linux 6.8,這次更新帶來了諸多優(yōu)化和改進(jìn),進(jìn)一步鞏固了 Mint 在 Linux 桌面操作系統(tǒng)領(lǐng)域的2025-01-16LinuxMint怎么安裝? Linux Mint22下載安裝圖文教程
Linux Mint22發(fā)布以后,有很多新功能,很多朋友想要下載并安裝,該怎么操作呢?下面我們就來看看詳細(xì)安裝指南2025-01-16