http://blog.chinaunix.net/uid-7340476-id-225283.htmlphp
find命令主要用來在硬盤上搜索文件, find命令主要用於文件查找,列出當前目錄及子目錄下全部的文件和文件夾css
格式:find path -option "keyword" [-print] [-exec -ok command] {} \;html
path: 查找路徑 ;該命令用於在指定路徑中查找符合條件的文件,搜索路徑可以是多個目錄,不一樣目錄之間用空格分割java
-option: 選項 node
"keyword": 關鍵字 mysql
command: 須要執行的命令linux
-exec command] {} \;將查到的文件執行command操做,注意{}和\;之間有空格web
-ok和-exec的做用同樣,只是-ok在執行前會詢問用戶是否執行操做正則表達式
find . -name "*" | xargs grep -i "passwd"(本身工做中用的最多的實例,-i的做用是不區分大小寫)sql
find -name "t*" -perm 744 //查找當前目錄下文件名以t開頭的,且文件屬性主具備讀、寫、執行權限的文件。。。。
find還有-exec選項,對匹配文件執行該參數過給出的shell命令。
例如:find /etc/ -type f -name "rc*" -exec ls -l {} \; //注意{}和\之間有空格。。。
因而可知:能夠接多個選項參數
目錄路徑:表示以此目錄做爲根目錄逐級往下搜索
1.目錄介紹:
若是find不指定目錄,則默認從當前所在的目錄開始搜索
$find
$find .
以上兩個結果同樣
. 一個點表示當前目錄,也可使用 ./ 來表示;
.. 兩個點表示父目錄,也能夠 ../ 來表明。
若是須要從根目錄開始查找:/
find . 遍歷輸出當前目錄下的全部文件(夾)及子文件(夾)
find / 遍歷輸出根目錄下的全部文件(夾)及子文件(夾)
find ./ 遍歷輸出當前目錄的下一級路徑的全部文件(夾)及子文件(夾)
也能夠是/opt/qmfsun之類的路徑
find中的目錄能夠指定多個搜索目錄
$ind /usr /home /tmp -name "*.java";在/usr /home /tmp三個目錄中查找以.java結尾的文件
若是對某個目錄沒有訪問權限的話,就會報錯,提示: find: /tmp/qmfsun:Permission denide
2. 須要搜索的關鍵字
3.主要選項參數:
注意:
a)每個選項前面跟隨一個橫槓-
$find /doc -name '*bak' -exec rm -rf {} \; //從 /doc 目錄開始往下找,找到凡是文件名結尾爲 bak的文件,把它刪除掉。
注意:-exec 選項是執行的意思,rm -rf是刪除命令,{ } 表示文件名,「\;」是規定的命令結尾。
find命令默認狀況下是區分大小寫的,能夠經過-iname或者在grep中添加-i參數來忽略大小寫
-name :指定按照文件名查找文件,查找時文件名大小寫敏感。只能搜索到文件名,若是須要搜索文件內容裏包含的特定字符串,須要用grep(用的最多見)
-iname: 查找時不區分文件名大小寫
$ find . -iname U*
users users2
#若是執行find . -name U*將不會找到匹配的文件
find -iname "MyCProgram.c";全部不區分大小寫的文件名爲「MyCProgram.c」的文件
查找當前用戶主目錄下的全部文件:下面兩種方法均可以使用
find -perm,根據文件的權限來查找文件,有三種形式,是位"與", + /是位"或"
find -perm mode
find -perm -mode
find -perm /mode(+符號的做用與 / 符號相同,可是如今新版 GNU findutils 中不支持使用該符號)
三者區別:
-perm按文件權限查找。例如:-perm -777, -perm –a+x(user, group, other 都具備write屬性)
find plsql -type f -perm -ug=rw -exec ls -l {} \; 2>/dev/null //將查找可由「other」和組寫入的文件
或者
find plsql -type f -perm -220 -exec ls -l {} \; 2>/dev/null
-rw-rw-rw- 1 bluher users 4303 Jun 7 2004 plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan 12 2005 plsql/FORALLSample/doc/readme.html
-rw-rw-rw- 1 bluher users 22647 Jan 12 2005
find plsql -type f -perm /ug=rw -exec ls -l {} \; 2>/dev/null //查找由用戶、組或兩者共同寫入的文件:
或者
find plsql -type f -perm /220 -exec ls -l {} \; 2>/dev/null
-rw-r--r-- 1 bluher users 21473 May 3 16:02 plsql/regexpvalidate.zip
-rw-rw-rw- 1 bluher users 4303 Jun 7 2004 plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan 12 2005 plsql/FORALLSample/doc/readme.html
-rw-rw-rw- 1 bluher users 22647 Jan 12 2005 plsql/FORALLSample/src/config.sql
find /etc -perm 640 精確匹配,其權限必須是640
find /etc -perm /640三組權限中有任意一組匹配都行
find /etc -perm -640含有該權限的都得匹配
-perm -222 可查找出666,只要含有222權限的均可以
-perm -400只要屬主有讀權限便可,其餘任意權限
-perm /400屬主有讀權限,其餘沒有任何權限;符合這三組均可
find . -perm 700 是說剛好爲 700, rwx------
find . -perm -700 是說第一組知足 7 就能夠了,後兩組無所謂,所以 rwx------ 和 rwxrwxrwx 都會入選
find . -perm +700 是說第一組每一位有一個知足就能夠了,所以 r-x------ 也會入選,範圍又擴大不少
# find . -perm 111 -print //表示在當前目錄下搜尋全部者、同組成員及其餘成員均爲可執行權限的文件及文件夾
# find . -perm -111 -print //表示在當前目錄下搜尋全部者、同組成員及其餘成員均含有可執行權限的文件及文件夾
#find . -perm /111 -print //表示在當前目錄下搜尋全部者、同組成員及其餘成員中至少一個角色含有可執行權限的文件及文件夾
+ 針對 三個權限位中的任意 一位
- 針對 三個權限位中的所有 三位
MODE 也是針對三位的
[root@localhost ~]# ls -lh test
total 0
-r--r--r-- 1 root root 0 Oct 10 20:50 test1
-rwxr--r-- 1 root root 0 Oct 10 20:50 test2
-rw-r--r-- 1 root root 0 Oct 10 20:50 test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test4
[root@localhost ~]# find test -perm 644 | xargs ls -hld
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
[root@localhost ~]# find test -perm +644 |xargs ls -lhd
drwxr-xr-x 2 root root 1.0K Oct 10 20:50 test //找到這個,是由於find中沒有指明查找的類型,即沒有-type f的緣由
-r--r--r-- 1 root root 0 Oct 10 20:50 test/test1
-rwxr--r-- 1 root root 0 Oct 10 20:50 test/test2
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
[root@localhost ~]# find test -perm -644 | xargs ls -lhd
drwxr-xr-x 2 root root 1.0K Oct 10 20:50 test
-rwxr--r-- 1 root root 0 Oct 10 20:50 test/test2
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
-rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
能夠看到 a ab abc 的權限分別爲600 640 666
find -perm 640 是作精確匹配,只會匹配到640即 ab
find -perm -640 作比640更充足的匹配,固然666是知足的,即 ab abc
find -perm /640 是要任意的一組權限中1的位置上有一個符合便可,所以a ab abc 都會匹配出來
若是在查找文件時但願忽略某個目錄,由於你知道那個目錄中沒有你所要查找的文件,那麼可使用-prune選項來指出須要忽略的目錄。在使用-prune選項時要小心,由於若是你同時使用了-depth選項,那麼-prune選項就會被find命令忽略。
find /apps -path "/apps/bin" -prune -o -print;但願在/apps目錄下查找文件,但不但願在/apps/bin目錄下查找
find /usr/sam -path "/usr/sam/dir1" -prune -o -print;好比要在/usr/sam目錄下查找不在dir1子目錄以內的全部文件
find命令和and(多個參數選項鍊接符),or搭配使用
b)這些選項能夠多個一塊兒用
$find -name "t*" -perm 744;查找當前目錄下文件名以t開頭的,且文件屬性主具備讀、寫、執行權限的文件。。。。
$find /etc/ -type f -name "rc*" -exec ls -l {} \; (find還有-exec選項,對匹配文件執行該參數過給出的shell命令。)
$find /doc -user jacky -name 'j*' //從 /doc 目錄開始往下找,找屬主爲jacky 的、文件名開頭是 j的文件。
多條件查找:條件間的邏輯關係
並關係:-a
或關係:-o
非關係:!或者-not
例如:find /tmp -name "passwd" -user root(默認並關係)
-a
-o
!
避開多個文件夾
\ 表示轉義字符,即指示 shell 不對後面的字符做特殊解釋,轉義字符。
查找某一肯定文件,-name等選項加在-o 以後
在linux find 進行查找的時候,有時候須要忽略某些目錄不查找,可使用 -prune 參數來進行過濾,但必需要注意要忽略的路徑參數必須緊跟着搜索的路徑以後,不然該參數沒法起做用。
如下是指定搜索/home/carryf目錄下的全部文件,可是會忽略/home/carryf/astetc的路徑:
find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -print
若是按照文件名來搜索則爲:
find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -name "cdr_*.conf" -print
若是要忽略兩個以上的路徑如何處理?
find /home/carryf /( -path "/home/carryf/astetc" -o -path "/home/carryf/etc" /) -prune -o -type f -print
find /home/carryf /( -path "/home/carryf/astetc" -o -path "/home/carryf/etc" /) -prune -o -type f -name "cdr_*.conf" -print
注意/( 和/) 先後都有空格。
查找某個文件包含內容,下面這個語句能夠解決目錄帶空格的問題:
find ./ -name "mysql*" -print0 |xargs -0 grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"
若是目錄不帶空格,那麼能夠以下面的形式執行:
find ./ -name "mysql*" |xargs grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"
-amin n 查找系統中最後N分鐘訪問的文件
-atime n 查找系統中最後n*24小時訪問的文件
-cmin n 查找系統中最後N分鐘被改變文件狀態的文件
-ctime n 查找系統中最後n*24小時被改變文件狀態的文件
-mmin n 查找系統中最後N分鐘被改變文件數據的文件
-mtime n 查找系統中最後n*24小時被改變文件數據的文件
解釋什麼是atime ctime mtime
atime (access time):最後一次訪問文件的時間
mtime(medify time):最後一次修改文件的時間
ctime(change time):最後一次改變文件(改變的是原數據即屬性)的時間
如:記錄該文件的inode節點被修改的時間。touch 命令除了-d -t選項外都會改變改時間,並且chmod,chown等命令也能改變該值
三者之間的關係
當修改mtime時,ctime必須隨着改變,由於文件大小等屬性;有人說atime 也必定會改變,要想修改文件必須先訪問;實際上是不對的,沒必要訪問文件就能修改內容:如#echo "change it" >> /etc/inittab ,inittab文件內容會改變,但並無訪問文件,因此atime沒有改變
查看三者的命令
stat filename 能夠查看三者的時間值
ls -l filename 查看文件修改時間
ls -lc filename 查看文件狀態改動時間
ls -lu filename 查看文件訪問時間
-size +2M大於2M的文件
-size -1k小於1k的
-size 2M介於2M正負1M範圍內的文件
$ find
/ -
type
f -name *.zip -size +100M -
exec
rm
-i {} \;(刪除大於100M的*.zip文件)
-exec 操做容許 find 在它遇到的文件上執行任何 shell 命令。大括號容許移動每一個空文件。
做用:用戶使用這一選項是爲了查找到舊文件並查看,刪除它或者作其餘的操做
exec和ok:
選項後面跟隨着所要執行的命令或腳本,而後是一對兒{ },一個空格和一個\,最後是一個分號。若是驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名。任何形式的命令均可以在-exec選項中使用。
eg:
-prune剔除某個文件或者文件夾
$find . –path 「/DSF」 –prune –o –print
$find . \( -path 「./DSF」 –o 「./file1」 \) –prune –o –perm -444 -print
注意:-o 是或者,以此類推也能夠用-a,-a是而且的意思。可根據相應的狀況用-o 或者-a
實例1:ls -l命令放在find命令的-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]#
說明:
上面的例子中,find命令匹配到了當前目錄下的全部普通文件,並在-exec選項中使用ls -l命令將它們列出。
實例2:在目錄中查找更改時間在n日之前的文件並刪除它們
命令:
find . -type f -mtime +14 -exec rm {} \;
輸出:
[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]#
說明:
在shell中用任何方式刪除文件以前,應當先查看相應的文件,必定要當心!當使用諸如mv或rm命令時,可使用-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
[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" {} \;
輸出:
[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]#
說明:
任何形式的命令均可以在-exec選項中使用。 在上面的例子中咱們使用grep命令。find命令首先匹配全部文件名爲「 passwd*」的文件,例如passwd、passwd.old、passwd.bak,而後執行grep命令看看在這些文件中是否存在一個root用戶。
實例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]#
實例6:用exec選項執行cp命令
命令:
find . -name "*.log" -exec cp {} test3 \;
輸出:
[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]# 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
[root@localhost 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]#
實例1: 查找系統中的每個普通文件,而後使用xargs命令來測試它們分別屬於哪類文件
命令:
find . -type f -print | xargs file
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 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
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -type f -print | xargs file
./log2014.log: empty
./log2013.log: empty
./log2012.log: ASCII text
[root@localhost test]#
實例2:在整個系統中查找內存信息轉儲文件(core dump) ,而後把結果保存到/tmp/core.log 文件中
命令:
find / -name "core" -print | xargs echo "" >/tmp/core.log
輸出:
[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log
[root@localhost test]# cd /tmp
[root@localhost tmp]# ll
總計 16
-rw-r--r-- 1 root root 1524 11-12 22:29 core.log
drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766
drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815
drwx------ 2 root root 4096 11-03 07:11 vmware-root
實例3:在當前目錄下查找全部用戶具備讀、寫和執行權限的文件,並收回相應的寫權限
命令:
find . -perm -7 -print | xargs chmod o-w
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 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
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -perm -7 -print | xargs chmod o-w
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 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 19:32 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
執行命令後,文件夾scf、test3和test4的權限都發生改變
實例4:用grep命令在全部的普通文件中搜索hostname這個詞
命令:
find . -type f -print | xargs grep "hostname"
輸出:
[root@localhost test]# find . -type f -print | xargs grep "hostname"
./log2013.log:hostnamebaidu=baidu.com
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#
實例5:用grep命令在當前目錄下的全部普通文件中搜索hostnames這個詞
命令:
find . -name \* -type f -print | xargs grep "hostnames"
輸出:
[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#
說明:
注意,在上面的例子中, \用來取消find命令中的*在shell中的特殊含義。
實例6:使用xargs執行mv
命令:
find . -name "*.log" | xargs -i mv {} test4
輸出:
[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:54 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計 0[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4/
[root@localhost test4]# 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 test4]#
實例7:find後執行xargs提示xargs: argument line too long解決方法:
命令:
find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
輸出:
[root@pd test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f
[root@pdtest4]#
說明:
-l1是一次處理一個;-t是處理以前打印出命令
實例8:使用-i參數默認的前面輸出用{}代替,-I參數能夠指定其餘代替字符,如例子中的[]
命令:
輸出:
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4
[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..
[root@localhost test4]# 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 test4]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#
說明:
使用-i參數默認的前面輸出用{}代替,-I參數能夠指定其餘代替字符,如例子中的[]
實例9:xargs的-p參數的使用
命令:
find . -name "*.log" | xargs -p -i mv {} ..
輸出:
[root@localhost test3]# ll
總計 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:06 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test3
[root@localhost test3]# find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#
find命令查找實例:
查找2004-11-30 16:36:37時更改過的文件
# A=`find ./ -name "*php"` | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"
將find出來的東西拷到另外一個地方
find *.c -exec cp '{}' /tmp ;
好比要查找磁盤中大於3M的文件:
find . -size +3000k -exec ls -ld {} ;
在/tmp中查找全部的*.h,並在這些文件中查找「SYSCALL_VECTOR",最後打印出全部包含"SYSCALL_VECTOR"的文件名
A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} \; -print
find /tmp -name tmp.txt -exec cat {} \;
$find . -name "yao*" | xargs file
$find . -name "yao*" | xargs echo "" > /tmp/core.log
find -name april* 在當前目錄下查找以april開始的文件
find -name april* fprint file 在當前目錄下查找以april開始的文件,並把結果輸出到file中
find -name ap* -o -name may* 查找以ap或may開頭的文件
find /mnt -name tom.txt -ftype vfat 在/mnt下查找名稱爲tom.txt且文件系統類型爲vfat的文件
find /mnt -name t.txt ! -ftype vfat 在/mnt下查找名稱爲tom.txt且文件系統類型不爲vfat的文件
find /tmp -name wa* -type l 在/tmp下查找名爲wa開頭且類型爲符號連接的文件
find /home -mtime -2 在/home下查最近兩天內改動過的文件
find /home -atime -1 查1天以內被存取過的文件
find /home -mmin +60 在/home下查60分鐘前改動過的文件
find /home -amin +30 查最近30分鐘前被存取過的文件
find /home -newer tmp.txt 在/home下查更新時間比tmp.txt近的文件或目錄
find /home -anewer tmp.txt 在/home下查存取時間比tmp.txt近的文件或目錄
find /home -used -2 列出文件或目錄被改動過以後,在2日內被存取過的文件或目錄
find /home -user cnscn 列出/home目錄內屬於用戶cnscn的文件或目錄
find /home -uid +501 列出/home目錄內用戶的識別碼大於501的文件或目錄
find /home -group cnscn 列出/home內組爲cnscn的文件或目錄
find /home -gid 501 列出/home內組id爲501的文件或目錄
find /home -nouser 列出/home內不屬於本地用戶的文件或目錄
find /home -nogroup 列出/home內不屬於本地組的文件或目錄
find /home -name tmp.txt -maxdepth 4 列出/home內的tmp.txt 查時深度最多爲3層
find /home -name tmp.txt -mindepth 3 從第2層開始查
find /home -empty 查找大小爲0的文件或空目錄
find /home -size +512k 查大於512k的文件
find /home -size -512k 查小於512k的文件
find /home -links +2 查硬鏈接數大於2的文件或目錄
find /home -perm 0700 查權限爲700的文件或目錄
find /tmp -name tmp.txt -exec cat {} \;
find /tmp -name tmp.txt -ok rm {} \;
find / -amin -10 # 查找在系統中最後10分鐘訪問的文件
find / -atime -2 # 查找在系統中最後48小時訪問的文件
find / -empty # 查找在系統中爲空的文件或者文件夾
find / -group cat # 查找在系統中屬於 groupcat的文件
find / -mmin -5 # 查找在系統中最後5分鐘裏修改過的文件
find / -mtime -1 #查找在系統中最後24小時裏修改過的文件
find / -nouser #查找在系統中屬於做廢用戶的文件
find / -user fred #查找在系統中屬於FRED這個用戶的文件
$find /etc -name "passwd*" -exec grep "cnscn" {} \; #看是否存在cnscn用戶
|
find命令的複雜查找
#find . -size -10c –print | ls –l //在當前目錄下查找100~200塊長的文件並顯示文件的實際塊數。
$find ./ -perm -002 -exec mv {} {}.old \; //將查找到文件的名字加上.old(至關於重命名)
1)在/tmp中查找全部的*.h,並在這些文件中查找「SYSCALL_VECTOR",最後打印出全部包含"SYSCALL_VECTOR"的文件名
A)find /tmp -name "*.h" | xargs n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} \;
# A='find ./ -name "*php"' | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37 //查找2004-11-30 16:36:37時更改過的文件
$find / -name access_log 2 >/dev/null //無錯誤查找,全部的錯誤信息都被輸入到/dev/null 目錄
$ find . –print
-print指明打印出匹配文件的文件名(路徑),’\n’做爲分割符。使用-print0可指明使用’\0’做爲分割符。
咱們可根據文件名進行搜索,如搜索以.txt結尾的文件名:
$ find . –name 「*.txt」 –print
另外還有一個-iname選項,與-name選項的區別僅在於-iname選項忽略字母大小寫。
若是想匹配多個條件中的一個,能夠採用OR條件操做:
$ find . \( –name 「*.txt」 –o –name 「*.pdf」 \) –print
./new.txt
./new.pdf
上面的代碼打印出全部的.txt和.pdf文件。\(以及\)用於將它們之中的內容視爲一個總體。
選項-path(一樣也有-ipath)將文件路徑做爲一個總體進行匹配,如:
$ find . –path 「*new*」 –print
./new.txt
./new.pdf
./new.py
選項-regex的參數和-path的相似,只不過-regex(一樣也有-iregex)是基於正則表達式來匹配文件路徑的。
$ find . –regex 「.*\(\.txt\|\.pdf\)$」 –print
./new.txt
./new.pdf
find也能夠用」!」否認參數的含義,如:
$ find . ! –name 「*.txt」 –print
.
./new.pdf
./new.py
find命令在使用時會遍歷全部的子目錄。但可使用-maxdepth和-mindepth來限制find命令遍歷的深度。如:
$ find . maxdepth 1 –print
$ find . mindepth 2 –print
-maxdepth和-mindepth應該做爲find的第3個參數出現。若是做爲第4個或以後的參數,就可能會影響到find的效率,由於它不得不進行一些沒必要要的檢查。
Linux中文件具備不一樣的類型,例如普通文件、目錄、字符設備、塊設備、符號連接、硬連接、套接字以及FIFO等。Find命令中的-type能夠對文件搜索進行過濾。如:
$ find . –type d –print #列出全部的目錄
$ find . –type f –print #列出普通文件
$ find . –type l –print #列出符號連接
$ find . –type c –print #列出字符設備
$ find . –type b –print #列出塊設備
$ find . –type s –print #列出套接字
$ find . –type p –print #列出FIFO
Linux文件系統中的每個文件都有三種時間戳:
l 訪問時間(-atime):用戶最近一次訪問文件的時間。
l 修改時間(-mtime):文件內容最後一次被修改的時間。
l 變化時間(-ctime):文件元數據(例如權限或全部權)最後一次改變的時間。
-atime、-mtime、-ctime可做爲find的時間參數,它們能夠整數值給出,單位是天。這些整數值還能夠帶有-或+,如:
$ find . type f –atime -7 -print #打印最近7天被訪問過的全部文件
$ find . type f –atime 7 –print #打印剛好在7天前被訪問過的全部文件
$ find . type f –atime +7 –print #打印出訪問時間超過7天的全部文件
另外,還有以分鐘做爲計量單位的,包括:
l -amin(訪問時間)
l -mmin(修改時間)
l -cmin(變化時間)
find命令還有一個-newer參數,使用-newer,能夠指定一個用於比較時間戳的參考文件,如:
$ find . –type f –newer new.txt –print #打印比file.txt修改時間更新的全部文件
find還能夠根據文件大小搜索:
$ find . –type f –size +2k #大於2KB的文件
$ find . –type f –size -2k #小於2KB的文件
$ find . –type f –size 2k #大小等於2KB的文件
除了k以外,還有:
l b——塊(512字節)。
l C——字節。
l w——字(2字節)。
l k——千字節。
文件匹配還能夠根據文件權限進行,如:
$ find . –type f –perm 644 –print #打印出權限爲644的文件
還能夠用該方法找出那些沒有設置好權限執行權限的PHP文件:
$ find . type f –name 「*.php」 ! –perm 644 –print
也能夠根據文件的全部權進行搜索,如:
$ find . –type f –user baojie –print #打印用戶baojie擁有的全部文件
find命令能夠藉助選項-exec與其餘命令進行結合,如將全部.py添加可執行權限:
$ find . –name 「*.py」 –exec chmod +x {} \;
{}是一個特殊的字符串,與-exec選項結合使用。對於每個匹配的文件,{}會被替換成響應的文件名。
沒法在-exec參數中直接使用多個命令,但能夠把多個命令寫到一個shell腳本中,而後再-exec中使用這個腳本。
-exec可以同printf結合起來生成有用的輸出信息。例如:
$ find . –name 「*.txt」 –exec printf 「Text file: %s\n」 {} \;
find命令在執行搜索時還能夠跳過一些子目錄,以下示例:
$ find . \( -name 「old」 –prune \) –o \( -type f –print \)
以上命令打印出不包括在old目錄中的全部文件的名稱。
使用find命令在linux系統中查找文件時,有時須要忽略某些目錄,可使用 -prune 參數來進行過濾。 不過必須注意:要忽略的路徑參數要緊跟着搜索的路徑以後,不然該參數沒法起做用。
例如:指定搜索/home/zth目錄下的全部文件,可是會忽略/home/zth/astetc的路徑:
按照文件名來搜索則爲:
要忽略兩個以上的路徑如何處理?
注意:/( 和/) 先後都有空格。
查找某個文件包含內容,如下語句能夠解決目錄帶空格的問題:
若是目錄不帶空格,能夠這樣:
經過以上的例子,你們應該能夠掌握find命令查找文件時,忽略相關目錄的方法了。
若是想同時刪除A和B文件則能夠用-o 鏈接條件 find -name "*" -o -name "A" -o -name "B" -newer A ! -newer B -exec rm -f {} \;