Linux常用命令----grep命令
作者:redrose2100   类别:    日期:2022-12-14 15:22:05    阅读:987 次   消耗积分:0 分

一、grep命令使用方法

1.1 grep 命令格式

  1. grep [选项] '关键字' 文件名

1.2 grep命令常用的选项

  • -i: 不区分大小写
  • -v: 查找不包含指定内容的行,反向选择
  • -w: 按单词搜索
  • -o: 打印匹配关键字
  • -c: 统计匹配到的次数
  • -n: 显示行号
  • -r: 逐层遍历目录查找
  • -A: 显示匹配行及后面多少行
  • -B: 显示匹配航及前面多少行
  • -C: 显示匹配行前后多少行
  • -l: 值列出匹配的文件名
  • -L: 列出不匹配的文件名
  • -e: 使用正则匹配
  • -E: 使用扩展正则匹配
  • ^key: 以关键字开头

二、grep命令使用实例

2.1 从文件中查找指定字符串的行

从 /etc/passwd 文件中查找包含root的行

  1. [root@centos7-1 /]# grep 'root' /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. [root@centos7-1 /]#

2.2 从文件中查找指定字符串的行并显示所在行号

从 /etc/passwd 文件中查看包含root的行在第几行

  1. [root@centos7-1 /]# grep -n 'root' /etc/passwd
  2. 1:root:x:0:0:root:/root:/bin/bash
  3. 10:operator:x:11:0:operator:/root:/sbin/nologin
  4. [root@centos7-1 /]#

2.3 从文件中查找包含指定字符串的行,忽略大小写查找,并显示行号

从 /etc/passwd 文件中找出包含user行并显示行号,忽略大小写

  1. [root@centos7-1 /]# grep -ni 'user' /etc/passwd
  2. 12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  3. 16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
  4. 24:saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
  5. 25:qemu:x:107:107:qemu user:/:/sbin/nologin
  6. 26:radvd:x:75:75:radvd user:/:/sbin/nologin
  7. 29:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
  8. 30:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
  9. 32:saned:x:993:987:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
  10. 34:colord:x:991:985:User for colord:/var/lib/colord:/sbin/nologin
  11. 36:geoclue:x:990:984:User for geoclue:/var/lib/geoclue:/sbin/nologin
  12. 38:sssd:x:989:983:User for sssd:/:/sbin/nologin
  13. 39:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
  14. 40:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
  15. [root@centos7-1 /]#

2.4 从文件中查找以指定字符串开头的行

从 /etc/passwd 文件找出以root开头的行

  1. [root@centos7-1 /]# grep '^root' /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. [root@centos7-1 /]#

2.5 从文件中查找以指定字符串结尾额行

从 /etc/passwd 文件中找出以bash结尾的行

  1. [root@centos7-1 /]# grep 'bash$' /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. redrose2100:x:1000:1000:redrose2100:/home/redrose2100:/bin/bash
  4. [root@centos7-1 /]#

2.6 从文件中反向查找,即将查找符合条件以外的行

从 /etc/passwd 文件中找出不以root开头的行

  1. [root@centos7-1 /]# grep -v '^root' /etc/passwd
  2. bin:x:1:1:bin:/bin:/sbin/nologin
  3. daemon:x:2:2:daemon:/sbin:/sbin/nologin
  4. adm:x:3:4:adm:/var/adm:/sbin/nologin
  5. lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  6. sync:x:5:0:sync:/sbin:/bin/sync
  7. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  8. halt:x:7:0:halt:/sbin:/sbin/halt
  9. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  10. operator:x:11:0:operator:/root:/sbin/nologin
  11. games:x:12:100:games:/usr/games:/sbin/nologin
  12. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  13. nobody:x:99:99:Nobody:/:/sbin/nologin
  14. systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
  15. dbus:x:81:81:System message bus:/:/sbin/nologin
  16. polkitd:x:999:998:User for polkitd:/:/sbin/nologin
  17. sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
  18. postfix:x:89:89::/var/spool/postfix:/sbin/nologin
  19. chrony:x:998:996::/var/lib/chrony:/sbin/nologin
  20. rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
  21. gluster:x:997:993:GlusterFS daemons:/run/gluster:/sbin/nologin
  22. libstoragemgmt:x:996:992:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
  23. unbound:x:995:991:Unbound DNS resolver:/etc/unbound:/sbin/nologin
  24. saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
  25. qemu:x:107:107:qemu user:/:/sbin/nologin
  26. radvd:x:75:75:radvd user:/:/sbin/nologin
  27. rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
  28. ntp:x:38:38::/etc/ntp:/sbin/nologin
  29. tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
  30. usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
  31. pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
  32. saned:x:993:987:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
  33. setroubleshoot:x:992:986::/var/lib/setroubleshoot:/sbin/nologin
  34. colord:x:991:985:User for colord:/var/lib/colord:/sbin/nologin
  35. abrt:x:173:173::/etc/abrt:/sbin/nologin
  36. geoclue:x:990:984:User for geoclue:/var/lib/geoclue:/sbin/nologin
  37. gdm:x:42:42::/var/lib/gdm:/sbin/nologin
  38. sssd:x:989:983:User for sssd:/:/sbin/nologin
  39. rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
  40. nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
  41. gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
  42. tcpdump:x:72:72::/:/sbin/nologin
  43. avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
  44. redrose2100:x:1000:1000:redrose2100:/home/redrose2100:/bin/bash
  45. [root@centos7-1 /]#

2.7 从文件查找符合条件的行以及之前的N行

从 /etc/passwd 文件中找出以ftp开头的行以及它前面的三行

  1. [root@centos7-1 /]# grep -B 3 '^ftp' /etc/passwd
  2. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. games:x:12:100:games:/usr/games:/sbin/nologin
  5. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  6. [root@centos7-1 /]#

2.8 从文件中查找符合条件的行以及之后的N行

从 /etc/passwd 文件中找出以ftp开头的行以及它之后的三行

  1. [root@centos7-1 /]# grep -A 3 '^ftp' /etc/passwd
  2. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  3. nobody:x:99:99:Nobody:/:/sbin/nologin
  4. systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
  5. dbus:x:81:81:System message bus:/:/sbin/nologin
  6. [root@centos7-1 /]#

2.9 从文件中查找符合条件的行以及前后N行

从 /etc/passwd 文件中找出以ftp开头的行以及它前后的三行

  1. [root@centos7-1 /]# grep -C 3 '^ftp' /etc/passwd
  2. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. games:x:12:100:games:/usr/games:/sbin/nologin
  5. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
  6. nobody:x:99:99:Nobody:/:/sbin/nologin
  7. systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
  8. dbus:x:81:81:System message bus:/:/sbin/nologin
  9. [root@centos7-1 /]#

2.10 从文件中按照单词查找符合条件的行

从 /etc/passwd 文件中找出带user单词的行,注意必须是user单词,如果是单词中含有user是不符合要求的。

如下,可以看出通过-w参数即可以做到按照单词来筛选。

  1. [root@centos7-1 /]# grep 'user' /etc/passwd
  2. saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
  3. qemu:x:107:107:qemu user:/:/sbin/nologin
  4. radvd:x:75:75:radvd user:/:/sbin/nologin
  5. tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
  6. usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
  7. saned:x:993:987:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
  8. rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
  9. [root@centos7-1 /]#
  10. [root@centos7-1 /]# grep -w 'user' /etc/passwd
  11. saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
  12. qemu:x:107:107:qemu user:/:/sbin/nologin
  13. radvd:x:75:75:radvd user:/:/sbin/nologin
  14. usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
  15. saned:x:993:987:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
  16. [root@centos7-1 /]#

2.11 显示查找到的符合条件的行数

从 /etc/passwd 文件中统计出现root的行数,如下,使用-c参数即可

  1. [root@centos7-1 /]# grep 'root' /etc/passwd
  2. root:x:0:0:root:/root:/bin/bash
  3. operator:x:11:0:operator:/root:/sbin/nologin
  4. [root@centos7-1 /]#
  5. [root@centos7-1 /]#
  6. [root@centos7-1 /]# grep -c 'root' /etc/passwd
  7. 2
  8. [root@centos7-1 /]#

2.12 从目录下所有文件中查找符合条件的文件以及所在的行的内容

在 /etc 目录下递归的查找所有含有username的文件即所在的行的内容

  1. [root@centos7-1 /]# grep -r 'username' /etc
  2. /etc/security/chroot.conf:# username_regex chroot_dir
  3. /etc/sasl2/qemu-kvm.conf:# Default to a simple username+password mechanism
  4. /etc/sasl2/qemu-kvm.conf:# If using digest-md5 for username/passwds, then this is the file
  5. /etc/sasl2/qemu-kvm.conf:# containing the passwds. Use 'saslpasswd2 -a qemu [username]'
  6. /etc/sasl2/libvirt.conf:# 'scram-sha-1' plugin allows plain username/password authentication
  7. /etc/sasl2/libvirt.conf:# If using scram-sha-1 for username/passwds, then this is the file
  8. /etc/sasl2/libvirt.conf:# containing the passwds. Use 'saslpasswd2 -a libvirt [username]'
  9. /etc/vmware-tools/tools.conf.example:# /var/log/vmware-<servicename>-<username>.log
  10. /etc/postfix/main.cf:# In the left-hand side, specify a bare username, an @domain.tld
  11. /etc/postfix/main.cf:# username->Firstname.Lastname mapping.
  12. /etc/postfix/main.cf:# Other environment variables of interest: USER (recipient username),
  13. /etc/postfix/main.cf:# username), $shell (recipient shell), $home (recipient home directory),
  14. /etc/postfix/virtual:# own user name space. Local (i.e. non-virtual) usernames
  15. /etc/iscsi/iscsid.conf:# To set a CHAP username and password for initiator
  16. /etc/iscsi/iscsid.conf:#node.session.auth.username = username
  17. /etc/iscsi/iscsid.conf:# To set a CHAP username and password for target(s)
  18. /etc/iscsi/iscsid.conf:#node.session.auth.username_in = username_in
  19. /etc/iscsi/iscsid.conf:# To set a discovery session CHAP username and password for the initiator
  20. /etc/iscsi/iscsid.conf:#discovery.sendtargets.auth.username = username
  21. /etc/iscsi/iscsid.conf:# To set a discovery session CHAP username and password for target(s)
  22. /etc/iscsi/iscsid.conf:#discovery.sendtargets.auth.username_in = username_in
  23. /etc/wgetrc:#header = From: Your Name <username@site.domain>
  24. /etc/libreport/events.d/abrt_event.conf:# Record username only if uid element is present:
  25. /etc/libreport/events.d/abrt_event.conf: if [ -f uid ]; then getent passwd "`cat uid`" | cut -d: -f1 >username; fi
  26. /etc/libreport/plugins/mantisbt_format.conf: -username,-hostname,-os_release,-os_info,\
  27. /etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf: -username,-hostname,-os_release,-os_info,\
  28. /etc/libreport/plugins/mantisbt_formatdup.conf: -username,-hostname,-os_release,-os_info,\
  29. /etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf: -username,-hostname,-os_release,-os_info,\
  30. /etc/libreport/plugins/bugzilla_format.conf: -username,-hostname,-os_release,-os_info,\
  31. /etc/libreport/plugins/bugzilla_format_libreport.conf: -username,-hostname,-os_release,-os_info,\
  32. /etc/libreport/plugins/bugzilla_formatdup.conf: -username,-hostname,-os_release,-os_info,\
  33. /etc/libreport/plugins/bugzilla_format_anaconda.conf: -username,-hostname,-os_release,-last_occurrence,-ureports_counter,\
  34. /etc/libreport/plugins/bugzilla_formatdup_anaconda.conf: -username,-hostname,-os_release,-last_occurrence,-ureports_counter,\
  35. /etc/libreport/plugins/ureport.conf:# Use username and password:
  36. /etc/libreport/plugins/ureport.conf:# HTTPAuth = username:password
  37. /etc/libreport/forbidden_words.conf:username
  38. /etc/dnsmasq.conf:#dhcp-option=encap:175, 190, user # iSCSI username
  39. /etc/libvirt/libvirtd.conf:# A whitelist of allowed SASL usernames. The format for username
  40. /etc/libvirt/libvirtd.conf:# depends on the SASL authentication mechanism. Kerberos usernames
  41. /etc/libvirt/libvirtd.conf:# look like username@REALM
  42. /etc/libvirt/libvirtd.conf:#sasl_allowed_username_list = ["joe@EXAMPLE.COM", "fred@EXAMPLE.COM" ]
  43. /etc/GeoIP.conf:# ProxyUserPassword username:password
  44. [root@centos7-1 /]#

2.13 从指定目录中查找含有自定字符串的所有文件

查找 /etc/ 目录下所有含有username的文件

  1. [root@centos7-1 /]# grep -rl 'username' /etc
  2. /etc/security/chroot.conf
  3. /etc/sasl2/qemu-kvm.conf
  4. /etc/sasl2/libvirt.conf
  5. /etc/vmware-tools/tools.conf.example
  6. /etc/postfix/main.cf
  7. /etc/postfix/virtual
  8. /etc/iscsi/iscsid.conf
  9. /etc/wgetrc
  10. /etc/libreport/events.d/abrt_event.conf
  11. /etc/libreport/plugins/mantisbt_format.conf
  12. /etc/libreport/plugins/mantisbt_format_analyzer_libreport.conf
  13. /etc/libreport/plugins/mantisbt_formatdup.conf
  14. /etc/libreport/plugins/mantisbt_formatdup_analyzer_libreport.conf
  15. /etc/libreport/plugins/bugzilla_format.conf
  16. /etc/libreport/plugins/bugzilla_format_libreport.conf
  17. /etc/libreport/plugins/bugzilla_formatdup.conf
  18. /etc/libreport/plugins/bugzilla_format_anaconda.conf
  19. /etc/libreport/plugins/bugzilla_formatdup_anaconda.conf
  20. /etc/libreport/plugins/ureport.conf
  21. /etc/libreport/forbidden_words.conf
  22. /etc/dnsmasq.conf
  23. /etc/libvirt/libvirtd.conf
  24. /etc/GeoIP.conf
  25. [root@centos7-1 /]#

2.14 从指定目录中查找所有不含有自定字符串的文件

查找 /etc/ 目录下所有不含有username的文件

  1. grep -rL 'username' /etc
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/503
个人成就
  • 2022年 : 371 篇 
  • 2023年 : 211 篇 
  • 2024年 : 31 篇 
  • 2025年 : 0 篇 
  • 博客总数: 613 
  • 阅读总量: 643131 
测试开发技术全栈公众号
DevOps技术交流微信群