Day07 - grep、find
2019-05-16 17:58
489 查看
grep 过滤操作
• 根据字符串模式提取文本行
– grep [选项] '匹配模式' 文本文件...
– 命令行 | grep [选项] '匹配模式'
• 常用命令选项
– -v, 取反匹配
– -i, 忽略大小写
– ^..., 以字符串开头
– ...$, 以字符串结尾
– ^$, 匹配空行
[root@server0 ~]# grep 'root' /etc/passwd [root@server0 ~]# grep -v 'root' /etc/passwd [root@server0 ~]# grep 'ROOT' /etc/passwd [root@server0 ~]# grep -i 'ROOT' /etc/passwd [root@server0 ~]# grep '^root' /etc/passwd [root@server0 ~]# grep '/bin/bash$' /etc/passwd
请显示/etc/login.defs配置文件有效的字段?(以#开头,为注释文字)
grep -v '^#' /etc/login.defs grep -v '^#' /etc/login.defs | grep -v '^$'
find 按条件查找文件
• 根据预设的条件递归查找对应的文件
– find [目录] [条件1] [-a|-o] [条件2] ...
– 常用条件表示:
-type 类型 f(文本文件)、d(目录)、l(快捷方式)
-name "文档名称"
-size +|- 文件大小(k、M、G)
-mtime +|- 修改天数
-user 用户名
-group 组名
请查找/etc目录下以“.conf”结尾的文本文件?(不包含子目录)
[root@server0 ~]# ls /etc/*.conf
请查找/etc目录下以“.conf”结尾的文本文件?(包含子目录)
[root@server0 ~]# find /etc/ -name "*.conf"
[root@server0 ~]# find /boot -type f #查找文件 [root@server0 ~]# find /boot -type d #查找目录 [root@server0 ~]# find /boot -type l #查找快捷方式 [root@server0 ~]# find /etc -name "passwd" [root@server0 ~]# touch /root/nsd01.txt [root@server0 ~]# touch /root/nsd02.txt [root@server0 ~]# find /root/ -name "nsd*" [root@server0 ~]# mkdir /root/nsd1705 [root@server0 ~]# find /root/ -name "nsd*" 多个条件同时匹配 [root@server0 ~]# find /root/ -name "nsd*" -type f [root@server0 ~]# find /boot/ -size +300k [root@server0 ~]# find /boot/ -size +10M [root@server0 ~]# find /boot/ -size -10M [root@server0 ~]# find /home/ -user student [root@server0 ~]# find /home/ -group student [root@server0 ~]# find / -user student -mtime +90 [root@server0 ~]# find /root/ -mtime -10 [root@server0 ~]# find /root/ -mtime +1000 [root@server0 ~]# find /var/log -mtime +90
• 使用find命令的 --exec 操作
– find .. .. -exec 处理命令 {} \;
– 优势:以 {} 代替每一个结果,逐个处理,遇 \; 结束
# find /boot/ -size +10M # find /boot/ -size +10M -exec cp {} /opt \; # ls /opt # find / -user student -type f # mkdir /root/findfiles # find / -user student -type f -exec cp {} /root/findfiles \; # ls -A /root/findfiles
相关文章推荐
- grep和find
- linux grep 和 find 命令
- linux grep find 命令
- Linux命令综合使用(find, xargs, grep)
- 查找-find -grep
- 【那些年遇到过的面试题】 grep find 区别
- linux下的find文件查找命令与grep文件内容查找命令
- linux grep、find 命令详解
- find pwd grep cp xargs组合
- find,grep 忽略svn文件
- liunx 编程常用命令(expand,grep ,find )
- find与grep的组合使用
- Linux命令之查找文件、文件查找-find,grep
- linux 实用命令find+grep 备忘
- find grep的用法
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键
- linux下的find文件查找命令与grep文件内容查找命令
- linux下的find文件查找命令与grep文件内容查找命令
- linux下的grep和find命令
- Linux文件查找find &grep区分