find命令在工作中用的是相當多的,下面總結下find命令的常見用法:
1.查找文件或者路徑:
[root@hexel ~/shell]#find /etc -name ifcfg-eth2
/etc/sysconfig/network-scripts/ifcfg-eth2
/etc/sysconfig/networking/profiles/default/ifcfg-eth2
/etc/sysconfig/networking/devices/ifcfg-eth2
[root@hexel ~/shell]#find /root/shell/ -name "tim*.log" --使用通配符
/root/shell/timing.log
[root@hexel ~/shell]#find /root/shell/ -iname "tim*.log" -- -iname忽略大小寫查找
/root/shell/timing.log
/root/shell/TIMING.LOG
[root@hexel ~/shell]#find ./ -iname "*.log" -o -name "*.sh" -- -o選項用於或關係查詢,默認是and關係
或者
[root@hexel ~/shell]#find ./ \( -name "*.log" -o -name "*.sh" \) --這時候使用\(和\)把括號內部的看成一個整體
./IFS/passwd.sh
./IFS/csv.sh
./timing.log
./taw_capinfo.sh
./file_operation/2_read_ouput.sh
./file_operation/ok.sh
查找不以.log和.sh結尾的文件和目錄:
[root@hexel ~/shell]#find ./ ! \( -name "*.log" -o -name "*.sh" \) | head
./
./output.session
./IFS
./IFS/passwd
./scriptfifo
./file_operation
./function
./tty
./TIMING.LOG
./user_parameter
[root@hexel ~/shell]#find ./ -path "*sort*" -- -name用於查找文件,-path可以使用通配符查找文件路徑
./sort
./sort/s.sh
./sort/sort.sh
[root@hexel ~/shell]#find ./ -regex "^.*\(\.sh\|\.log\)$" | head -- -regex用於正則匹配,注意轉義字符的應用
./IFS/passwd.sh
./IFS/csv.sh
./timing.log
./taw_capinfo.sh
./file_operation/2_read_ouput.sh
./file_operation/ok.sh
./file_operation/1_delete_file.sh
./function/fun_out.sh
./function/parameter.sh
[root@hexel ~/shell]#find ./ -iregex "^.*\(\.sh\|\.LOG\)$" | head --不考慮大小寫
./IFS/passwd.sh
./IFS/csv.sh
./timing.log
./taw_capinfo.sh
./file_operation/2_read_ouput.sh
./file_operation/ok.sh
./file_operation/1_delete_file.sh
./function/fun_out.sh
./function/parameter.sh
./function/function.sh
2 基於目錄深度的查找
默認會在指定目錄及其所有子目錄查找,可以指定目錄深度查找。例如,只想查找當前目錄下的文件,不想在子目錄中查找:
[root@hexel ~/shell]#find ./ -maxdepth 1 -iregex "^.*\(\.sh\|\.LOG\)$"
./timing.log
./taw_capinfo.sh
./TIMING.LOG
./timing.sh
有時候可能只需要在某個深度目錄查找,可以這樣:
[root@hexel ~/shell]#find ./ -maxdepth 2 -mindepth 2 -iregex "^.*\(\.sh\|\.LOG\)$" | head
./IFS/passwd.sh
./IFS/csv.sh
./file_operation/2_read_ouput.sh
./file_operation/ok.sh
./file_operation/1_delete_file.sh
./function/fun_out.sh
./function/parameter.sh
./function/function.sh
./function/variable.sh
./function/fork.sh
3 根據文件類型查找(-type)
例如:
[root@hexel ~/shell]#find ./ -type f -iregex "^.*\(\.log\|\.ln\)$" --只列出文件
./timing.log
./TIMING.LOG
[root@hexel ~/shell]#find ./ -type l -iregex "^.*\(\.log\|\.ln\)$" --只列出鏈接
./timing.ln
[root@hexel ~/shell]#find ./ -type p -name "*"
./scriptfifo
還有其他類型;
f:普通文件
d: 目錄
l: 符號鏈接
b: 塊設備
s: 套接字文件
c:字符塊設備
p: fifo文件
4.基於相關時間搜索
搜索最近五天被訪問過的文件;
[root@hexel ~/shell]#find ./ -type f -name "*.sh" -atime -5
./IFS/passwd.sh
./IFS/csv.sh
./timing.sh
搜索最近五天被修改過的文件
[root@hexel ~/shell]#find ./ -type f -name "*.sh" -mtime -5
./timing.sh
搜索最近五天被修改過元數據(權限,所有者等屬性)的文件
[root@hexel ~/shell]#find ./ -type f -name "*.sh" -ctime -5
./taw_capinfo.sh
./timing.sh
注:可以按照分鐘搜索,具體同上
-amin:訪問分鐘
-mmin:修改分鐘
-cmin:變化分鐘
5.基於文件大小的搜索
這個功能特別有用,例如,查找大於5k大小的文件
[root@hexel ~/shell]#find ./ -type f -size +5k
./xm_l_change/11.tar.gz
./xm_l_change/hx/log.sh
./xm_l_change/hx/11.tar.gz
./tcpdump/121111-00:01:50
./tcpdump/121110-23:38:50
./tcpdump/121110-23:40:35
同理,-5k表示小於5k,5k表示大於等於5k。還可以以b(block),c(字節),w(字),G(吉字節)爲單位進行搜索
找到size大於某個數值的文件後,可以使用-delete進行刪除操作:
[root@hexel ~/shell]#find ./ -type f -size +50k
./xm_l_change/hx/log.sh
[root@hexel ~/shell]#find ./ -type f -size +50k -delete
6.查找特定用戶的文件:
[root@hexel /var/www/html/ecshop]#find ./ -type f -iname "*.php" -user root
./oracle/oracle.php
./oracle/config.php
./oracle/oracle3.php
./oracle/oracle1.php
./md5.php