find是我們很常用的一個(gè)Linux命令,但是我們一般查找出來的并不僅僅是看看而已,還會(huì)有進(jìn)一步的操作,這個(gè)時(shí)候exec的作用就顯現(xiàn)出來了。
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í)行的命令或腳本,然后是一對(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]#說明:
上面的例子中,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.loglrwxrwxrwx 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.txtdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxrwxrwx 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.loglrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 11-12 19:32 test3drwxrwxrwx 2 root root 4096 11-12 19:32 test4[root@localhost test]#
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注