linux find命令之exec

  find是咱們很經常使用的一個Linux命令,可是咱們通常查找出來的並不單單是看看而已,還會有進一步的操做,這個時候exec的做用就顯現出來了。 shell

  exec解釋:安全

    -exec  參數後面跟的是command命令,它的終止是以;爲結束標誌的,因此這句命令後面的分號是不可缺乏的,考慮到各個系統中分號會有不一樣的意義,因此前面加反斜槓。bash

    {}   花括號表明前面find查找出來的文件名。spa

  使用find時,只要把想要的操做寫在一個文件裏,就能夠用exec來配合find查找,很方便的。在有些操做系統中只容許-exec選項執行諸如l s或ls -l這樣的命令。大多數用戶使用這一選項是爲了查找舊文件並刪除它們。建議在真正執行rm命令刪除文件以前,最好先用ls命令看一下,確認它們是所要刪除的文件。 exec選項後面跟隨着所要執行的命令或腳本,而後是一對兒{ },一個空格和一個\,最後是一個分號。爲了使用exec選項,必需要同時使  用print選項。若是驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名。操作系統

  實例1:ls -l命令放在find命令的-exec選項中 code

  命令:find . -type f -exec ls -l {} \;(find命令匹配到了當前目錄下的全部普通文件,並在-exec選項中使用ls -l命令將它們列出)blog

[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

  實例2:在目錄中查找更改時間在n日之前的文件並刪除它們class

  命令:find . -type f -mtime +14 -exec rm {} \; (在shell中用任何方式刪除文件以前,應當先查看相應的文件,必定要當心!當使用諸如mv或rm命令時,能夠使用-exec選項的安全模式。它將在對每一個匹配到的文件進行操做以前提示你。 )test

[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.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
總計 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]# 

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

  命令:find . -name "*.log" -mtime +5 -ok rm {} \;( find命令在當前目錄中查找全部文件名以.log結尾、更改時間在5日以上的文件,並刪除它們,只不過在刪除以前先給出提示。 按y鍵刪除文件,按n鍵不刪除。 )

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

  實例4:-exec中使用grep命令

  命令:find /etc -name "passwd*" -exec grep "root" {} \;(任何形式的命令均可以在-exec選項中使用。 find命令首先匹配全部文件名爲「 passwd*」的文件,例如passwd、passwd.old、passwd.bak,而後執行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]#

  實例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
[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.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]#
相關文章
相關標籤/搜索