濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > linux find命令之exec簡(jiǎn)單概述

linux find命令之exec簡(jiǎn)單概述

熱門(mén)標(biāo)簽:鶴崗400電話申請(qǐng) 百度地圖標(biāo)注直線距離 測(cè)繪地圖標(biāo)注名稱 怎么在百度地圖標(biāo)注公司的位置 智能電銷機(jī)器人有用嗎 德陽(yáng)400電話申請(qǐng) 商機(jī)地圖標(biāo)注 天津電話外呼系統(tǒng)排名 外呼電話系統(tǒng)怎么操作

find是我們很常用的一個(gè)Linux命令,但是我們一般查找出來(lái)的并不僅僅是看看而已,還會(huì)有進(jìn)一步的操作,這個(gè)時(shí)候exec的作用就顯現(xiàn)出來(lái)了。

exec解釋:

-exec 參數(shù)后面跟的是command命令,它的終止是以;為結(jié)束標(biāo)志的,所以這句命令后面的分號(hào)是不可缺少的,考慮到各個(gè)系統(tǒng)中分號(hào)會(huì)有不同的意義,所以前面加反斜杠。

{} 花括號(hào)代表前面find查找出來(lái)的文件名。

使用find時(shí),只要把想要的操作寫(xiě)在一個(gè)文件里,就可以用exec來(lái)配合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í)行的命令或腳本,然后是一對(duì)兒{ },一個(gè)空格和一個(gè)\,最后是一個(gè)分號(hào)。為了使用exec選項(xiàng),必須要同時(shí)使用print選項(xiàng)。如果驗(yàn)證一下find命令,會(huì)發(fā)現(xiàn)該命令只輸出從當(dāng)前路徑起的相對(duì)路徑及文件名。

實(shí)例1:ls -l命令放在find命令的-exec選項(xiàng)中

命令:

find . -type f -exec ls -l {} \;

輸出:

[root@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]#

說(shuō)明:

上面的例子中,find命令匹配到了當(dāng)前目錄下的所有普通文件,并在-exec選項(xiàng)中使用ls -l命令將它們列出。

實(shí)例2:在目錄中查找更改時(shí)間在n日以前的文件并刪除它們

命令:

find . -type f -mtime +14 -exec rm {} \; 

輸出:

[root@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
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
[root@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]# 

說(shuō)明:

在shell中用任何方式刪除文件之前,應(yīng)當(dāng)先查看相應(yīng)的文件,一定要小心!當(dāng)使用諸如mv或rm命令時(shí),可以使用-exec選項(xiàng)的安全模式。它將在對(duì)每個(gè)匹配到的文件進(jìn)行操作之前提示你。

實(shí)例3:在目錄中查找更改時(shí)間在n日以前的文件并刪除它們,在刪除之前先給出提示

命令:

find . -name "*.log" -mtime +5 -ok rm {} \;

輸出:

[root@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
[root@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]#

說(shuō)明:

在上面的例子中, find命令在當(dāng)前目錄中查找所有文件名以.log結(jié)尾、更改時(shí)間在5日以上的文件,并刪除它們,只不過(guò)在刪除之前先給出提示。 按y鍵刪除文件,按n鍵不刪除。

實(shí)例4: -exec中使用grep命令

命令:

find /etc -name "passwd*" -exec grep "root" {} \;

輸出:

[root@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]#

說(shuō)明:

任何形式的命令都可以在-exec選項(xiàng)中使用。 在上面的例子中我們使用grep命令。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個(gè)root用戶。

實(shí)例5:查找文件移動(dòng)到指定目錄

命令:

find . -name "*.log" -exec mv {} .. \;

輸出:

[root@localhost test]# ll
總計(jì) 12
drwxr-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
[root@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
[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
[root@localhost test3]# ll
總計(jì) 0
[root@localhost test3]# cd ..
[root@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]#

實(shí)例6:用exec選項(xiàng)執(zhí)行cp命令

命令:

find . -name "*.log" -exec cp {} test3 \;

輸出:

[root@localhost test3]# ll
總計(jì) 0
[root@localhost test3]# cd ..
[root@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
[root@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
[root@localhost test3]#

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Python實(shí)現(xiàn)Linux的find命令實(shí)例分享
  • Linux中find命令的用法匯總
  • linux find命令之xargs簡(jiǎn)單概述
  • linux命令之find命令簡(jiǎn)單概述
  • Linux中find命令的用法入門(mén)
  • 淺談Linux下通過(guò)find命令進(jìn)行rm文件刪除的小技巧
  • linux的一個(gè)find命令配合rm刪除某天前的文件方法
  • Linux 命令find之查找文件的示例

標(biāo)簽:鎮(zhèn)江 滁州 丹東 自貢 優(yōu)質(zhì)小號(hào) 百色 六盤(pán)水 武漢

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux find命令之exec簡(jiǎn)單概述》,本文關(guān)鍵詞  linux,find,命令,之,exec,簡(jiǎn)單,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux find命令之exec簡(jiǎn)單概述》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于linux find命令之exec簡(jiǎn)單概述的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    衡山县| 盐城市| 姜堰市| 临颍县| 嫩江县| 玛曲县| 濮阳县| 噶尔县| 九寨沟县| 皮山县| 七台河市| 长葛市| 汉阴县| 盐津县| 龙岩市| 淮滨县| 扎兰屯市| 洮南市| 九龙城区| 团风县| 濮阳市| 炉霍县| 广宁县| 抚松县| 汶川县| 扎鲁特旗| 铁力市| 信宜市| 本溪| 壤塘县| 咸阳市| 阳城县| 镇沅| 临清市| 桦川县| 东山县| 长寿区| 鹤庆县| 达州市| 平顶山市| 渑池县|