find 的經驗總結

1,當前目錄查找*.java
find . -name "*.java"
find . -name \*.java



2,列出當前目錄所有文件,類似  ls -l  ,但是帶路徑
find . -print



3,避免因爲沒有權限而輸出錯誤
find /usr /home  /tmp -name "*.jar" 2>/dev/null



4,查找名字不區分大小寫
find . -iname "*.gif"



5,按照類型搜索
搜索所有子目錄
find . -type d
搜索所有link
find . -type l



6,按照文件變化時間查找
mtime — 文件內容上次修改時間 
atime — 文件被讀取或訪問的時間
ctime — 文件狀態變化時間

查找最近1小時內修改過的文件
find . -mtime -1
結果出來了:可以用下面兩個形式:
find . -mtime -4 -mtime +2
find . -mtime +2 -mtime -4
都是找出在4天之內2天以前修改過的文件


find . -atime +10
查找10天前的

-newer 指內容最近被修改的文件
-anewer 指最近被讀取過的文件
-cnewer 指狀態最近發生變化的文件

find . -newer  backup.tar.gz




7,查找完後,繼續輸出給其他命令
查找類型爲file,並且大小小於100字節,輸出給ls -l
find . -type f -size -100 -exec ls -l {} \;
find . -type f -size +50000000c -exec ls -l {} \;
find . -type f -size +100000000c -exec ls -l {} \;

在test文件夾內查找大小爲0的文件,移動到/tmp/zerobyte
find test -type f  -size 0 -exec mv {} /tmp/zerobyte \;

查找空文件
find test -empty 



8,按權限查找
在當前目錄查找文件權限爲777,然後ls -l
find . -type f -perm 777 -exec ls -l {} \;



9,按用戶名查找
查找文件屬於bluher的,ls -l
find / -type f -user bluher -exec ls -ls {}  \;

查找屬於users用戶組的文件
find /  -type f -group users

查找group id等於100的目錄
find / -type d -gid  100


10,目錄深度
查找當前目錄下3層子文件夾中的log文件
find . -maxdepth 3  -name "*log"

-depth 選項確保先在一個目錄中進行查找,然後纔在其子目錄中進行查找。以下命令提供了一個示例:
find -name "*test*" -depth
./test/test
./test
./localbin/test
./localbin/test_shell_var
./localbin/test.txt
./test2/test/test
./test2/test
./test2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章