find 的使用及練習

find是個使用頻率比較高的命令。常常用它在系統特定目錄下,查找具有某種特征【名字類型屬主權限等】的文件。
find命令的格式: find  [-path ..] -options [-print -exec -ok]

path:要查找的目錄路徑。

~ 表示$HOME目錄

. 表示當前目錄

/ 表示根目錄

-print :表示將結果輸出到標準輸出
-exec :對匹配的文件執行該參數所給出的shell命令。形式為 command  {} \; ,注意{}與\; 之間有空格
-ok :與-exec作用相同,區別在于,在執行命令之前,都會給出提示,讓用戶確認是否執行

options常用的有下選項:

-name 按照名字查找

-perm 安裝權限查找

-prune 不再當前指定的目錄下查找

-user 文件屬主來查找

-group 所屬組來查找

-nogroup 查找無有效所屬組的文件

-nouser 查找無有效屬主的文件

-type 按照文件類型查找

下面通過一些簡單的例子來介紹下find的常規用法:

1、按名字查找

在當前目錄及子目錄中,查找大寫字母開頭的txt文件

  1. [root@localhost ~]# find . -name ‘[A-Z]*.txt’ -print  

在/etc及其子目錄中,查找host開頭的文件

  1. [root@localhost ~]# find /etc -name ‘host*’ -print   

在$HOME目錄及其子目錄中,查找所有文件


  1. [root@localhost ~]# find ~ -name ‘*’ -print  

在當前目錄及子目錄中,查找不是out開頭的txt文件


  1. [root@localhost .code]# find . -name “out*” -prune -o -name “*.txt” -print    

2、按目錄查找

在當前目錄除aa之外的子目錄內搜索 txt文件


  1. [root@localhost .code]# find . -path “./aa” -prune -o -name “*.txt” -print  

在當前目錄及除aa和bb之外的子目錄中查找txt文件


  1. [root@localhost .code]# find . ?path”./aa”?o?path”./bb” -prune -o -name “*.txt” -print     

在當前目錄,不再子目錄中,查找txt文件


  1. [root@localhost .code]# find .  ! -name “.” -type d -prune -o -type f -name “*.txt” -print  

3、按權限查找

在當前目錄及子目錄中,查找屬主具有讀寫執行,其他具有讀執行權限的文件


  1. [root@localhost ~]# find . -perm 755 -print  

4、按類型查找

在當前目錄及子目錄下,查找符號鏈接文件


  1. [root@localhost .code]# find . -type l -print  

5、按屬主及屬組

查找屬主是www的文件


  1. [root@localhost .code]# find / -user www -type f -print  

查找屬主被刪除的文件


  1. [root@localhost .code]# find /  -nouser -type f -print   

查找組mysql的文件


  1. [root@localhost .code]# find /  -group mysql -type f  -print  

查找用戶組被刪掉的文件


  1. [root@localhost .code]# find /  -nogroup -type f  -print     

6、按時間查找

查找2天內被更改過的文件


  1. [root@localhost .code]# find . -mtime -2 -type f -print   

查找2天前被更改過的文件


  1. [root@localhost .code]# find . -mtime +2 -type f -print  

查找一天內被訪問的文件


  1. [root@localhost .code]# find . -atime -1 -type f -print   

查找一天前被訪問的文件


  1. [root@localhost .code]# find . -atime +1 -type f -print   


查找一天內狀態被改變的文件


  1. [root@localhost .code]# find . -ctime -1 -type f -print   

查找一天前狀態被改變的文件


  1. [root@localhost .code]# find . -ctime +1 -type f -print  

查找10分鐘以前狀態被改變的文件


  1. [root@localhost .code]# find . -cmin +10 -type f -print   

7、按文件新舊

查找比aa.txt新的文件


  1. [root@localhost .code]# find . -newer “aa.txt”  -type f -print   

查找比aa.txt舊的文件


  1. [root@localhost .code]# find . ! -newer “aa.txt”  -type f -print    

查找比aa.txt新,比bb.txt舊的文件


  1. [root@localhost .code]# find . -newer ‘aa.txt’ ! -newer ‘bb.txt’ -type f -print   

8、按大小查找

查找超過1M的文件


  1. [root@localhost .code]# find / -size +1M -type f -print    

查找等于6字節的文件


  1. [root@localhost .code]# find . -size 6c -print  

查找小于32k的文件


  1. [root@localhost .code]# find . -size -32k -print  

9、執行命令

查找del.txt并刪除,刪除前提示確認


  1. [root@localhost .code]# find . -name ‘del.txt’ -ok rm {} \;  

查找aa.txt 并備份為aa.txt.bak


  1. [root@localhost .code]# find . -name ‘aa.txt’ -exec cp {} {}.bak \;   

查找aa.txt 歸檔壓縮為aa.txt.tar.gz 并刪除aa.txt


  1. find . -name “aa.txt” -type f -exec tar -zcvf {}.tar.gz {} \; -exec rm -rf {} \; > /dev/null  

原創文章,作者:sunhao,如若轉載,請注明出處:http://www.www58058.com/77897

(0)
sunhaosunhao
上一篇 2017-06-11 15:52
下一篇 2017-06-11 17:23

相關推薦

  • parted使用說明

    一.為什么使用parted命令     傳統的MBR(Master Boot Record)分區方式,有一個局限:無法支持超過2TB的硬盤分區(單個分區超過2TB)。     GPT(GUID Partition Table)的分區表很好的解決了這個問題,但在Linux系統中,傳…

    Linux干貨 2015-04-13
  • zabbix部署(Linux上部署/監控端/被監控端)

    zabbix部署 一、監控系統的簡單介紹 (1)cacti:存儲數據能力強,報警性能差 (2)nagios:報警性能差,存儲數據僅有簡單的一段可以判斷是否在合理范圍內的數據長度,儲存在內存中。比如,連續采樣數據存儲,有連續三次不在合理范圍內的數據就報警 (3)zabbix:結合上面兩種工具的優點,又可以存儲數據,又可以報警 二、zabbix特性 (1)數據采…

    Linux干貨 2017-01-06
  • linux 計劃任務

    Linux之 計劃任務 介紹 相信每個人都有使用鬧鐘的習慣,我們設定鬧鐘的種類有很多。比如說,只提醒一次、工作日提醒、休息日提醒等。在設定鬧鐘之后,每天的設定時間都會按時的提醒你去做什么事情,以免自己忘記一些重要的會議等事情。像這樣在每天特定的時間安排做一些事情。這樣一種事情我們就稱之為例行任務計劃。 其實在個系統平臺上都有類似的例行性任務計劃功能,那如何去…

    Linux干貨 2017-09-04
  • 文件系統權限管理

    文件系統權限管理 文件及目錄權限 文件系統上的權限是針對訪問者的 訪問者:     owner:屬主,u     group:屬組,g     other:其他,o 針對每個訪問者有三種權限 r:readable w:writeable x…

    Linux干貨 2016-11-05
  • nmcli命令使用,解析性能監控工具

    nmcli nmcli地址配置工具,NetworkManager client 網絡管理客戶端 相關命令:nmcli connection show        查看當前連接狀態 nmcli connection reload      重啟服務 nmcli connection sho…

    Linux干貨 2016-09-07
  • 程序包管理

    程序包 linux的程序包主要分為兩類;二進制可執行安裝包和源代碼程序文件包     二進制應用程序的組成部分:     二進制文件、庫文件、配置文件、幫助文件   查看二進制程序所依賴的庫文件:     ldd&nbs…

    Linux干貨 2016-05-30

評論列表(1條)

  • 371987341
    371987341 2017-06-11 17:26

    雙擊評論 666

欧美性久久久久