中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

每天一個(ge)linux命令(20):find命令之(zhi)exec

find是我們很常用的(de)(de)一(yi)(yi)個Linux命令,但是我們一(yi)(yi)般(ban)查找出來(lai)的(de)(de)并不僅(jin)僅(jin)是看(kan)看(kan)而已,還會有進一(yi)(yi)步的(de)(de)操(cao)作(zuo),這個時候(hou)exec的(de)(de)作(zuo)用就顯(xian)現出來(lai)了。 

exec解釋:

-exec  參數后面(mian)跟的是(shi)(shi)command命令(ling),它(ta)的終止是(shi)(shi)以(yi);為結束標志的,所以(yi)這句命令(ling)后面(mian)的分號(hao)是(shi)(shi)不(bu)可(ke)缺少的,考慮(lv)到各個(ge)系統(tong)中分號(hao)會有(you)不(bu)同的意義,所以(yi)前面(mian)加反斜杠。

{}   花括號代(dai)表前面find查找出來的文件名(ming)。

使用find時,只(zhi)要把(ba)想要的(de)操作寫在(zai)一(yi)個文件里,就可以用exec來(lai)配(pei)合(he)find查找,很(hen)方便的(de)在有些操作系(xi)統中只允許-exec選(xuan)(xuan)項(xiang)執(zhi)行諸如l s或ls -l這樣的命令(ling)(ling)(ling)(ling)。大多(duo)數(shu)用戶使(shi)用這一(yi)(yi)(yi)選(xuan)(xuan)項(xiang)是為(wei)了(le)(le)查(cha)找舊(jiu)文(wen)件(jian)(jian)(jian)并刪除它(ta)們。建議在真正(zheng)執(zhi)行rm命令(ling)(ling)(ling)(ling)刪除文(wen)件(jian)(jian)(jian)之前,最好先用ls命令(ling)(ling)(ling)(ling)看一(yi)(yi)(yi)下(xia),確認(ren)它(ta)們是所(suo)要(yao)(yao)刪除的文(wen)件(jian)(jian)(jian)。 exec選(xuan)(xuan)項(xiang)后(hou)面跟隨(sui)著所(suo)要(yao)(yao)執(zhi)行的命令(ling)(ling)(ling)(ling)或腳本,然(ran)后(hou)是一(yi)(yi)(yi)對(dui)兒{ },一(yi)(yi)(yi)個空格和(he)一(yi)(yi)(yi)個\,最后(hou)是一(yi)(yi)(yi)個分號。為(wei)了(le)(le)使(shi)用exec選(xuan)(xuan)項(xiang),必(bi)須(xu)要(yao)(yao)同時使(shi)用print選(xuan)(xuan)項(xiang)。如果驗證一(yi)(yi)(yi)下(xia)find命令(ling)(ling)(ling)(ling),會發現該(gai)命令(ling)(ling)(ling)(ling)只輸出(chu)從(cong)當前路徑起(qi)的相對(dui)路徑及文(wen)件(jian)(jian)(jian)名

實例1:ls -l命(ming)令放在find命(ming)令的-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]#

說明: 

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

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

命(ming)令:

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

輸出(chu):

[root@localhost test]# ll

總計(ji) 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]# 

說明

在shell中用任何(he)方式刪(shan)除(chu)文(wen)(wen)件(jian)(jian)之(zhi)前(qian),應當(dang)先查看相應的文(wen)(wen)件(jian)(jian),一定要(yao)小心!當(dang)使用諸如mv或(huo)rm命令時(shi),可(ke)以(yi)使用-exec選(xuan)項(xiang)的安全(quan)模式。它將在對每個匹配(pei)到的文(wen)(wen)件(jian)(jian)進(jin)行操作之(zhi)前(qian)提示你。 

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

命令:

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

輸(shu)出:

[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

總(zong)計 312

-rw-r--r-- 1 root root 302108 11-03 06:19&nbsp;log2012.log

drwxr-xr-x 6&nbsp;root root &nbsp; 4096 10-27 01:58 scf

drwxrwxrwx 2 root root&nbsp;  ;4096 11-12 19:32 test3

drwxrwxrwx 2 root root  &nbsp;4096 11-12 19:32 test4

[root@localhost test]#

說明

面(mian)的(de)例子中, find命令(ling)在當前目錄中查找(zhao)所(suo)有(you)文件名(ming)以(yi).log結尾、更改時間(jian)在(zai)5日以上的(de)文(wen)件,并刪(shan)(shan)除(chu)它們,只不過在(zai)刪(shan)(shan)除(chu)之前先給出提示。 按y鍵(jian)刪(shan)(shan)除(chu)文(wen)件,按n鍵(jian)不刪(shan)(shan)除(chu)。 

 

實例4:-exec使用grep命令

命令:

find /etc -name "passwd*" -exec&nbsp;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]#

說明:

任何形式的命令都可(ke)以在-exec選(xuan)項中(zhong)使(shi)用。 ; 在面(mian)的(de)例(li)子中我(wo)們使(shi)用grep命令。find命令首先匹配所有文件名(ming)為“ passwd*”的(de)文件,例(li)如passwd、passwd.old、passwd.bak,然后執行grep命令看(kan)看(kan)在這些(xie)文件中是否存(cun)在一個root用戶。

實例5:查找文(wen)件移動到指定目錄  

命令:

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

輸出:

[root@localhost test]# ll

總計 12drwxr-xr-x 6 root root 4096&nbsp;10-27 01:58 scf

drwxrwxr-x 2 root root&nbsp;4096 11-12 22:49 test3

drwxrwxr-x 2 root root 4096&nbsp;11-12 19:32 test4

[root@localhost test]# cd test3/

[root@localhost test3]# ll

總計 304

-rw-r--r--&nbsp;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  &nbsp;   0 11-12 22:25 log2014.log

[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;

[root@localhost test3]# ll

總(zong)計 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計(ji) 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root&nbsp;root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root      0&nbsp;11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58&nbsp;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]#

實例(li)6:用exec選(xuan)項執行cp命令  

命(ming)令:

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

輸出:

[root@localhost test3]# ll

總計(ji) 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&nbsp;root     61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root  &nbsp;  &nbsp;0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096&nbsp;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” 為同一(yi)文件(jian)

cp: “./test3/log2013.log” 及 “test3/log2013.log” 為(wei)同一文件(jian)

cp: “./test3/log2012.log” 及 “test3/log2012.log” 為同一文件

[root@localhost test]# cd test3

[root@localhost test3]# ll

總計(ji) 304

-rw-r--r-- 1 root root&nbsp;302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54&nbsp;log2013.log

-rw-r--r-- 1 root root     &nbsp;0 11-12 22:54 log2014.log

[root@localhost test3]#

posted @ 2012-11-14 08:02  peida  閱讀(149317)  評論(12)    收藏  舉報