Shell查找命令find和grep的具体使用

Nita ·
更新时间:2024-09-20
· 263 次阅读

目录

第一节 文件查找之find命令

第二节 find、locate、whereis和which总结

第三节 grep和egrep

第一节 文件查找之find命令

语法格式:find [路径] [选项] [操作]

常用选项

# - name 查找/etc目录下以conf结尾的文件 区分大小写 find /etc -name '*conf' # -iname 查找当前目录下文件名为aa的文件,不区分大小写 find . -iname aa # -user 查找文件属主为hdfs的所有文件 find . -user hdfs # -group 查找文件属组为yarn的所有文件 find . -group yarn # -type # f 文件 find . -type f # d 目录 find . -type d # c 字符设备文件 find . -type c # b 块设备文件 find . -type b # l 链接文件 find . -type 1 # 管道文件 find . -type p # -size # -n 大小小于n的文件 # +n 大小大于n的文件 # n 大小等于n的文件 # 例子1:查找/etc目录下小于1000字节的文件 find /etc -size -10000c # 例子2:查找/etc目录下大于1M的文件 find /etc -size +1M # -mtime # -n天以内修改的文件 # +n n天以外修改的文件 # n 正好n天修改的文件 # 例子1:查找/etc目录下5天之内修改且以conf结尾的文件 find /etc -mtime -5 -name '*.conf' # 例子2:查找/etc目录下10天之前修改且属主为root的文件 find /etc -mtime +10 -user root # -mmin # -n n分钟以内修改的文件 # +n n分钟以外修改的文件 # 例子1:查找/etc目录下30分钟之前修改的文件 find /etc -mmin +30 # 例子2:查找/etc目录下30分钟之内修改的目录 find /etc -mnin -30 -type d # -mindepth n 表示从n级子目录开始搜索 # 例子:在/etc下的3级子目录开始搜索 find /etc -mindepth 3 # -maxdepth n 表示最多搜索到n-1级子目录 # 例子1:在/etc下搜索符合条件的文件,但最多搜索到2级子目录 find /etc -maxdepth 3 -name '*.conf' # 例子2: find ./etc/ -type f -name '*.conf' -size +10k -maxdepth 2

了解选项

# -nouser 查找没有属主的用户 find . -type f -nouser # -nogroup 查找没有属组的用户 find . -type f -nogroup # -perm 根据权限查找 find . -perm 664 # -prune 通常和-path一起使用,用于将特定目录排除在搜索条件之外 # 例子1:查找当前目录下所有普通文件,但排除etc目录 find . -path ./etc -prune -o -type f # 例子2:查找当前目录下所有普通文件,但排除etc和opt目录 find . -path ./etc -prune -o -path ./opt -prune -o -type f # 例子3:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs find . -path ./etc -prune -o -path ./opt -prune -o type f -a -user hdfs # 例子4:查找当前目录下所有普通文件,但排除etc和opt目录,但属主为hdfs,且文件大小必须大于500字 find . -path ./etc -prune -o -path ./opt -prune -o -type f -a -user hdfs -a -size +500c # -newer file1 比file1新的文件 find /etc -newer a

对查找到的文件操作

# -print 打印输出 # -exec 对搜索到的文件执行特定的操作,格式为-exec 'command'{} \; # {} 表示前面搜索到的结果 固定写法{} \ # 例子1:搜索/etc下的文件(非目录),文件名以conf结尾且大于10k,然后将其删除 find ./etc/ -type f -name '*.conf' -size +10k -exec rm -f {} \; # 例子2:将/var/1og/目录下以1og结尾的文件,且更改时间在7天以上的删除 find /var/log/ -name '*.1og' -mtime +7 -exec rm -rf {} \; # 例子3:搜索条件和例子1一样,只是不删除,而是将其复制到/root/conf目录下 find ./etc/ -size +10k -type f -name '*.conf' -exec cp {} /root/conf/ \; # -ok 和exec功能一样,只是每次操作都会给用户提示 # 逻辑运算符: # -a 与 # -o 或 # -not|! 非 # 例子1:查找当前目录下,属主不是hdfs的所有文件 find . -not -user hdfs | find .! -user hdfs # 例子2:查找当前目录下,属主属于hdfs,且大小大于300字节的文件 find . -type f -a -user hdfs -a -size +300c # 例子3:查找当前目录下的属主为hdfs或者以xm1结尾的普通文件 find . -type f -a \( -user hdfs -o -name '*.xml') 第二节 find、locate、whereis和which总结

locate命令介绍:

文件查找命令,所属软件包mlocate

不同于find命令是在整块磁盘中搜索,locate命令在数据库文件中查找

find就会很影响IO性能,locate会归档到数据库中,文件查找就会很快。

find是默认全部匹配,locate则是默认部分匹配

通过updatedb命令及时更新locate命令的数据库,做到及时查到,否则就要等到它自动归档到数据库中。

yum -y install mlocate # 用户更新/var/lib/mlocate/mlocate.db # 所使用配置文件/etc/updatedb.conf # 该命令在后台cron计划任务中定期执行 updatedb # 会遍历整个文件系统 会比较慢 locate my.cnf

whereis

-b 只返回二进制文件

-m 只返回帮助文档文件

-s 只返回源代码文件

whereis mysql whereis -b mysql whereis -m mysql

which 作用∶仅查找二进制程序文件

-b 只返回二进制文件

which mysql

第三节 grep和egrep

第一种形式: grep [option] [pattern] [file1,file2...]

第二种形式:command \ grep [option] [pattern]

# 必须掌握的选项: # -v 最示不匹配pattern的行 排除包含字符的内容 # -i 搜索时忽略大小写 # -n 显示行号 # -E 支持扩展的正则表达式 # -F 不支持正则表达式,按字符串的字面意思进行匹配 # -r 递归搜索 # file i love python .lovelove python lovelove LOVE PYTHON Love pYtHoN grep python file # file文件包含python的行 grep -v python file # file文件不包括python的文件 grep -iv python file # file文件不包括python的文件 忽略大小写 grep -n python file # file文件包含python的行 并显示行号 # 下面的|就是拓展正则表达式 grep "python | PYTHON" file grep -E "python | PYTHON" file # -F 不支持正则表达式,按字符串的字面意思进行匹配 grep "py.*" file grep -F "py.*" file # 需了解的选项: # -c 只输出匹配行的数量,不显示具体内容 # -w 匹配整词 # -x 匹配整行 # -l 只列出匹配的文件名,不显示具体匹配行内容 grep -c python file grep -w love file grep -x "i love python" file

grep和egrep:

grep默认不支持扩展正则表达式,只支持基础正则表达式使用

grep -E可以支持扩展正则表达式

使用egrep可以支持扩展正则表达式,与grep -E等价

到此这篇关于 Shell查找命令find和grep的具体使用的文章就介绍到这了,更多相关 Shell查找命令find和grep内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



shell grep find

需要 登录 后方可回复, 如果你还没有账号请 注册新账号