系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > Linux find命令-exec參數(shù)的作用

Linux find命令中-exec參數(shù)的作用(2)

時(shí)間:2014-12-31 18:02:54 作者:qipeng 來源:系統(tǒng)之家 1. 掃描二維碼隨時(shí)看資訊 2. 請(qǐng)使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20141231/33889.html 手機(jī)查看 評(píng)論

  代碼如下:

  [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.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

 。踨oot@localhost test]# find 。 -type f -mtime +14 -exec rm {} \;

 。踨oot@localhost test]# ll

  總計(jì) 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中用任何方式刪除文件之前,應(yīng)當(dāng)先查看相應(yīng)的文件,一定要小心!當(dāng)使用諸如mv或rm命令時(shí),可以使用-exec選項(xiàng)的安全模式。它將在對(duì)每個(gè)匹配到的文件進(jìn)行操作之前提示你。

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

  命令:

  find 。 -name “*.log” -mtime +5 -ok rm {} \;

  輸出:

  代碼如下:

 。踨oot@localhost test]# ll

  總計(jì) 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

 。踨oot@localhost test]# find 。 -name “*.log” -mtime +5 -ok rm {} \;

  《 rm 。。。 。/log_link.log 》 ? y

  《 rm 。。。 。/log2012.log 》 ? n

 。踨oot@localhost test]# ll

  總計(jì) 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

 。踨oot@localhost test]#

  說明:

  在上面的例子中, find命令在當(dāng)前目錄中查找所有文件名以.log結(jié)尾、更改時(shí)間在5日以上的文件,并刪除它們,只不過在刪除之前先給出提示。按y鍵刪除文件,按n鍵不刪除。

  實(shí)例4:-exec中使用grep命令

  命令:

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

  輸出:

  代碼如下:

 。踨oot@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]#

  說明:

  任何形式的命令都可以在-exec選項(xiàng)中使用。在上面的例子中我們使用grep命令。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個(gè)root用戶。

發(fā)表評(píng)論

0

沒有更多評(píng)論了

評(píng)論就這些咯,讓大家也知道你的獨(dú)特見解

立即評(píng)論

以上留言僅代表用戶個(gè)人觀點(diǎn),不代表系統(tǒng)之家立場(chǎng)

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄