grep,find用法-2

1、显示当前系统上root、fedora或user1用户的默认shell;

grep -E “^(root|fedora|user1)>” /etc/passwd | cut -d: -f1,7
[root@bogon Desktop]# grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash

2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();

grep “[[:alpha:]]+()” /etc/rc.d/init.d/functions
[root@bogon Desktop]# grep "[[:alpha:]]+()" /etc/rc.d/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {

3、使用echo命令输出一个绝对路径,使用grep取出其基名;扩展:取出其路径名;

echo /etc/passwd | grep -o “[^/]+/\?$” passwd
[root@bogon ~]# echo /etc/passwd | grep -o "[^/]\+/\?$"
passwd
[root@bogon ~]#
echo /etc/passwd | grep -oP ‘/.*/(?=.?\b)’
[root@bogon ~]# echo /etc/passwd | grep -oP '/.*/(?=.?\b)'
/etc/
[root@bogon ~]#

4、找出ifconfig命令结果中的1-255之间数字;

ifconfig | grep -E ‘\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))>’
[root@bogon ~]# ifconfig | grep -E '\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))\>'
        inet 192.168.101.15  netmask 255.255.255.0  broadcast 192.168.101.255
        inet6 fe80::20c:29ff:fe00:b610  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:00:b6:10  txqueuelen 1000  (Ethernet)
        RX packets 90557  bytes 114145934 (108.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32344  bytes 15212308 (14.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 4  bytes 340 (340.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 340 (340.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 00:00:00:00:00:00  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@bogon ~]#

5、挑战题:写一个模式,能匹配合理的IP地址;

ifconfig | grep -o -E “(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[0-9][0-9]|1[0-9])”
[root@bogon ~]# ifconfig | grep -o -E "(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9][0-9]|1[0-9])"
192.168.101.15
255.255.255.0
192.168.101.25
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.25
[root@bogon ~]#

6、挑战题:写一个模式,能匹配出所有的邮件地址;

grep -E “([[:alph^C]]|[0-9]|[[:punct:]]*|.|\@)”

7、查找/var目录下属主为root,且属组为mail的所有文件或目录;

find /var/ -user root -group mail -ls
[root@bogon ~]# find /var/ -user root -group mail -ls
100664302    4 drwxrwxr-x   2 root     mail         4096 Aug  2 22:57 /var/spool/mail
[root@bogon ~]#

8、查找当前系统上没有属主或属组的文件;

find / -nouser -nogroup -ls
[root@bogon ~]# find / -nouser -nogroup -ls
find: ‘/proc/11927/task/11927/fd/6’: No such file or directory
find: ‘/proc/11927/task/11927/fdinfo/6’: No such file or directory
find: ‘/proc/11927/fd/6’: No such file or directory
find: ‘/proc/11927/fdinfo/6’: No such file or directory
find: ‘/run/user/1000/gvfs’: Permission denied
   137    0 drwxr-xr-x   2 2006     2006            6 Aug  3 20:44 /root/abc
35461830    0 drwxr-xr-x   2 2006     2006            6 Aug  3 20:44 /root/asdf
68898945    0 drwx------   3 2006     2006           74 Aug  3 20:44 /home/qqq
102702695    0 drwxr-xr-x   4 2006     2006           37 Jul 11 07:27 /home/qqq/.mozilla
1622887    0 drwxr-xr-x   2 2006     2006            6 Jun  9  2014 /home/qqq/.mozilla/extensions
35461829    0 drwxr-xr-x   2 2006     2006            6 Jun  9  2014 /home/qqq/.mozilla/plugins
68898946    4 -rw-r--r--   1 2006     2006           18 Nov 20  2015 /home/qqq/.bash_logout
68898949    4 -rw-r--r--   1 2006     2006          193 Nov 20  2015 /home/qqq/.bash_profile
68898950    4 -rw-r--r--   1 2006     2006          231 Nov 20  2015 /home/qqq/.bashrc
[root@bogon ~]#

9、查找/etc目录下所有用户都有写权限的文件;

find /etc -prem -222 -ls
[root@bogon ~]# find /etc/ -perm -222 -ls
33790768    0 lrwxrwxrwx   1 root     root           10 Jul 11 07:27 /etc/rc6.d -> rc.d/rc6.d
100801421    0 lrwxrwxrwx   1 root     root           16 Jul 11 07:27 /etc/ssl/certs -> ../pki/tls/certs
36511098    0 lrwxrwxrwx   1 root     root           38 Jul 11 07:36 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
33902848    0 lrwxrwxrwx   1 root     root           56 Jul 11 07:27 /etc/favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
34570567    0 lrwxrwxrwx   1 root     root           25 Jul 11 07:32 /etc/pam.d/smtp -> /etc/alternatives/mta-pam
33904308    0 lrwxrwxrwx   1 root     root           14 Jul 11 07:36 /etc/pam.d/system-auth -> system-auth-ac
33904306    0 lrwxrwxrwx   1 root     root           12 Jul 11 07:36 /etc/pam.d/postlogin -> postlogin-ac
33904305    0 lrwxrwxrwx   1 root     root           16 Jul 11 07:36 /etc/pam.d/password-auth -> password-auth-ac
33904303    0 lrwxrwxrwx   1 root     root           19 Jul 11 07:36 /etc/pam.d/fingerprint-auth -> fingerprint-auth-ac
33904307    0 lrwxrwxrwx   1 root     root           17 Jul 11 07:36 /etc/pam.d/smartcard-auth -> smartcard-auth-ac
1660802    0 lrwxrwxrwx   1 root     root           14 Jul 11 07:29 /etc/libvirt/qemu/networks/autostart/default.xml -> ../default.xml
34181389    0 lrwxrwxrwx   1 root     root           19 Aug  1 21:15 /etc/httpd/logs -> ../../var/log/httpd
34181390    0 lrwxrwxrwx   1 root     root           29 Aug  1 21:15 /etc/httpd/modules -> ../../usr/lib64/httpd/modules
34181391    0 lrwxrwxrwx   1 root     root           10 Aug  1 21:15 /etc/httpd/run -> /run/httpd
101468179    0 lrwxrwxrwx   1 root     root           35 Jul 11 07:32 /etc/cifs-utils/idmap-plugin -> /etc/alternatives/cifs-idmap-plugin
71753217    0 lrwxrwxrwx   1 root     root           21 Jul 11 07:31 /etc/gdm/Xsession -> ../X11/xinit/Xsession
33556151    0 lrwxrwxrwx   1 root     root           22 Jul 11 07:32 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
36134996    0 lrwxrwxrwx   1 root     root            8 Jul 11 07:32 /etc/scl/conf -> prefixes

10、查找/etc目录下大于1M,且类型为普通文件的所有文件;

find /etc -size +1M -ls
[root@bogon ~]# find /etc/ -size +1M -ls
72502453 3772 -rw-r--r--   1 root     root      3858924 Nov 20  2015 /etc/selinux/targeted/policy/policy.29
36328392 6852 -r--r--r--   1 root     root      7014922 Jul 11 07:37 /etc/udev/hwdb.bin
101554968 1336 -rw-r--r--   1 root     root      1367395 Mar  5  2015 /etc/brltty/zh-tw.ctb
[root@bogon ~]#

原创文章,作者:N27_Danry,如若转载,请注明出处:http://www.178linux.com/83620

(0)
N27_DanryN27_Danry
上一篇 2017-08-04
下一篇 2017-08-04

相关推荐

  • bash脚本之函数及循环特殊用法

    一,概述 while的循环特殊用法(遍历文件的每一行)     while read line;do         循环体        &nbsp…

    Linux干货 2016-08-29
  • 回溯法 -数据结构与算法

    1.回溯法算法思想: 定义:         回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。 1、回溯法适用:有许多问题,当需要找出它的解集(…

    Linux干货 2015-04-07
  • 用户和组的管理

    前言 服务器最主要的工作是提供可靠的服务,提供服务就必须对外开放自己的网络,可靠就需要一定的机制来保证了。Linux中有一个3A的机制,首先是认证,就是我们经常听到的一句话,怎么证明你就是你;其次是授权,管理一个服务器,每个管理员都有自己的职责,那么我们就只分配对应的权限给特定的人,这样就可以明确事故的责任,从源头甩锅;最后是审计,总有一些黑客可以通过各种手…

    Linux干货 2016-10-22
  • https实现

    实现https 搭建CA 颁发证书

    2018-01-29
  • 【N25第六周作业】VIM、crontab、简单脚本

    请详细总结vim编辑器的使用并完成以下练习题 第二周有写过vim的用法,请查看连接: 周期性任务计划:cron 服务程序: cronie:主程序包,提供了crond守护进程及相关辅助工具; 确保crond守护进程(daemon)处于运行状态: CentOS 7: systemctl  status  cron…

    Linux干货 2016-12-27
  • hadoop分布式集群

    分布式集群的实现 环境: 192.168.36.129   node1:NN,SNN,RM  192.168.36.130  node2:DN,NM 192.168.36.132   node3:DN,NM 192.168.36.133   node4:DN,NM 配置java环境,每个节点时间同步,基于主机名互相通信,定义/etc/hosts 文件 &nb…

    2017-12-09