Linux中find命令,與exec合用,按修改時間查詢等
find是我們很常用的一個Linux命令,但是我們一般查找出來的並不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。
exec解釋:
-exec 引數後面跟的是command命令,它的終止是以;為結束標誌的,所以這句命令後面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜槓。
{} 花括號代表前面find查找出來的檔名。
使用find時,只要把想要的操作寫在一個檔案裡,就可以用exec來配合find查詢,很方便的。在有些作業系統中只允許-exec選項執行諸如l s或ls -l這樣的命令。大多數使用者使用這一選項是為了查詢舊檔案並刪除它們。建議在真正執行
例項1:ls -l命令放在find命令的-exec選項中
命令:
find . -type f -exec ls -l {} \;
輸出:
[[email protected] test]#find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 127 10-28 16:51 ./
-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 ./
-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 {} \;
輸出:
[[email protected] 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 {} \;
[[email protected] 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中用任何方式刪除檔案之前,應當先檢視相應的檔案,一定要小心!當使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的檔案進行操作之前提示你。
例項3:在目錄中查詢更改時間在n日以前的檔案並刪除它們,在刪除之前先給出提示
命令:
find . -name "*.log" -mtime +5 -ok rm {} \;
輸出:
[[email protected] 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]#
說明:
在上面的例子中, find命令在當前目錄中查詢所有檔名以.log結尾、更改時間在5日以上的檔案,並刪除它們,只不過在刪除之前先給出提示。按y鍵刪除檔案,按n鍵不刪除。
例項4:-exec中使用grep命令
命令:
find /etc -name "passwd*" -exec grep "root" {} \;
輸出:
[[email protected] 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選項中使用。在上面的例子中我們使用grep命令。find命令首先匹配所有檔名為“ passwd*”的檔案,例如passwd、passwd.old、passwd.bak,然後執行grep命令看看在這些檔案中是否存在一個root使用者。
例項5:查詢檔案移動到指定目錄
命令:
find . -name "*.log" -exec mv {} .. \;
輸出:
[[email protected] 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/
[[email protected] 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 {} .. \;
[[email protected] test3]#ll
總計 0[[email protected] test3]#cd ..
[[email protected] 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]#
例項6:用exec選項執行cp命令
命令:
find . -name "*.log" -exec cp {} test3 \;
輸出:
[[email protected] test3]#ll
總計 0[[email protected] test3]#cd ..
[[email protected] 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]#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
[[email protected] 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]#
另外:find中搭配atime、ctime和mtime的使用方法介紹如下:
atime:訪問時間(accesstime),指的是檔案最後被讀取的時間,可以使用touch命令更改為當前時間;
ctime:變更時間(changetime),指的是檔案本身最後被變更的時間,變更動作可以使chmod、chgrp、mv等等;
mtime:修改時間(modifytime),指的是檔案內容最後被修改的時間,修改動作可以使echo重定向、vi等等;
以下例子應該很容易理解上述三個時間:某使用者在2013年1月5日00:00:00時,在/home下輸入ping www.baidu.com> ping.log;5秒鐘後,該使用者使用ctrl+C強制關閉該命令;5秒鐘後,使用cat ping.log檢視。則ping.log的ctime為2013-01-05 00:00:00;mtime為2013-01-05 00:00:05;atime為2013-01-05 00:00:10。
這三個引數理解後,我們就可以使用find找到某個時刻進行過某類操作的檔案集合。
find . {-atime/-ctime/-mtime/-amin/-cmin/-mmin} [-/+]num
第一個引數,.,代表當前目錄,如果是其他目錄,可以輸入絕對目錄和相對目錄位置;
第二個引數分兩部分,前面字母a、c、m分別代表訪問、變更、修改,後面time為日期,min為分鐘,注意只能以這兩個作為單位;
第三個引數為量,其中不帶符號表示符合該數量的,帶-表示符合該數量以後的,帶+表示符合該數量以前的。
注意:find中對於時間的推算均為:
1、到......為止用+號,從......開始用-號,一個時間單位內的不帶符號;
2、數字代表往前偏移量;
3、當前到往後的一個時間單位為基準0;-0就是下限單位;+0就是上限單位。
假設當前時間是2013年1月5日00:00:00分,則:
1、尋找2013年1月5日00:00:00到2013年1月5日23:59:59被訪問過的檔案:時間單位是day,查詢單位時間內的不帶符號,當前無往前偏移,因此查詢語句為:
find . -atime 0
2、尋找2013年1月4日23:50:00到2013年1月4日23:55:00被修改過的檔案:時間單位是min,下限從當前往前偏移10min,上限從當前往前偏移5min,因此查詢語句為:
find . -mmin -10 -mmin +5