4. Linux系统——文件查找过滤
一、查找文件
# find 目录 查找条件
1. 条件的写法
- 按文件名称
[root@martin-host ~]# find /etc/ -name "*.conf"
[root@martin-host ~]# find /etc/ -name "*.conf" | wc -l
465
-iname, 忽略大小写
[root@martin-host ~]# find /dev/ -iname "*sd*"
- 按文件大小查找
[root@martin-host ~]# find /etc/ -size +2000k
[root@martin-host ~]# find /etc/ -size +5M
- 按文件类型
f: 文件d:目录b:块设备文件c:字符设备文件l:软链接
[root@martin-host ~]# find /boot/ -type f
[root@martin-host ~]# find /dev/ -type b
[root@martin-host ~]# find /etc/ -type l | wc -l
- 按文件修改时间
// 一周内
[root@martin-host ~]# find /etc/ -mtime -7
// 一个月前
[root@martin-host ~]# find /boot/ -mtime +30
- 按文件创建时间
[root@martin-host ~]# find /opt/ -ctime -7
- 多条件查找
[root@martin-host ~]# find /etc/ -type f -a -name "*.conf" -a -size +30k
[root@martin-host ~]# find /var/log/ -name "*.log" -a -ctime -7
2. 执行操作
# find 目录 查找条件 -exec 命令 \;
[root@martin-host ~]# find /opt/ -name "*.sql" -exec rm -rf {} \;
[root@martin-host ~]# find /boot/ -size +20M -exec ls -lh {} \;
[root@martin-host ~]# find /boot/ -size +20M -exec du -h {} \;
[root@martin-host ~]# find /boot/ -size +20M -exec cp {} /tmp/ \;
二、文本排序、去重
1. 去重
# uniq 文件
[root@martin-host ~]# uniq /opt/file01
aaaa
bbbb
// -c 按行计数
[root@martin-host ~]# uniq -c /opt/file01
3 aaaa
2 bbbb
2、排序
# sort [选项] 文件
[root@martin-host ~]# sort /opt/file01 | uniq
aaaa
bbbb
// -n 按自然数大小排序
[root@martin-host ~]# sort -n /opt/file02
// -r 倒序,默认为升序
[root@martin-host ~]# sort -n -r /opt/file02
// -k2 按每行第2列排序
[root@martin-host ~]# sort -k2 -n /opt/file02
// -t 指定行分隔符,默认为空白
[root@martin-host ~]# sort -t, -k2 -n /opt/file02
// -h, 按照计算机K,M, G单位排序
[root@martin-host ~]# sort -h /opt/file02
原文地址:https://blog.csdn.net/u010198709/article/details/142627287
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!