Linux下find命令在目錄結(jié)構(gòu)中搜索文件迟蜜,并執(zhí)行指定的操作向图。Linux下find命令提供了相當(dāng)多的查找條件,功能很強(qiáng)大迅栅。
由于find具有強(qiáng)大的功能殊校,所以它的選項(xiàng)也很多,其中大部分選項(xiàng)都值得我們花時(shí)間來了解一下读存。
即使系統(tǒng)中含有網(wǎng)絡(luò)文件系統(tǒng)( NFS)为流,find命令在該文件系統(tǒng)中同樣有效,只你具有相應(yīng)的權(quán)限让簿。
在運(yùn)行一個(gè)非常消耗資源的find命令時(shí)敬察,很多人都傾向于把它放在后臺(tái)執(zhí)行,因?yàn)楸闅v一個(gè)大的文件系統(tǒng)可能會(huì)花費(fèi)很長的時(shí)間(這里是指30G字節(jié)以上的文件系統(tǒng))尔当。
1.命令格式:
find pathname -options [-print -exec -ok ...]
2.命令功能:
用于在文件樹種查找文件莲祸,并作出相應(yīng)的處理
3.命令參數(shù):
pathname: find命令所查找的目錄路徑蹂安。例如用.來表示當(dāng)前目錄,用/來表示系統(tǒng)根目錄锐帜。
-print: find命令將匹配的文件輸出到標(biāo)準(zhǔn)輸出田盈。
-exec: find命令對匹配的文件執(zhí)行該參數(shù)所給出的shell命令。相應(yīng)命令的形式為'command' { } ;缴阎,注意{ }和允瞧;之間的空格。
-ok: 和-exec的作用相同蛮拔,只不過以一種更為安全的模式來執(zhí)行該參數(shù)所給出的shell命令述暂,在執(zhí)行每一個(gè)命令之前,都會(huì)給出提示建炫,讓用戶來確定是否執(zhí)行畦韭。
4.命令選項(xiàng):
-name 按照文件名查找文件。
-perm 按照文件權(quán)限來查找文件肛跌。
-prune 使用這一選項(xiàng)可以使find命令不在當(dāng)前指定的目錄中查找艺配,如果同時(shí)使用-depth選項(xiàng),那么-prune將被find命令忽略惋砂。
-user 按照文件屬主來查找文件妒挎。
-group 按照文件所屬的組來查找文件。
-mtime -n +n 按照文件的更改時(shí)間來查找文件西饵, - n表示文件更改時(shí)間距現(xiàn)在n天以內(nèi)酝掩,+ n表示文件更改時(shí)間距現(xiàn)在n天以前。find命令還有-atime和-ctime 選項(xiàng)眷柔,但它們都和-m time選項(xiàng)期虾。
-nogroup 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在驯嘱。
-nouser 查找無有效屬主的文件镶苞,即該文件的屬主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改時(shí)間比文件file1新但比文件file2舊的文件鞠评。
-type 查找某一類型的文件茂蚓,諸如:
b - 塊設(shè)備文件。
d - 目錄剃幌。
c - 字符設(shè)備文件聋涨。
p - 管道文件。
l - 符號鏈接文件负乡。
f - 普通文件牍白。
-size n:[c] 查找文件長度為n塊的文件,帶有c時(shí)表示文件長度以字節(jié)計(jì)抖棘。-depth:在查找文件時(shí)茂腥,首先查找當(dāng)前目錄中的文件狸涌,然后再在其子目錄中查找。
-fstype:查找位于某一類型文件系統(tǒng)中的文件最岗,這些文件系統(tǒng)類型通撑恋ǎ可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統(tǒng)中有關(guān)文件系統(tǒng)的信息般渡。
-mount:在查找文件時(shí)不跨越文件系統(tǒng)mount點(diǎn)惶楼。
-follow:如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件诊杆。
-cpio:對匹配的文件使用cpio命令,將這些文件備份到磁帶設(shè)備中何陆。
另外,下面三個(gè)的區(qū)別:
-amin n 查找系統(tǒng)中最后N分鐘訪問的文件
-atime n 查找系統(tǒng)中最后n*24小時(shí)訪問的文件
-cmin n 查找系統(tǒng)中最后N分鐘被改變文件狀態(tài)的文件
-ctime n 查找系統(tǒng)中最后n*24小時(shí)被改變文件狀態(tài)的文件
-mmin n 查找系統(tǒng)中最后N分鐘被改變文件數(shù)據(jù)的文件
-mtime n 查找系統(tǒng)中最后n*24小時(shí)被改變文件數(shù)據(jù)的文件
5.使用實(shí)例:
實(shí)例1:查找指定時(shí)間內(nèi)修改過的文件
命令:
find -atime -2
輸出:
[root@peidachang ~]# find -atime -2
.
./logs/monitor
./.bashrc
./.bash_profile
./.bash_history
說明:超找48小時(shí)內(nèi)修改過的文件
實(shí)例2:根據(jù)關(guān)鍵字查找
命令:
find . -name "*.log"
輸出:
[root@localhost test]# find . -name "*.log"
./log_link.log
./log2014.log
./test4/log3-2.log
說明:在當(dāng)前目錄查找 以.log結(jié)尾的文件晨汹。 “. “代表當(dāng)前目錄
實(shí)例3:按照目錄或文件的權(quán)限來查找文件
命令:
find /opt/soft/test/ -perm 777
輸出:
[root@localhost test]# find /opt/soft/test/ -perm 777
/opt/soft/test/log_link.log
/opt/soft/test/test4
/opt/soft/test/test5/test3
/opt/soft/test/test3
說明:
查找/opt/soft/test/目錄下 權(quán)限為 777的文件
實(shí)例4:按類型查找
命令:
find . -type f -name "*.log"
輸出:
[root@localhost test]# find . -type f -name "*.log"
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
說明:
查找當(dāng)目錄,以.log結(jié)尾的普通文件
實(shí)例5:查找當(dāng)前所有目錄并排序
命令:
find . -type d | sort
輸出:
[root@localhost test]# find . -type d | sort
.
./scf
./scf/bin
./scf/doc
./scf/lib
實(shí)例6:按大小查找文件
命令:
find . -size +1000c -print
輸出:
[root@localhost test]# find . -size +1000c -print
.
./test4
./scf
./scf/lib
./scf/service
說明:
查找當(dāng)前目錄大于1K的文件
find是我們很常用的一個(gè)Linux命令贷盲,但是我們一般查找出來的并不僅僅是看看而已淘这,還會(huì)有進(jìn)一步的操作,這個(gè)時(shí)候exec的作用就顯現(xiàn)出來了巩剖。
exec解釋:
-exec 參數(shù)后面跟的是command命令铝穷,它的終止是以;為結(jié)束標(biāo)志的,所以這句命令后面的分號是不可缺少的佳魔,考慮到各個(gè)系統(tǒng)中分號會(huì)有不同的意義曙聂,所以前面加反斜杠。
{} 花括號代表前面find查找出來的文件名鞠鲜。
使用find時(shí)宁脊,只要把想要的操作寫在一個(gè)文件里,就可以用exec來配合find查找贤姆,很方便的榆苞。
在有些操作系統(tǒng)中只允許-exec選項(xiàng)執(zhí)行諸如l s或ls -l這樣的命令。
大多數(shù)用戶使用這一選項(xiàng)是為了查找舊文件并刪除它們霞捡。
建議在真正執(zhí)行rm命令刪除文件之前坐漏,最好先用ls命令看一下,確認(rèn)它們是所要?jiǎng)h除的文件碧信。
exec選項(xiàng)后面跟隨著所要執(zhí)行的命令或腳本赊琳,然后是一對兒{ },一個(gè)空格和一個(gè)\音婶,最后是一個(gè)分號慨畸。
為了使用exec選項(xiàng),必須要同時(shí)使用print選項(xiàng)衣式。如果驗(yàn)證一下find命令寸士,會(huì)發(fā)現(xiàn)該命令只輸出從當(dāng)前路徑起的相對路徑及文件名檐什。
實(shí)例1:ls -l命令放在find命令的-exec選項(xiàng)中
命令:
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
說明:
上面的例子中,find命令匹配到了當(dāng)前目錄下的所有普通文件弱卡,并在-exec選項(xiàng)中使用ls -l命令將它們列出乃正。
實(shí)例2:在目錄中查找更改時(shí)間在n日以前的文件并刪除它們
命令:
find . -type f -mtime +14 -exec rm {} \;
輸出:
[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
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
[root@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)的安全模式凡人。它將在對每個(gè)匹配到的文件進(jìn)行操作之前提示你名党。
實(shí)例3:在目錄中查找更改時(shí)間在n日以前的文件并刪除它們,在刪除之前先給出提示
命令:
find . -name "*.log" -mtime +5 -ok rm {} \;
輸出:
[root@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]# find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[root@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
[root@localhost test]#
說明:
在上面的例子中挠轴, find命令在當(dāng)前目錄中查找所有文件名以.log結(jié)尾传睹、更改時(shí)間在5日以上的文件,并刪除它們岸晦,只不過在刪除之前先給出提示欧啤。 按y鍵刪除文件,按n鍵不刪除。
實(shí)例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選項(xiàng)中使用。 在上面的例子中我們使用grep命令痕慢。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd倒慧、passwd.old、passwd.bak包券,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個(gè)root用戶迫靖。
實(shí)例5:查找文件移動(dòng)到指定目錄
命令:
find . -name "*.log" -exec mv {} .. \;
輸出:
[root@localhost test]# ll
總計(jì) 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
總計(jì) 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
總計(jì) 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計(jì) 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]#
實(shí)例6:用exec選項(xiàng)執(zhí)行cp命令
命令:
find . -name "*.log" -exec cp {} test3 \;
輸出:
[root@localhost test3]# ll
總計(jì) 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計(jì) 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
總計(jì) 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命令的-exec選項(xiàng)處理匹配到的文件時(shí), find命令將所有匹配到的文件一起傳遞給exec執(zhí)行兴使。
但有些系統(tǒng)對能夠傳遞給exec的命令長度有限制系宜,這樣在find命令運(yùn)行幾分鐘之后,就會(huì)出現(xiàn)溢出錯(cuò)誤发魄。
錯(cuò)誤信息通常是“參數(shù)列太長”或“參數(shù)列溢出”盹牧。這就是xargs命令的用處所在,特別是與find命令一起使用励幼。
find命令把匹配到的文件傳遞給xargs命令汰寓,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項(xiàng)那樣苹粟。
這樣它可以先處理最先獲取的一部分文件有滑,然后是下一批,并如此繼續(xù)下去嵌削。
在有些系統(tǒng)中毛好,使用-exec選項(xiàng)會(huì)為處理每一個(gè)匹配到的文件而發(fā)起一個(gè)相應(yīng)的進(jìn)程望艺,并非將匹配到的文件全部作為參數(shù)一次執(zhí)行;
這樣在有些情況下就會(huì)出現(xiàn)進(jìn)程過多肌访,系統(tǒng)性能下降的問題找默,因而效率不高;
而使用xargs命令則只有一個(gè)進(jìn)程吼驶。另外惩激,在使用xargs命令時(shí),究竟是一次獲取所有的參數(shù)蟹演,還是分批取得參數(shù)风钻,以及每一次獲取參數(shù)的數(shù)目都會(huì)根據(jù)該命令的選項(xiàng)及系統(tǒng)內(nèi)核中相應(yīng)的可調(diào)參數(shù)來確定。
使用實(shí)例:
實(shí)例1: 查找系統(tǒng)中的每一個(gè)普通文件酒请,然后使用xargs命令來測試它們分別屬于哪類文件
命令:
find . -type f -print | xargs file
輸出:
[root@localhost test]# ll
總計(jì) 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]#
實(shí)例2:在整個(gè)系統(tǒng)中查找內(nèi)存信息轉(zhuǎn)儲(chǔ)文件(core dump) 魄咕,然后把結(jié)果保存到/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
總計(jì) 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
實(shí)例3:在當(dāng)前目錄下查找所有用戶具有讀、寫和執(zhí)行權(quán)限的文件蚌父,并收回相應(yīng)的寫權(quán)限
命令:
find . -perm -7 -print | xargs chmod o-w
輸出:
[root@localhost test]# ll
總計(jì) 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
總計(jì) 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]#
說明:
執(zhí)行命令后,文件夾scf毛萌、test3和test4的權(quán)限都發(fā)生改變
實(shí)例4:用grep命令在所有的普通文件中搜索hostname這個(gè)詞
命令:
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]#
實(shí)例5:用grep命令在當(dāng)前目錄下的所有普通文件中搜索hostnames這個(gè)詞
命令:
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中的特殊含義阁将。
實(shí)例6:使用xargs執(zhí)行mv
命令:
find . -name "*.log" | xargs -i mv {} test4
輸出:
[root@localhost test]# ll
總計(jì) 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
總計(jì) 0[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost test]# ll
總計(jì) 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
總計(jì) 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]#
實(shí)例7:find后執(zhí)行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是一次處理一個(gè)膏秫;-t是處理之前打印出命令
實(shí)例8:使用-i參數(shù)默認(rèn)的前面輸出用{}代替,-I參數(shù)可以指定其他代替字符做盅,如例子中的[]
命令:
find . -name "file" | xargs -I [] cp [] ..
輸出:
[root@localhost test]# ll
總計(jì) 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
總計(jì) 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
總計(jì) 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參數(shù)默認(rèn)的前面輸出用{}代替缤削,-I參數(shù)可以指定其他代替字符,如例子中的[]
實(shí)例9:xargs的-p參數(shù)的使用
命令:
find . -name "*.log" | xargs -p -i mv {} ..
輸出:
[root@localhost test3]# ll
總計(jì) 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[root@localhost test]# ll
總計(jì) 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
總計(jì) 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計(jì) 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]#
說明:
-p參數(shù)會(huì)提示讓你確認(rèn)是否執(zhí)行后面的命令,y執(zhí)行吹榴,n不執(zhí)行亭敢。
find一些常用參數(shù)的一些常用實(shí)例和一些具體用法和注意事項(xiàng)。
1.使用name選項(xiàng):
文件名選項(xiàng)是find命令最常用的選項(xiàng)图筹,要么單獨(dú)使用該選項(xiàng)帅刀,要么和其他選項(xiàng)一起使用。
可以使用某種文件名模式來匹配文件远剩,記住要用引號將文件名模式引起來扣溺。
不管當(dāng)前路徑是什么,如果想要在自己的根目錄$HOME中查找文件名符合*.log的文件瓜晤,
使用~作為 'pathname'參數(shù)锥余,波浪號~代表了你的$HOME目錄。
find ~ -name "*.log" -print
想要在當(dāng)前目錄及子目錄中查找所有的‘ *.log‘文件痢掠,可以用:
find . -name "*.log" -print
想要的當(dāng)前目錄及子目錄中查找文件名以一個(gè)大寫字母開頭的文件驱犹,可以用:
find . -name "[A-Z]*" -print
想要在/etc目錄中查找文件名以host開頭的文件嘲恍,可以用:
find /etc -name "host*" -print
想要查找$HOME目錄中的文件,可以用:
find ~ -name "*" -print 或find . -print
要想讓系統(tǒng)高負(fù)荷運(yùn)行着绷,就從根目錄開始查找所有的文件蛔钙。
find / -name "*" -print
如果想在當(dāng)前目錄查找文件名以一個(gè)個(gè)小寫字母開頭,最后是4到9加上.log結(jié)束的文件:
命令:
find . -name "[a-z]*[4-9].log" -print
輸出:
[root@localhost test]# ll
總計(jì) 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 . -name "[a-z]*[4-9].log" -print
./log2014.log
./log2015.log
./test4/log2014.log
[root@localhost test]#
2.用perm選項(xiàng):
按照文件權(quán)限模式用-perm選項(xiàng),按文件權(quán)限模式來查找文件的話荠医。最好使用八進(jìn)制的權(quán)限表示法吁脱。
如在當(dāng)前目錄下查找文件權(quán)限位為755的文件,即文件屬主可以讀彬向、寫兼贡、執(zhí)行,其他用戶可以讀娃胆、執(zhí)行的文件遍希,可以用:
[root@localhost test]# find . -perm 755 -print
.
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
[root@localhost test]#
還有一種表達(dá)方法:在八進(jìn)制數(shù)字前面要加一個(gè)橫杠-,表示都匹配里烦,如-007就相當(dāng)于777凿蒜,-005相當(dāng)于555,
命令:
find . -perm -005
輸出:
[root@localhost test]# ll
總計(jì) 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 . -perm -005
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./test3
[root@localhost test]#
3.忽略某個(gè)目錄:
如果在查找文件時(shí)希望忽略某個(gè)目錄,因?yàn)槟阒滥莻€(gè)目錄中沒有你所要查找的文件胁黑,那么可以使用-prune選項(xiàng)來指出需要忽略的目錄废封。在使用-prune選項(xiàng)時(shí)要當(dāng)心,因?yàn)槿绻阃瑫r(shí)使用了-depth選項(xiàng)丧蘸,那么-prune選項(xiàng)就會(huì)被find命令忽略漂洋。如果希望在test目錄下查找文件,但不希望在test/test3目錄下查找力喷,可以用:
命令:
find test -path "test/test3" -prune -o -print
輸出:
[root@localhost soft]# find test -path "test/test3" -prune -o -print
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
4.使用find查找文件的時(shí)候怎么避開某個(gè)文件目錄:
實(shí)例1:在test 目錄下查找不在test4子目錄之內(nèi)的所有文件
命令:
find test -path "test/test4" -prune -o -print
輸出:
[root@localhost soft]# find test
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]# find test -path "test/test4" -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]#
說明:
find [-path ..] [expression]
在路徑列表的后面的是表達(dá)式
-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的簡寫表達(dá)式按順序求值,
-a 和 -o 都是短路求值刽漂,與 shell 的 && 和 || 類似如果
-path "test" 為真,則求值 -prune , -prune 返回真弟孟,與邏輯表達(dá)式為真贝咙;否則不求值 -prune,與邏輯表達(dá)式為假拂募。
如果 -path "test" -a -prune 為假颈畸,則求值 -print ,-print返回真没讲,或邏輯表達(dá)式為真眯娱;否則不求值 -print,或邏輯表達(dá)式為真爬凑。
這個(gè)表達(dá)式組合特例可以用偽碼寫為:
if -path "test" then
-prune
else
實(shí)例2:避開多個(gè)文件夾:
命令:
find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
輸出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
說明:
圓括號表示表達(dá)式的結(jié)合徙缴。 \ 表示引用,即指示 shell 不對后面的字符作特殊解釋,而留給 find 命令去解釋其意義于样。
實(shí)例3:查找某一確定文件疏叨,-name等選項(xiàng)加在-o 之后
命令:
find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
輸出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
test/log2014.log
test/log2015.log
test/log2013.log
test/log2012.log
[root@localhost soft]#
5.使用user和nouser選項(xiàng):
按文件屬主查找文件:
實(shí)例1:在$HOME目錄中查找文件屬主為peida的文件
命令:
find ~ -user peida -print
實(shí)例2:在/etc目錄下查找文件屬主為peida的文件:
命令:
find /etc -user peida -print
說明:
實(shí)例3:為了查找屬主帳戶已經(jīng)被刪除的文件,可以使用-nouser選項(xiàng)穿剖。在/home目錄下查找所有的這類文件
命令:
find /home -nouser -print
說明:
這樣就能夠找到那些屬主在/etc/passwd文件中沒有有效帳戶的文件蚤蔓。在使用-nouser選項(xiàng)時(shí),不必給出用戶名糊余; find命令能夠?yàn)槟阃瓿上鄳?yīng)的工作秀又。
6.使用group和nogroup選項(xiàng):
就像user和nouser選項(xiàng)一樣,針對文件所屬于的用戶組贬芥, find命令也具有同樣的選項(xiàng)吐辙,為了在/apps目錄下查找屬于gem用戶組的文件,可以用:
find /apps -group gem -print
要查找沒有有效所屬用戶組的所有文件蘸劈,可以使用nogroup選項(xiàng)昏苏。下面的find命令從文件系統(tǒng)的根目錄處查找這樣的文件:
find / -nogroup-print
7.按照更改時(shí)間或訪問時(shí)間等查找文件:
如果希望按照更改時(shí)間來查找文件,可以使用mtime,atime或ctime選項(xiàng)威沫。如果系統(tǒng)突然沒有可用空間了贤惯,很有可能某一個(gè)文件的長度在此期間增長迅速,這時(shí)就可以用mtime選項(xiàng)來查找這樣的文件棒掠。
用減號-來限定更改時(shí)間在距今n日以內(nèi)的文件孵构,而用加號+來限定更改時(shí)間在距今n日以前的文件。
希望在系統(tǒng)根目錄下查找更改時(shí)間在5日以內(nèi)的文件句柠,可以用:
find / -mtime -5 -print
為了在/var/adm目錄下查找更改時(shí)間在3日以前的文件,可以用:
find /var/adm -mtime +3 -print
8.查找比某個(gè)文件新或舊的文件:
如果希望查找更改時(shí)間比某個(gè)文件新但比另一個(gè)文件舊的所有文件棒假,可以使用-newer選項(xiàng)溯职。
它的一般形式為:
newest_file_name ! oldest_file_name
其中,帽哑!是邏輯非符號谜酒。
實(shí)例1:查找更改時(shí)間比文件log2012.log新但比文件log2017.log舊的文件
命令:
find -newer log2012.log ! -newer log2017.log
輸出:
[root@localhost test]# ll
總計(jì) 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
-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log
-rw-r--r-- 1 root root 0 11-16 14:43 log2017.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 -newer log2012.log ! -newer log2017.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
實(shí)例2:查找更改時(shí)間在比log2012.log文件新的文件
命令:
find . -newer log2012.log -print
輸出:
[root@localhost test]# find -newer log2012.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
9.使用type選項(xiàng):
實(shí)例1:在/etc目錄下查找所有的目錄
命令:
find /etc -type d -print
實(shí)例2:在當(dāng)前目錄下查找除目錄以外的所有類型的文件
命令:
find . ! -type d -print
實(shí)例3:在/etc目錄下查找所有的符號鏈接文件
命令:
find /etc -type l -print
10.使用size選項(xiàng):
可以按照文件長度來查找文件,這里所指的文件長度既可以用塊(block)來計(jì)量妻枕,也可以用字節(jié)來計(jì)量僻族。以字節(jié)計(jì)量文件長度的表達(dá)形式為N c;以塊計(jì)量文件長度只用數(shù)字表示即可屡谐。
在按照文件長度查找文件時(shí)述么,一般使用這種以字節(jié)表示的文件長度,在查看文件系統(tǒng)的大小愕掏,因?yàn)檫@時(shí)使用塊來計(jì)量更容易轉(zhuǎn)換度秘。
實(shí)例1:在當(dāng)前目錄下查找文件長度大于1 M字節(jié)的文件
命令:
find . -size +1000000c -print
實(shí)例2:在/home/apache目錄下查找文件長度恰好為100字節(jié)的文件:
命令:
find /home/apache -size 100c -print
實(shí)例3:在當(dāng)前目錄下查找長度超過10塊的文件(一塊等于512字節(jié))
命令:
find . -size +10 -print
11.使用depth選項(xiàng):
在使用find命令時(shí),可能希望先匹配所有的文件饵撑,再在子目錄中查找剑梳。使用depth選項(xiàng)就可以使find命令這樣做唆貌。這樣做的一個(gè)原因就是,當(dāng)在使用find命令向磁帶上備份文件系統(tǒng)時(shí)垢乙,希望首先備份所有的文件锨咙,其次再備份子目錄中的文件。
實(shí)例1:find命令從文件系統(tǒng)的根目錄開始追逮,查找一個(gè)名為CON.FILE的文件酪刀。
命令:
find / -name "CON.FILE" -depth -print
說明:
它將首先匹配所有的文件然后再進(jìn)入子目錄中查找
12.使用mount選項(xiàng):
在當(dāng)前的文件系統(tǒng)中查找文件(不進(jìn)入其他文件系統(tǒng)),可以使用find命令的mount選項(xiàng)羊壹。
實(shí)例1:從當(dāng)前目錄開始查找位于本文件系統(tǒng)中文件名以XC結(jié)尾的文件
命令:
find . -name "*.XC" -mount -print