选项与参数 -a 将二进制文件以text文件方式搜寻数据 -c 计算找到搜寻字符串的个数 -i 不区分大小写 -n 输出行号 -v 反向选择 选择不包含搜索字符串的选项 -l 列出含有搜索字符串的文件名 -r 在制定目录中递归查找 –color=auto ‘搜索字符串’ 将找到的关键字加上颜色 注释:可以 vim ~/.bashrc 在文件中加上 (1)alias grep=‘grep --color=auto’ (2) source ~/.bashrc 就可以不用每次都写这个参数了 例子: 1.将/etc/passwd 有出现root的行取出来
# grep root /etc/passwd 或者 cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin2.将/etc/passwd ,有出现root的行取出来,同时显示这些行在/etc/passwd的行号
#grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin3.将/etc/passwd 中没有出现root的行取出来
#grep -v root /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin4.将/etc/passswd 没有出现root和nologin 的行取出来
#grep -v root /etc/passwd | grep -v nologin /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt syslog:x:498:498::/home/syslog:/bin/false5.用dmesg 列出核心信息,再以grep找出内含eth那行,要将捉到的关键字显色,加上行号来表示
[root@www ~]#dmesg | grep -n --color=auto 'su' 1:Initializing cgroup subsys cpuset 2:Initializing cgroup subsys cpu 4:Command line: ro root=/dev/vda1 console=ttyS0 console=tty0 panic=5 rd_NO_LUKS rd_NO_LVM LANG=C rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM 5:KERNEL supported cpus: 109:Kernel command line: ro root=/dev/vda1 console=ttyS0 console=tty0 panic=5 rd_NO_LUKS rd_NO_LVM LANG=C rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=129M@0M KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM 130:Initializing cgroup subsys ns 131:Initializing cgroup subsys cpuacct 132:Initializing cgroup subsys memory 133:Initializing cgroup subsys devices 134:Initializing cgroup subsys freezer 135:Initializing cgroup subsys net_cls 136:Initializing cgroup subsys blkio 137:Initializing cgroup subsys perf_event 138:Initializing cgroup subsys net_prio 139:CPU: Unsupported number of siblings 14 140:mce: CPU supports 10 MCE banks 152:Performance Events: unsupported p6 CPU model 79 no PMU driver, software events only. 172:ACPI: (supports S0 S3 S4 S5) 208:SCSI subsystem initialized 532:[ 7446] 0 7446 36256 93 0 0 0 su 641:[ 7446] 0 7446 36256 93 0 0 0 su 740:[22131] 503 22131 36256 96 0 0 0 su 742:[22185] 0 22185 36256 93 0 0 0 su6.用dmesg 列出核心信息,再已grep找出内含supported那行,在关键字所在行的前两行与后三行也一起取出来显示
[root@www ~]#dmesg | grep -n -A3 -B2 --color=auto 'supported' 3-Linux version 2.6.32-642.6.2.el6.x86_64 (mockbuild@worker1.bsys.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Wed Oct 26 06:52:09 UTC 2016 4-Command line: ro root=/dev/vda1 console=ttyS0 console=tty0 panic=5 rd_NO_LUKS rd_NO_LVM LANG=C rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM 5:KERNEL supported cpus: 6- Intel GenuineIntel 7- AMD AuthenticAMD 8- Centaur CentaurHauls -- 137-Initializing cgroup subsys perf_event 138-Initializing cgroup subsys net_prio 139:CPU: Unsupported number of siblings 14 140-mce: CPU supports 10 MCE banks 141-alternatives: switching to unfair spinlock 142-SMP alternatives: switching to UP code -- 150-..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1 151-CPU0: Intel(R) Xeon(R) CPU E5-26xx v4 stepping 01 152:Performance Events: unsupported p6 CPU model 79 no PMU driver, software events only. 153-NMI watchdog disabled (cpu0): hardware events not enabled 154-Brought up 1 CPUs 155-Total of 1 processors activated (4788.89 BogoMIPS).7.根据文件内容递归查找目录
#grep 'supported' * #在当前目录搜索带’supported‘行的文件 #grep -r 'supported' * #在当前目录及子目录下搜索'supported' 行的文件 #grep -l -r 'supported' * #在当前目录及其子目录下搜索'supported'行文件,但是不显示匹配的行,只显示匹配的文件