find命令-exec參數

find是很常用的一個Linux命令,但是一般查找出來的並不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。
exec解釋:

  • -exec 參數後面跟的是command命令,它的終止是以;為結束標誌的,所以這句命令後面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜杠。
    {}花括弧代表前面find查找出來的檔案名。

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

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

命令:

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

演示執行及輸出:

[root@localhost zaixian]# find . -type f -exec ls -l {} \;
-rw-r--r--. 1 zaixian zaixian 18 Aug  2  2016 ./.bash_logout
-rw-r--r--. 1 zaixian zaixian 193 Aug  2  2016 ./.bash_profile
-rw-r--r--. 1 zaixian zaixian 231 Aug  2  2016 ./.bashrc
-rw-rw-r--. 1 zaixian zaixian 30 Feb 13 03:14 ./test/mylog1.log
-rw-rw-r--. 1 zaixian zaixian 60 Feb 13 03:15 ./test/mylog2.log
-rw-rw-r--. 1 zaixian zaixian 49 Feb 13 03:20 ./test/log.txt
-rw-rw-r--. 1 zaixian zaixian 388 Feb 13 20:11 ./test/log.log
-rw-r--r--. 1 zaixian zaixian 11843 Feb 13 20:27 ./test/is line 1.
-rw-r--r--. 1 zaixian zaixian 3270 Feb 13 20:29 ./test/t
-rw-r--r--. 1 zaixian zaixian 11843 Feb 13 20:29 ./test/t test]$ cat log.log
-rw-rw-r--. 1 zaixian zaixian 229633 Feb 15 03:06 ./test/ping.log
-rw-rw-r--. 1 zaixian zaixian 9 Feb 12 22:46 ./test3/test4/test41/test.txt
-rw-rw-r--. 1 zaixian zaixian 26 Feb 13 01:15 ./test5/mylog.log
-rw-rw-r--. 1 zaixian zaixian 9 Feb 12 22:46 ./test5/test3/test4/test41/test.txt
-rw-rw-r--. 1 zaixian zaixian 0 Feb 13 01:26 ./mylog.log
-rw-rw-r--. 1 zaixian zaixian 9 Feb 12 22:46 ./test-noexists/test4/test41/test.txt
-rw-------. 1 zaixian zaixian 540 Feb 13 20:34 ./.lesshst
[root@localhost zaixian]#

說明: 上面的例子中,find命令匹配到了當前目錄下的所有普通檔,並在-exec選項中使用ls -l命令將它們列出。

實例2:在目錄中查找更改時間在n日以前的檔並刪除它們

命令:

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

演示執行及輸出:

[root@localhost zaixian]# ll
total 0
lrwxrwxrwx. 1 zaixian zaixian   9 Feb 13 01:26 mylog_link.log -> mylog.log
-rw-rw-r--. 1 zaixian zaixian   0 Feb 13 01:26 mylog.log
drwxrwxr-x. 2 zaixian zaixian 143 Feb 15 02:16 test
drwxrwxr-x. 2 zaixian zaixian   6 Feb 12 22:33 test1
drwxrwxr-x. 2 zaixian zaixian   6 Feb 12 22:33 test2
drwxrwxr-x. 3 zaixian zaixian  19 Feb 12 22:58 test3
drwxrwxr-x. 3 zaixian zaixian  36 Feb 13 01:20 test5
drwxrwxr-x. 3 zaixian zaixian  19 Feb 12 22:58 test-noexists
[root@localhost zaixian]# find . -type f -mtime +2 -exec rm {} \;

說明:在shell中用任何方式刪除檔之前,應當先查看相應的檔,一定要小心!當使用諸如mvrm命令時,可以使用-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.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

說明:在上面的例子中, find命令在當前目錄中查找所有檔案名以.log結尾、更改時間在5日以上的檔,並刪除它們,只不過在刪除之前先給出提示。 按y鍵刪除檔,按n鍵不刪除。

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

命令:

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

演示執行及輸出:

[root@localhost zaixian]# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost zaixian]# find /etc -name "passwd*" -exec grep "zaixian" {} \;
zaixian:x:1000:1000:zaixian:/home/zaixian:/bin/bash
zaixian:x:1000:1000:zaixian:/home/zaixian:/bin/bash
[root@localhost zaixian]#

說明:任何形式的命令都可以在-exec選項中使用。在上面的例子中我們使用grep命令。find命令首先匹配所有檔案名為“passwd*”的檔,例如:passwdpasswd.oldpasswd.bak,然後執行grep命令看看在這些檔中是否存在一個rootzaixian用戶(需要以root用戶身份操作)。

實例5:查找檔移動到指定目錄

命令:

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

演示執行及輸出:

[root@localhost zaixian]# find . -name "*.log" -exec mv {} .. \;
[root@localhost zaixian]# ls
test  test1  test2  test3  test5  test-noexists
[root@localhost zaixian]# cd ..
[root@localhost home]# ll
total 280
-rw-rw-r--. 1 zaixian zaixian    388 Feb 13 20:11 log.log
-rw-rw-r--. 1 zaixian zaixian     30 Feb 13 03:14 mylog1.log
-rw-rw-r--. 1 zaixian zaixian     60 Feb 13 03:15 mylog2.log
lrwxrwxrwx. 1 zaixian zaixian      9 Feb 13 01:26 mylog_link.log -> mylog.log
-rw-rw-r--. 1 zaixian zaixian      0 Feb 15 20:15 mylog.log
-rw-rw-r--. 1 zaixian zaixian 229633 Feb 15 03:06 ping.log
-rw-r--r--. 1 zaixian zaixian  11843 Feb 13 20:29 t test]$ cat log.log
drwx------. 8 zaixian zaixian    128 Feb 15 20:21 zaixian
[root@localhost home]#

實例6:用exec選項執行cp命令

命令:

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

演示執行及輸出:

[zaixian@localhost ~]$ ll
total 0
-rw-r--r--. 1 zaixian zaixian  0 Feb 15 20:23 file2.log
-rw-r--r--. 1 zaixian zaixian  0 Feb 15 20:23 file.log
drwxrwxr-x. 2 zaixian zaixian 48 Feb 15 20:21 test
drwxrwxr-x. 2 zaixian zaixian  6 Feb 12 22:33 test1
drwxrwxr-x. 2 zaixian zaixian  6 Feb 12 22:33 test2
drwxrwxr-x. 3 zaixian zaixian 19 Feb 12 22:58 test3
drwxrwxr-x. 3 zaixian zaixian 19 Feb 15 20:21 test5
drwxrwxr-x. 3 zaixian zaixian 19 Feb 12 22:58 test-noexists
[zaixian@localhost ~]$ ll test3/
total 0
drwxrwxr-x. 3 zaixian zaixian 20 Feb 12 22:46 test4
[zaixian@localhost ~]$ find . -name "*.log" -exec cp {} test3 \;
[zaixian@localhost ~]$ ll test3/
total 0
-rw-r--r--. 1 zaixian zaixian  0 Feb 15 20:28 file2.log
-rw-r--r--. 1 zaixian zaixian  0 Feb 15 20:28 file.log
drwxrwxr-x. 3 zaixian zaixian 20 Feb 12 22:46 test4
[zaixian@localhost ~]$

上一篇: find命令 下一篇: find命令-xargs參數