Linux命令:find命令詳解

find命令格式

find path -option [-print] [-exec -ok |xargs |grep] [command {} \;]

# 參數說明
    path: find命令所查找的目錄路徑。
    		~ 表示$HOME目錄;
    		.來表示當前目錄;
    		/來表示系統根目錄。
    		
  -print: find命令將匹配的文件輸出到標準輸出。
  
   -exec: find命令對匹配的文件執行該參數所給出的shell命令。
   			相應命令的形式爲command {} ;,注意{};之間的空格。
   			
     -ok: 和-exec的作用相同,只不過以一種更爲安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。

  |xargs: 與exec作用相同 ,起承接作用,區別在於|xargs 主要用於承接刪除操作 ,而-exec都可用 如複製、移動、重命名等。

 options: 表示查找方式

find命令參數

path :要查找的目錄路徑。

  • ~ 表示$HOME目錄
  • . 表示當前目錄
  • / 表示根目錄

print :表示將結果輸出到標準輸出。

exec :對匹配的文件執行該參數所給出的shell命令。

  • 相應命令的形式爲command {} ;,注意{}和;之間的空格。

ok :與exec作用相同,區別在於,在執行命令之前,都會給出提示,讓用戶確認是否執行。

|xargs :與exec作用相同 ,起承接作用,區別在於 |xargs 主要用於承接刪除操作 ,而 -exec 都可用 如複製、移動、重命名等。

options :表示查找方式。

options常用選項:

-name filename        #查找名爲filename的文件
-perm                 #按執行權限來查找
-user username        #按文件屬主來查找
-group groupname      #按組來查找
-mtime -n +n          #按文件更改時間來查找文件,-n指n天以內,+n指n天以前
-atime -n +n          #按文件訪問時間來查找文件,-n指n天以內,+n指n天以前
-ctime -n +n          #按文件創建時間來查找文件,-n指n天以內,+n指n天以前
-nogroup              #查無有效屬組的文件,即文件的屬組在/etc/groups中不存在
-nouser               #查無有效屬主的文件,即文件的屬主在/etc/passwd中不存
-type b/d/c/p/l/f     #查是塊設備、目錄、字符設備、管道、符號鏈接、普通文件
-size n[c]            #查長度爲n塊[或n字節]的文件
-mount                #查文件時不跨越文件系統mount點
-follow               #如果遇到符號鏈接文件,就跟蹤鏈接所指的文件
-prune                #忽略某個目錄

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

1、按名字查找

	# 在當前目錄及子目錄中,查找大寫字母開頭的txt文件 
    $ find . -name '[A-Z]*.txt' -print   

    # 在/etc及其子目錄中,查找host開頭的文件 
    $ find /etc -name 'host*' -print   

    # 在$HOME目錄及其子目錄中,查找所有文件   
    $ find ~ -name '*' -print 

    # 在當前目錄及子目錄中,查找不是out開頭的txt文件   
    $ find . -name "out*" -prune -o -name "*.txt" -print 

2、按目錄查找

	# 在當前目錄除aa之外的子目錄內搜索 txt文件   
    $ find . -path "./aa" -prune -o -name "*.txt" -print   

    # 在當前目錄及除aa和bb之外的子目錄中查找txt文件   
    $ find . −path′./dir0′−o−path′./dir1′−path′./dir0′−o−path′./dir1′ -a -prune -o -name '*.txt' -print

	# 在當前目錄,不再子目錄中,查找txt文件 
    $ find . ! -name "." -type d -prune -o -type f -name "*.txt" -print 
	# 或者
	$ find . -name *.txt -type f -print

3、按權限查找

	# 在當前目錄及子目錄中,查找屬主具有讀寫執行,其他具有讀執行權限的文件   
    $ find . -perm 755 -print 

    # 查找用戶有寫權限或者組用戶有寫權限的文件或目錄
    $ find ./ -perm /220
    $ find ./ -perm /u+w,g+w
    $ find ./ -perm /u=w,g=w

4、按類型查找

	# 在當前目錄及子目錄下,查找符號鏈接文件   
    $ find . -type l -print

5、按屬主及屬組

	# 查找屬主是www的文件   
    $ find / -user www -type f -print   

    # 查找屬主被刪除的文件 
    $ find / -nouser -type f -print   

    # 查找屬組 mysql 的文件 
    $ find / -group mysql -type f -print   

    # 查找用戶組被刪掉的文件 
    $ find / -nogroup -type f -print 

6、按時間查找

	# 查找2天內被更改過的文件 
    $ find . -mtime -2 -type f -print   

    # 查找2天前被更改過的文件 
    $ find . -mtime +2 -type f -print   

    # 查找一天內被訪問的文件 
    $ find . -atime -1 -type f -print   

    # 查找一天前被訪問的文件 
    $ find . -atime +1 -type f -print   

    # 查找一天內狀態被改變的文件 
    $ find . -ctime -1 -type f -print   

    # 查找一天前狀態被改變的文件 
    $ find . -ctime +1 -type f -print   

    # 查找10分鐘以前狀態被改變的文件 
    $ find . -cmin +10 -type f -print

7、按文件新舊查找

	# 查找比 aa.txt 新的文件 
    $ find . -newer "aa.txt" -type f -print   

    # 查找比 aa.txt 舊的文件 
    $ find . ! -newer "aa.txt" -type f -print   

    # 查找比aa.txt新,比bb.txt舊的文件 
    $ find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print

8、按大小查找

	# 查找超過1M的文件 
    $ find / -size +1M -type f -print   

    # 查找等於6字節的文件 
    $ find . -size 6c -print   

    # 查找小於32k的文件 
    $ find . -size -32k -print
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章