天天看點

Linux系統管理(二):檔案查找指令find

Linux系統管理(二):檔案查找指令find

檔案查找指令find

有些時候,我們可能會忘了某個檔案所在的位置,此時就需要通過find來查找。

還有些時候,我想要找到,某個目錄下,所有小于1k的檔案。……

還還還有些時候,我們想找到,某個目錄下,所有以.sh結尾的腳本。

Linux系統中的find指令在查找檔案時非常有用而且友善。

find指令的文法

指令   路徑     選項(按xx查找)       表達式         動作
find[path...][options]           [expression][action]
查找   地區     小姐姐               18           約...
find     /etc/       -name "*conf*" -a -name "*config*"           

示例:

#按檔案名查找


-name
-iname   忽略大小寫


如
[root@localhost ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1} 建立檔案


[root@localhost ~]# find /etc/ -name "ifcfg-eth1"     查找/etc目錄下包含ifcfg-eth0名稱的檔案      
/etc/sysconfig/network-scripts/ifcfg-eth1


[root@localhost ~]# find /etc/ -iname "ifcfg-eth1"     -i 忽略大小寫
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1


[root@localhost ~]# find /etc/ -iname "ifcfg-eth*"     * 查找所有名字以ifcfg-eth開頭的檔案
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1


[root@localhost ~]# find / -name "xxx*"
/root/xxx
/root/xxx1
/var/spool/mail/xxx
/tmp/xxx1
/home/xxx
[root@localhost ~]# find / -name *"xxx*"               兩個* 查找/下所有名字包含xxx的檔案
/root/xxx
/root/xxx1
/var/spool/mail/xxx
/tmp/xxx1
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxx
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxx/qat_c3xxx.ko.xz
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxxvf
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxxvf/qat_c3xxxvf.ko.xz
......

           
# 按照檔案類型找


find 路徑 -type (f d l等)
-type


f 普通檔案
d 目錄
l 連結
b 塊裝置檔案   就那些磁盤 很少
c 字元裝置檔案
s 安全套接字檔案
p 管道檔案
等等


如:
find / -type f
find /etc -type f


-a 和 并且
-o 或者
!取反(非)


[root@localhost ~]# find / -type d -name 'conf'     查找根下d類型,名字conf的檔案
/proc/sys/net/ipv4/conf
/proc/sys/net/ipv6/conf


[root@localhost ~]# find /etc/ -name "*conf*" -a -name "*config*"     /etc下 名字有這兩種的
/etc/chkconfig.d
/etc/systemd/system/multi-user.target.wants/rhel-configure.service
/etc/pki/nss-legacy/nss-rhel7.config
/etc/pm/config.d
......




[root@localhost ~]# find /etc/ -name "*conf*" -o -name "*config*"   比上面出來多得多
/etc/resolv.conf
/etc/chkconfig.d
/etc/libaudit.conf
/etc/security/pwquality.conf
/etc/security/access.conf
/etc/security/chroot.conf
......


[root@localhost ~]# find /etc/ -type d ! -name "conf"     /etc/下名字不帶conf的目錄 出來一大堆
......


[root@localhost ~]# find /etc -type f ! -name "*config*"   /etc下所有不帶"*config*"的檔案           
# 按檔案大小找


find 路徑 -size ±/number
+     大于
-     小于
number 等于


[root@localhost ~]# find /root -size 1k     查找/root下1k的檔案
/root
/root/.bash_logout
/root/.bash_profile
...
#看大小
[root@localhost ~]# du -sh /root/.bash_logout   由于1k占用1個block,出來的是4k。(計算機最小機關1個block是4k)
4.0K/root/.bash_logout                   搜11k的話 出來是12k   是以用+-多一些




[root@localhost ~]# find /etc -size +5M     查找/etc下大于5M的檔案
/etc/udev/hwdb.bin


[root@localhost ~]# ll -h /etc/udev/hwdb.bin     看一下大小
-r--r--r--. 1 root root 7.6M Aug 17 15:33 /etc/udev/hwdb.bin           

小練習

# 在/opt下建1000個檔案 然後一次全部删掉 要求用find指令  
[root@localhost opt]# touch {1..1000}

find /opt -size -1k |xargs rm -fr

find -name '{1..1000}' |xargs rm -fr {1..1000}   前面的其實沒用   認的後面的 ~~~~~           
# 按檔案的使用者查找


find 路徑 選項 xxx
-user
-group
-nouser
-nogroup


[root@localhost ~]# find /home -user xxx
/home/xxx
/home/xxx/.bash_logout
/home/xxx/.bash_profile
/home/xxx/.bashrc
/home/xxx/.bash_history
/home/xxx/test1
[root@localhost ~]# cd /home
[root@localhost home]# ll
total 0
drwx------. 2 xxx xxx      96 Aug 25 12:14 xxx
drwx------. 2 zls students 62 Aug 18 11:34 zls
[root@localhost home]# find /home -user xxx -group xxx
/home/xxx
/home/xxx/.bash_logout
/home/xxx/.bash_profile
/home/xxx/.bashrc
/home/xxx/.bash_history
/home/xxx/test1
[root@localhost home]# find -user xxx -group xxx
./xxx
./xxx/.bash_logout
./xxx/.bash_profile
./xxx/.bashrc
./xxx/.bash_history
./xxx/test1           
# 按時間查找


stat裡面看下
[root@localhost opt]# stat /opt/1000.txt
File: ‘/opt/1000.txt’
Size: 0         Blocks: 0         IO Block: 4096   regular empty file
Device: 803h/2051dInode: 17159663   Links: 1
Access: (0644/-rw-r--r--) Uid: (    0/   root)   Gid: (    0/   root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2022-08-25 11:24:34.827131101 +0800     # Access:接近、進入、通路
Modify: 2022-08-25 11:24:34.827131101 +0800
Change: 2022-08-25 11:24:34.827131101 +0800


-atime 檔案通路時間
-mtime 檔案内容建立、修改時間
-ctime 檔案屬性修改時間


±/=Num
7   查找第7天的檔案
+7   查找7天以前的檔案(不包含今天,不包含第7天)
-7   查找最近7天的檔案,不建議使用(包含當天的檔案)


如:
# find /opt -mtime -1           
# 按檔案的權限查找
find 路徑 -perm (權限值)


精确比對
find / -perm 022


模糊比對 包含-後面的對應權限
find / -perm -022


find . -perm 644 -ls           
# 按照深度查找
find 路徑 -maxdepth 10000 顯示全部


//列印目錄的時候,隻列印1級目錄(按深度查找) 
例
[root@db04 ~]# find /home/ -maxdepth 1 -type d  -user zls           

find處理動作

當查找到一個檔案後, 需要對檔案進行如何處理, 預設動作 -print

動作	        含義
-print	  列印查找到的内容(預設)
-ls	      以長格式顯示找到東西的詳細資訊
-delete	  删除查找到的檔案(僅能删除空目錄)
-ok	      後面跟自定義shell指令(會提示是否操作)
-exec	  後面跟自定義shell指令(标準寫法-exec \;)


-exec { \ ;
           

繼續閱讀