shell腳本--shell編程中的常用工具find

一.文件查找之find命令

         語法格式
語法格式   |  find[路徑][選項][操作]
                    選項參數對照表

選項                      含義
-name                    根據文件名查找
-iname                   根據文件名查找不區分大小寫
-perm                    根據文件權限查找
-prune                   該選項可以排除某些查找目錄
-user                    根據文件屬主查找
-group                   根據文件屬組查找
-mtime -n | +n           根據文件更改時間進行查找
-nogroup                 查找無有效屬組的文件
-nouser                  查找無有效屬主的文件
-newer file1 ! file2     查找更改時間比file1新但比file2舊IDE文件
-type                    按文件類型查找
-size -n +n              按照文件大小查找
-mindepth n              從n級子目錄開始搜索
-maxdepth n              最多搜索到n級子目錄

 

示例:

$ find /etc -name '*.conf'
/etc/adduser.conf
.....
$ find . -user chencl

./opt/setupwizard/library/main/src/com/android/setupwizardlib/GlifListLayout.java
./opt/setupwizard/library/main/src/com/android/setupwizardlib/view
./opt/setupwizard/library/main/src/com/android/setupwizardlib/view/BottomScrollView.java

-type 文件搜索分爲

f         文件             find . -type f
d         目錄             find . -type d
c         字符設備文件      find . -type c
b         塊設備文件        find . -type b
l         鏈接文件          find . -type l
p         管道文件          find . -type p
$ find . -type d
./opt/setupwizard/library/main/src/com
./opt/setupwizard/library/main/src/com/android

搜索/etc下面大於1M的文件

$ sudo find  /etc -size +100k
/etc/brltty/Contraction/zh-tw.ctb


$ sudo find  /etc -size -100k
/etc/adduser.conf

自定搜索100K大小文件
$ sudo find  /etc -size  100k
/etc/adduser.conf

 三天以內的文件

sudo find  /etc -mtime 3
/etc/skel/模板/XLS 工作表.xls


5天之內修改的文件
$ sudo find  /etc -mtime -5 -name '*.conf'
/etc/cups/printers.conf

三天以外的修改文件 

$ sudo find  /etc -mtime +3

剛好三天文件 

$ sudo find  /etc -mtime 3

從4級目錄開始查找

$ sudo find  /etc -mindepth 4
/etc/brltty/Input/ir/all.kti

最大搜索到第幾級目錄 

sudo find  /etc -maxdepth 1
/etc/adduser.conf

查找沒有用屬組的文件 

$ find . -nouser

對查找到文件之後 對文件進行操作  -exec rm -rf {} \; 爲固定格式 其中的{} 爲我們前面搜索到的文件列表.

# -exec rm -rf {} \; 爲固定格式 其中的{} 爲我們前面搜索到的文件列表.

find ./etc/ -name '*.conf' -exec rm -rf {} \;
rm: 無法刪除'./etc/adduser.conf': 權限不夠
rm: 無法刪除'./etc/init/checkfs.sh.conf': 權限不夠

邏輯運算符

-a            與
-o            或
-not |  !     非

查找到 文件名字爲 .conf 或者 修改大於 7天的文件進行刪除 

$ find /etc/ -size +10k -type f -name '*.conf' -o -mtime +7 -exec rm  -rf {} \; 

 查找非 chencl的用戶組

$ find . -not -user chencl | find . ! -user chencl

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章