3.shell編程-文件查找之find命令


3.1.語法格式

find [路勁][選項][操作]

選項參數對照表

3.2.-name   

查找/etc/目錄下以.conf結尾的文件

find /etc/ -name "*.conf"

-iname   不區分大小寫

find /etc/ -iname "*.conf"

-user      查找當前目錄為root用戶的文件

find ./ -user root

3.3.-type   

文件的類型

  • f     文件
  • d    目錄
  • c    字符設備文件
  • b    塊設備文件
  • l     鏈接文件
  • p    管道文件 
find . -type f

find . -type d

3.4.-size

文件大小

  • -n    小與n的文件
  • +n   大於n的文件

 查找/etc目錄下小與100k的文件

find /etc -size -100k

查找/etc目錄下大於1M的文件

find /etc -size +1M

3.5.-mtime

修改時間

  • -n    n天以內修改的文件
  • +n   n天以外修改的文件
  • n     正好n天修改的文件

 查找/etc目錄下5天之內修改並且以conf結尾的文件

find /etc -mtime -5 -name '*.conf'

查找/etc目錄下10天之前修改並且屬主為root的文件

find /etc -mtime +10 -user root

3.6.-mmin

  • -n    n分鍾以內修改的文件
  • +n   n分鍾以外修改的文件

修改/etc目錄下30分鍾以內修改的目錄

find /etc -mmin -30 -type d

3.7.-mindepth 

表示從n級子目錄開始搜索

find /etc -mindepth 3 -type -f

-madepth n

表示最多搜索到n-1級子目錄

3.8.操作-exec

對搜索的文件常用操作

  •  -print   打印輸出
  • -exec    對文件執行特定的操作
  • -ok        和exec功能意義,只是每次操作都會給用戶提示

-exec的格式為

-exec 'command' {} \

例子一:

搜索/home/shell_learn/下的文件,文件名以.sh結尾,且修改時間在一個星期之內的,然后將其刪除

#打印
find /home/shell_learn/ -type f -name '*.sh' -mtime -7 -print

#復制
find /home/shell_learn/ -type f -name '*.sh' -mtime -7 -exec cp {} /home/shell_learn/test/ \;

#刪除
find /home/shell_learn/ -type f -name '*.sh' -mtime -7 -exec rm -rf {} \;

3.9.locate命令

locate不同於find命令是在整塊磁盤中搜索,locate命令是在數據庫文件中查找

find是默認全局匹配,locate則是默認部分匹配

updatedb命令

  • 用戶更新/var/lib/mlocate/mlocate.db
  • 所使用的配置文件/etc/updatedb.conf

 實例:updatedb命令把文件更新到數據庫(默認是第二天系統才會自動更新到數據庫),否則locate查找不到

[root@VM_0_9_centos shell_learn]# touch 789.txt
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# locate 789.txt
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# updatedb
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# locate 789.txt
/home/shell_learn/789.txt
[root@VM_0_9_centos shell_learn]#

3.10 .whereis命令

實例

[root@VM_0_9_centos shell_learn]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# whereis -b mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# whereis -m mysql
mysql: /usr/share/man/man1/mysql.1.gz
[root@VM_0_9_centos shell_learn]# 

3.11.which

作用:僅查找二進制程序文件

[root@VM_0_9_centos shell_learn]# which mysql
/usr/bin/mysql
[root@VM_0_9_centos shell_learn]# 

3.12.各查找命令總結

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM