- 查找文本内容
• 根据字符串模式提取文本行
– grep [选项] '匹配模式' 文本文件...
– 命令行 | grep [选项] '匹配模式'
• 常用命令选项
– -v,取反匹配
– -i,忽略大小写
[root@server0 ~]# grep 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 -v root /etc/passwd
– ^word 以字符串word开头
– word$ 以字符串word结尾
– ^$ 匹配空行
[root@server0 ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@server0 ~]# grep root$ /etc/passwd
[root@server0 ~]# grep bash$ /etc/passwd
[root@server0 ~]# cat /etc/default/useradd #查看文件内容
[root@server0 ~]# grep ^$ /etc/default/useradd
[root@server0 ~]# grep -v ^$ /etc/default/useradd
在Linux大多数配置文件中,以#开头的行为注释行
请显示文件内容的有效配置(去除空行与注释行)
]# grep -v ^# /etc/login.defs #去除以#开头
]# grep -v ^# /etc/login.defs | grep -v ^$
]# grep -v ^# /etc/login.defs | grep -v ^$ | cat -n
]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/1.txt
- 查找文件
• 根据预设的条件递归查找对应的文件
– find [目录] [条件1] [-a|-o] [条件2] ...
– 常用条件表示:
-type 类型(f文件、d目录、l快捷方式)
-name "文档名称"
-size &#