每天一個linux命令(20):find命令之exec 相關連結:每天一個linux命令(1):ls命令http://www.bkjia.com/os/201210/163049.html;每天一個linux命令(2):cd命令http://www.bkjia.com/os/201210/163050.html;每天一個linux命令(3):pwd命令http://www.bkjia.com/os/201210/163462.html;每天一個linux命令(4):mkdir命令http://www.bkjia.com/os/201210/163463.html;每天一個linux命令(5):rm命令http://www.bkjia.com/os/201210/163662.html;每天一個linux命令(6):rmdir命令http://www.bkjia.com/os/201210/164017.html;每天一個linux命令(7):mv命令http://www.bkjia.com/os/201210/164247.html;每天一個linux命令(8):cp命令http://www.bkjia.com/os/201210/164254.html;每天一個linux命令(9):touch命令http://www.bkjia.com/os/201211/165699.html;每天一個linux命令(10):cat命令http://www.bkjia.com/os/201211/165989.html;每天一個linux命令(11):nl命令http://www.bkjia.com/os/201211/165990.html每天一個linux命令(12):more命令http://www.bkjia.com/os/201211/165994.html每天一個linux命令(13):less命令http://www.bkjia.com/os/201211/165998.html每天一個linux命令(14):head命令http://www.bkjia.com/os/201211/166191.html每天一個linux命令(15):tail命令http://www.bkjia.com/os/201211/168702.html每天一個linux命令(16):which命令http://www.bkjia.com/os/201211/168890.html每天一個linux命令(17):whereis命令http://www.bkjia.com/os/201211/168893.html每天一個linux命令(18):locate命令http://www.bkjia.com/os/201211/168895.html每天一個linux命令(19):find命令概覽http://www.bkjia.com/os/201211/168897.html find是我們很常用的一個Linux命令,但是我們一般尋找出來的並不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。 www.2cto.com exec解釋:-exec 參數後面跟的是command命令,它的終止是以;為結束標誌的,所以這句命令後面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜線。{} 花括弧代表前面find尋找出來的檔案名稱。 使用find時,只要把想要的操作寫在一個檔案裡,就可以用exec來配合find尋找,很方便的。在有些作業系統中只允許-exec選項執行諸如l s或ls -l這樣的命令。大多數使用者使用這一選項是為了尋找舊檔案並刪除它們。建議在真正執行rm命令刪除檔案之前,最好先用ls命令看一下,確認它們是所要刪除的檔案。 exec選項後面跟隨著所要執行的命令或指令碼,然後是一對兒{ },一個空格和一個\,最後是一個分號。為了使用exec選項,必須要同時使用print選項。如果驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及檔案名稱。執行個體1:ls -l命令放在find命令的-exec選項中 命令: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命令匹配到了目前的目錄下的所有普通檔案,並在-exec選項中使用ls -l命令將它們列出。執行個體2:在目錄中尋找更改時間在n日以前的檔案並刪除它們命令:find . -type f -mtime +14 -exec rm {} \; 輸出:[root@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.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總計 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]# 說明:在shell中用任何方式刪除檔案之前,應當先查看相應的檔案,一定要小心!當使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的檔案進行操作之前提示你。 執行個體3:在目錄中尋找更改時間在n日以前的檔案並刪除它們,在刪除之前先給出提示命令:find . -name "*.log" -mtime +5 -ok rm {} \;輸出:[root@localhost test]# ll總計 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]# find . -name "*.log" -mtime +5 -ok rm {} \;< rm ... ./log_link.log > ? y< rm ... ./log2012.log > ? n[root@localhost test]# ll總計 312-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.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]#說明:在上面的例子中, find命令在目前的目錄中尋找所有檔案名稱以.log結尾、更改時間在5日以上的檔案,並刪除它們,只不過在刪除之前先給出提示。 按y鍵刪除檔案,按n鍵不刪除。 執行個體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/bashroot:x:0:0:root:/root:/bin/bash[root@localhost test]#說明:任何形式的命令都可以在-exec選項中使用。 在上面的例子中我們使用grep命令。find命令首先匹配所有檔案名稱為“ passwd*”的檔案,例如passwd、passwd.old、passwd.bak,然後執行grep命令看看在這些檔案中是否存在一個root使用者。執行個體5:尋找檔案移動到指定目錄 命令:find . -name "*.log" -exec mv {} .. \;輸出:[root@localhost test]# ll總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-12 22:49 test3drwxrwxr-x 2 root root 4096 11-12 19:32 test4[root@localhost test]# cd test3/[root@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[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;[root@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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-12 22:50 test3drwxrwxr-x 2 root root 4096 11-12 19:32 test4[root@localhost test]#執行個體6:用exec選項執行cp命令 命令:find . -name "*.log" -exec cp {} test3 \;輸出:[root@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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-12 22:50 test3drwxrwxr-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總計 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]#