歡迎您光臨本站 註冊首頁

Linux find命令用法小結

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

Linux find命令用法小結

Linux 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文件




view plaincopy to clipboard01.# find . -name '*.txt' -print  在/etc及其子目錄中,查找host開頭的文件




view plaincopy to clipboard01.# find /etc -name 'host*' -print   在$HOME目錄及其子目錄中,查找所有文件




view plaincopy to clipboard01.# find ~ -name '*' -print  在當前目錄及子目錄中,查找不是out開頭的txt文件  




view plaincopy to clipboard01.# find . -name "out*" -prune -o -name "*.txt" -print   2、按目錄查找

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




view plaincopy to clipboard01.# find . -path "./aa" -prune -o -name "*.txt" -print在當前目錄及除aa和bb之外的子目錄中查找txt文件




view plaincopy to clipboard01.# find . \( -path "./aa" -o -path "./bb" \) -prune -o -name "*.txt" -print  在當前目錄,不再子目錄中,查找txt文件




view plaincopy to clipboard01.# find .  ! -name "." -type d -prune -o -type f -name "*.txt" -print  3、按許可權查找

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




view plaincopy to clipboard01.# find . -perm 755 -print  4、按類型查找

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




view plaincopy to clipboard01.# find . -type l -print  5、按屬主及屬組

查找屬主是www的文件





view plaincopy to clipboard01.# find / -user www -type f -print  查找屬主被刪除的文件





view plaincopy to clipboard01.# find /  -nouser -type f -print   查找屬組mysql的文件




view plaincopy to clipboard01.# find /  -group mysql -type f  -print  查找用戶組被刪掉的文件




view plaincopy to clipboard01.# find /  -nogroup -type f  -print    6、按時間查找

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




view plaincopy to clipboard01.# find . -mtime -2 -type f -print   查找2天前被更改過的文件




view plaincopy to clipboard01.# find . -mtime +2 -type f -print  查找一天內被訪問的文件




view plaincopy to clipboard01.# find . -atime -1 -type f -print   查找一天前被訪問的文件




view plaincopy to clipboard01.# find . -atime +1 -type f -print  查找一天內狀態被改變的文件




view plaincopy to clipboard01.# find . -ctime -1 -type f -print   查找一天前狀態被改變的文件




view plaincopy to clipboard01.# find . -ctime +1 -type f -print  查找10分鐘以前狀態被改變的文件



view plaincopy to clipboard01.# find . -cmin +10 -type f -print 7、按文件新舊

查找比aa.txt新的文件




view plaincopy to clipboard01.# find . -newer "aa.txt"  -type f -print   查找比aa.txt舊的文件




view plaincopy to clipboard01.# find . ! -newer "aa.txt"  -type f -print 查找比aa.txt新,比bb.txt舊的文件




view plaincopy to clipboard01.# find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print   8、按大小查找

查找超過1M的文件




view plaincopy to clipboard01.# find / -size +1M -type f -print    查找等於6位元組的文件




view plaincopy to clipboard01.# find . -size 6c -print  查找小於32k的文件




view plaincopy to clipboard01.# find . -size -32k -print  9、執行命令

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




view plaincopy to clipboard01.# find . -name 'del.txt' -ok rm {} \;  查找aa.txt 並備份為aa.txt.bak




view plaincopy to clipboard01.# find . -name 'aa.txt' -exec cp {} {}.bak \;
《解決方案》

謝謝分享哦...抱走了

[火星人 ] Linux find命令用法小結已經有704次圍觀

http://coctec.com/docs/service/show-post-1488.html