Centos的文件搜索命令find

find [搜索範圍] [搜索條件]

#搜索文件

find / -name install.log

#避免大範圍搜索,會非常耗費系統資源

#find是在系統當中搜索符合條件的文件名。如果需要匹配,

使用通配符匹配,通配符是完全匹配。

[root@localhost ~]# ls

222  anaconda-ks.cfg  牛逼  牛牛

[root@localhost ~]# find / -name 牛牛

/root/牛牛

/tmp/牛牛

[root@localhost ~]# find / -name 牛

[root@localhost ~]# 

我們發現 搜索牛牛 能搜索到結果,但是搜索牛,麼有結果,

所以說 find搜索 是完全匹配搜索;

 

如果我們需要進行模糊查詢,我們要使用通配符;

* 匹配任意內容  

?匹配任意一個字符 

[]匹配任意一箇中括號的字符

 

我們創建一些文件來測試

[root@localhost ~]# ls

222  anaconda-ks.cfg  牛逼  牛逼2  牛牛  牛牛2

[root@localhost ~]# 

 

[root@localhost ~]# find / -name "牛*"

/root/牛逼

/root/牛牛

/root/牛逼2

/root/牛牛2

/tmp/牛牛

查找開頭是 “牛”的所有文件

 

[root@localhost ~]# find /root -name "牛?"

/root/牛逼

/root/牛牛

查找root目錄下,所以“牛”開頭然後後面接一位字符的文件

 

[root@localhost ~]# find /root -name "牛[牛逼]2"

/root/牛逼2

/root/牛牛2

[root@localhost ~]# 

查找首尾分別是“牛”“2”,中間字符串是“牛逼”當中的任一字符的文件

 

find /root -iname anaconda-ks.cfg

不區分大小寫

find /root -user root

根據所有者搜索

find /root -nouser

查找沒有所有者的文件

 

[root@localhost ~]# find /root -iname Anaconda-ks.cfg

/root/anaconda-ks.cfg

[root@localhost ~]# find /root -name Anaconda-ks.cfg

[root@localhost ~]# 

linux是嚴格區分大小寫的,假如用iname 查詢時不區分大小寫;

 

[root@localhost ~]# find /root -user root

/root

/root/.bash_logout

/root/.bash_profile

/root/.bashrc

/root/.cshrc

/root/.tcshrc

/root/anaconda-ks.cfg

/root/.bash_history

/root/牛逼

/root/牛逼/java.pdf

/root/222

/root/牛牛

/root/牛逼2

/root/牛牛2

root用戶的所有文件

 

find /var/log/ -mtime +10

查找10天前修改的文件

 

-10 10天內修改的文件

10 10天當前修改的文件

+10 10天前修改的文件

 

atime 文件訪問時間

ctime 改變文件屬性

mtime 修改文件內容

 

[root@localhost ~]# find /var/log -mtime +10

/var/log/ppp

查找10天前的日誌

 

find /root  -size 2k

查找文件大小是1到2KB的文件(進一法)

-2k 小於2KB的文件

2k 等於2KB的文件

+2k 大於2KB的文件

 

find /root -inum 262422

查找i節點是262422的文件

 

[root@localhost ~]# find /root -size 2k

/root/anaconda-ks.cfg

/root/.bash_history

[root@localhost ~]# find /root -size -2k

/root

/root/.bash_logout

/root/.bash_profile

/root/.bashrc

/root/.cshrc

/root/.tcshrc

/root/牛逼

/root/牛逼/java.pdf

/root/222

/root/牛牛

/root/牛逼2

/root/牛牛2

[root@localhost ~]# find /root -size +2k

[root@localhost ~]# 

 

[root@localhost ~]# ls -i

33575031 222                801541 牛逼   33575023 牛牛

33574979 anaconda-ks.cfg  33605192 牛逼2  33605193 牛牛2

[root@localhost ~]# find /root -inum 33575023

/root/牛牛

[root@localhost ~]# 

根據i節點來搜索

 

find /etc -size +20k -a -size -50k

查找/etc/目錄下,大於20KB並且小於50KB的文件

 

-a and 邏輯與 ,兩個條件都滿足

-o or 邏輯或,兩個條件滿足一個即可

 

find /etc -size +20k -a -size -50k -exec ls -lh{} \ ;

查找/etc/目錄下,大於20KB並且小於50KB的文件,並顯示詳細信息;

-exec/-ok 命令{} \; 對搜索結果執行操作;

 

[root@localhost ~]# find /etc -size +20k -a -size -50k

/etc/selinux/targeted/active/modules/100/apache/hll

/etc/selinux/targeted/active/modules/100/init/hll

/etc/selinux/targeted/active/modules/100/staff/cil

/etc/selinux/targeted/active/modules/100/staff/hll

/etc/selinux/targeted/active/modules/100/sysadm/cil

/etc/selinux/targeted/active/modules/100/sysadm/hll

/etc/selinux/targeted/active/modules/100/unprivuser/hll

/etc/selinux/targeted/active/modules/100/virt/hll

/etc/selinux/targeted/active/modules/100/xguest/hll

/etc/selinux/targeted/active/modules/100/xserver/hll

/etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin

/etc/sysconfig/network-scripts/network-functions-ipv6

/etc/ld.so.cache

/etc/dnsmasq.conf

/etc/postfix/access

/etc/postfix/header_checks

/etc/postfix/main.cf

[root@localhost ~]# find /etc -size +20k -a -size -50k -exec ls -lh {}\;

find: 遺漏“-exec”的參數

[root@localhost ~]# find /etc -size +20k -a -size -50k -exec ls -lh {} \;

-rw-r--r--. 1 root root 25K 11月 12 2016 /etc/selinux/targeted/active/modules/100/apache/hll

-rw-r--r--. 1 root root 31K 11月 12 2016 /etc/selinux/targeted/active/modules/100/init/hll

-rw-r--r--. 1 root root 21K 11月 12 2016 /etc/selinux/targeted/active/modules/100/staff/cil

-rw-r--r--. 1 root root 36K 11月 12 2016 /etc/selinux/targeted/active/modules/100/staff/hll

-rw-r--r--. 1 root root 30K 11月 12 2016 /etc/selinux/targeted/active/modules/100/sysadm/cil

-rw-r--r--. 1 root root 46K 11月 12 2016 /etc/selinux/targeted/active/modules/100/sysadm/hll

-rw-r--r--. 1 root root 31K 11月 12 2016 /etc/selinux/targeted/active/modules/100/unprivuser/hll

-rw-r--r--. 1 root root 29K 11月 12 2016 /etc/selinux/targeted/active/modules/100/virt/hll

-rw-r--r--. 1 root root 21K 11月 12 2016 /etc/selinux/targeted/active/modules/100/xguest/hll

-rw-r--r--. 1 root root 30K 11月 12 2016 /etc/selinux/targeted/active/modules/100/xserver/hll

-rw-r--r--. 1 root root 44K 11月 12 2016 /etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin

-rw-r--r--. 1 root root 27K 9月  12 2016 /etc/sysconfig/network-scripts/network-functions-ipv6

-rw-r--r--. 1 root root 27K 6月  10 05:21 /etc/ld.so.cache

-rw-r--r--. 1 root root 25K 11月 12 2016 /etc/dnsmasq.conf

-rw-r--r--. 1 root root 21K 6月  10 2014 /etc/postfix/access

-rw-r--r--. 1 root root 22K 6月  10 2014 /etc/postfix/header_checks

-rw-r--r--. 1 root root 27K 6月  10 2014 /etc/postfix/main.cf

[root@localhost ~]# 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章