shell脚本--shell编程中的常用工具

一.文件查找之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
发布了28 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/ChaoLi_Chen/article/details/103800481