20181216文件查找

文件查找

grep: 文件內容過濾
find: 文件查找,針對文件名
一、命令文件
which ls //從 PATH 環境變量 (echo $PATH)

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin
二、任意文件
(一). locate (查詢的數據庫: /var/lib/mlocate/mlocate.db)
計劃任務:每天自動更新數據庫 /etc/cron.daily/mlocate.cron
手動更新數據庫:updatedb 在查找之前,先要更新數據庫
locate ifcfg-eth0
locate ifcfg-enp0s25

(二)find(儘量縮小找的範圍)
find [options] [path...] [expression] [action]
1.按文件名
[root@tianyun ~]# find /etc -name "ifcfg-eth0"
[root@tianyun ~]# find /etc -iname "ifcfg-eth0" //-i 忽略大小寫
[root@tianyun ~]# find /etc -iname "ifcfg-eth*" //藉助通配符

2.按文件大小:
[root@tianyun ~]# find /etc -size +5M //大於 5M
[root@tianyun ~]# find /etc -size 5M //等於 5M
[root@tianyun ~]# find /etc -size -5M //小於 5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls 找到的處理動作

3.指定查找的目錄深度:
-maxdepth levels
-mindepth levels
[root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-eth0"

4.按時間找(atime,mtime,ctime):
[root@dong ~]# ll -t /etc |head 查看最新修改的文件
-rw-r--r--. 1 root root 1309 12月 16 10:01 tpvmlp.conf
-rw-r--r--. 1 root root 239 12月 16 10:01 resolv.conf
-rw-r--r--. 1 root root 576 12月 16 10:01 mtab

[root@dong ~]# ll -t /etc |tail 最舊的文件
-rw-r--r--. 1 root root 233 1月 12 2010 printcap
-rw-r--r--. 1 root root 6455 1月 12 2010 protocols
-rw-------. 1 root root 122 1月 12 2010 securetty

[root@tianyun ~]# find /etc -mtime +5 //修改時間超過 5 天(120小時)
[root@tianyun ~]# find /etc -mtime 5 //修改時間等於 5 天
[root@tianyun ~]# find /etc -mtime -5 //修改時間 5 天以內

5.按文件屬主、屬組找:
[root@tianyun ~]# find /home -user jack //屬主是 jack 的文件
[root@tianyun ~]# find /home -group hr //屬組是 hr 組的文件
[root@tianyun ~]# find /home -user jack -group hr //屬主是jack,屬組是group
[root@tianyun ~]# find /home -user jack -a -group hr //和
[root@tianyun ~]# find /home -user jack -o -group hr //或
[root@tianyun ~]# find /home -nouser
[root@tianyun ~]# find /home -nogroup
[root@tianyun ~]# find /home -nouser -o -nogroup

6.按文件類型:
[root@tianyun ~]# find /dev -type f //f 普通
[root@tianyun ~]# find /dev -type d //d 目錄
[root@tianyun ~]# find /dev -type l //l 鏈接
[root@tianyun ~]# find /dev -type b //b 塊設備
[root@tianyun ~]# find /dev -type c //c 字符設備
[root@tianyun ~]# find /dev -type s //s 套接字
[root@tianyun ~]# find /dev -type p //p 管道文件

7.按文件權限
[root@tianyun ~]# find . -perm 644 -ls //當前目錄下查找權限是644的
[root@tianyun ~]# find . -perm -644 -ls //當前目錄下查找權限是包含644的
[root@tianyun ~]# find . -perm -600 -ls // 只要所有者有讀寫就可以
[root@tianyun ~]# find . -perm -222 -ls //全局可寫
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含 set uid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含 set gid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含 sticky

8.按正則表達式:
-regex pattern
[root@tianyun ~]# find /etc -regex '.ifcfg-eth[0-9]'
.
任意多個字符
[0-9] 任意一個數字
[root@localhost ~]# find /etc -regex '.ifcfg-enp0s25'
/etc/sysconfig/network-scripts/ifcfg-enp0s25
[root@localhost ~]# find /etc -regex '.
ifcfg-enp0s[0-9]+'
/etc/sysconfig/network-scripts/ifcfg-enp0s25

9.找到後處理的動作 ACTIONS: (默認動作-print)
-ls
-delete
-exec 後面跟自定義的 shell 命令
-ok 後面跟自定義的 shell 命令
[root@tianyun ~]# find /etc -name "ifcfg" -print[root@tianyun ~]# find /etc -name "ifcfg" -ls
[root@tianyun ~]# find /etc -name "ifcfg" -exec cp -rvf {} /tmp \; //{}表示前面查找到的結果
[root@tianyun ~]# find /etc -name "ifcfg
" -ok cp -rvf {} /tmp \; //+v查看效果,OK會有提示
[root@tianyun ~]# find /etc -name "ifcfg" -exec rm -rf {} \;
[root@tianyun ~]# find /etc -name "ifcfg
" -delete

擴展知識:find 結合 xargs
[root@tianyun ~]# find . -name "yang*.txt" |xargs rm -rf / /參數的傳遞xargs
[root@tianyun ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp

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