Linux之find命令


一、介紹
Linux下find命令在目錄結構中搜索文件,並執行指定操作。如果使用該命令時,不設置任何參數,則find命令將在當前目錄下查找子目錄與文件。並且將查找到的子目錄和文件全部進行顯示。


二、使用方法
1.命令格式:
find pathname -options [-print ] [-exec -ok command ] {}\;
2.命令參數:
pathname:find命令所查找的目錄路徑。.來表示當前目錄,/用來表示系統根目錄
-print:find命令將匹配的文件輸出到標准輸出
-exec command {}\: 將查到的文件執行command操作,{}和\;之間有空格
-ok 和 -exec 相同,只不過在操作前要詢用戶

-name filename:查找名為filename的文件
-perm:按執行權限來查找
-user:按文件屬主來查找
-group:按組來查找
-mtime -n +n:按文件更改時間來查找文件,-n指yn天以內,+n指n天以前
-atime -n +n:按文件訪問時間來查看
-ctime -n +n:按文件創建時間來查找文件,-n 指n天以內,+n指n天以前
-nogroup:查無有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser:查無有效屬主的文件,即文件的屬主在/etc/passwd中不存在
-type: b/d/c/p/l/f:查找是塊設備、目錄、字符設備、管道、符號鏈接、普通文件
-size:查找文件長度為n字節的文件
-mount:查文件時不跨越文件系統mount點

 

三、案例
在查看root目錄下文件名為txt結尾的文件並輸出
[root@ping ~]# find ~ -name "*.txt" -print
/root/ping.txt

   在查找當前目錄下文件名為txt結尾的文件並輸出

[root@ping ~]# find . -name "*.txt" -print
./ping.txt

   在查找當前目錄下文件名以a到Z開頭的文件並輸出

[root@ping ~]# find . -name "[a-Z]*" -print
./fqqltxt.zipv

   在查找/etc/目錄下以host開頭的文件並輸出

[root@ping ~]# find /etc -name "host*" -print
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/host.conf

   在查找rootl目錄下查找以一個字母和一個數字開頭的文件並輸出

[root@ping ~]# find ~ -name "[a-z][0-9].conf" -print
/root/a9.conf

   在當前目錄下查找權限為755的文件並輸出

[root@ping ~]# find . -perm 755 -print | tail -1
./ping.txt
[root@ping ~]# ls -ld ping.txt/
drwxr-xr-x. 2 root root 4096 12月 26 11:12 ping.txt/
[root@ping ~]# find . -perm 755 -print -exec ls -l {} \;
-rwxr-xr-x. 1 root root 981093 12月 18 15:13 ./nginx-1.12.1.tar.gz

   在當前目錄下查找目錄並輸出

[root@ping ~]# find . -type d -print
./.ssh

   在根目錄下查找符號鏈接文件並輸出

[root@ping ~]# find / -type l -print
/etc/httpd/run

 查看當前目錄下大小為5kB的文件並輸出

[root@ping ~]# find . -size +10c -print -exec ls -lh {} \; | tail -1
-rw-------. 1 root root 1.9K 12月 19 22:30 ./.mysql_history

 搜索以cron名開頭的文件名,並以md5sum輸出 

[root@ping ~]# find /etc/cron* -type f -exec md5sum {} \; 

搜索當前目錄的第一層是普通文件,權限位是644的文件刪除

pingzimo@pingzhe:~$ find ./ -maxdepth 1 -type f -perm  664 | xargs ls -lh 
-rw-rw-r-- 1 pingzimo pingzimo 28K 3月  30 20:35 ./ping
pingzimo@pingzhe:~$ find ./ -maxdepth 1 -type f -perm  664 | xargs rm -f 

  

  




免責聲明!

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



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