2016全新Linux+Python高端运维班-Linux grep命令及基本正则表达式

简介:

本周作业内容:

1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
     第一种方式:
         [root@liu /] #chmod -R go=--- /home/tuser1
         [root@liu tuser1] # ls -l /home/tuser1/
         总用量 4
         -rw-------. 1 root root 47 8月  25 00:01 issue
     第二种方式:
         [root@liu /] # rm -rf /home/tuser1/
         [root@liu /] # cp -r /etc/skel/ /home/tuser1
         [root@liu /] # chmod -R g-rwx /home/tuser1/
         [root@liu /] # chmod -R o-rwx /home/tuser1/
         [root@liu /] # ls -l /home/tuser1/
         总用量 4
         -rw-------. 1 root root 47 8月  25 00:12 issue
         [root@liu /] # ls -ld /home/tuser1/
         drwx------. 4 root root 4096 8月  25 00:12  /home/tuser1/


2、编辑/etc/group文件,添加组hadoop。

1
2
3
4
5
6
7
     第一种方式:
         [root@liu /] # echo "hadoop:x:3008:" >> /etc/group
     第二种方式:
         vim  /etc/group
         在最下面添加一行:
         hadoop:x:3009:
         保存退出。


3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。 

1
2
3
4
5
6
vim  /etc/passwd 
     在最下面添加一行:
     hadoop:x:3008:3008:: /home/hadoop : /bin/bash
     保存退出。
     [root@liu /] # id hadoop
     uid=3008(hadoop) gid=3008(hadoop) 组=3008(hadoop)

    
4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

1
2
3
4
5
6
7
8
9
10
11
12
  [root@liu /] # cp -r /etc/skel/ /home/hadoop
     [root@liu /] # chmod -R go=--- /home/hadoop
     [root@liu /] # ls -la /home/hadoop
     总用量 32
     drwx------.  4 root root 4096 8月  25 00:33 .
     drwxr-xr-x. 19 root root 4096 8月  25 00:33 ..
     -rw-------.  1 root root   18 8月  25 00:33 .bash_logout
     -rw-------.  1 root root  176 8月  25 00:33 .bash_profile
     -rw-------.  1 root root  124 8月  25 00:33 .bashrc
     drwx------.  2 root root 4096 8月  25 00:33 .gnome2
     -rw-------.  1 root root   47 8月  25 00:33 issue
     drwx------.  4 root root 4096 8月  25 00:33 .mozilla


5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。
知识点
        修改文件的属主:chown
            chown [OPTION]... [OWNER][:[GROUP]] FILE...

                用法:
                    OWNER
                    OWNER:GROUP
                    :GROUP

                    Note: 命令中的冒号可用.替换;

                -R: 递归
            chown [OPTION]... --reference=RFILE FILE...

1
2
3
     [root@liu /] # chown -R hadoop:hadoop /home/hadoop/
     [root@liu /] # id hadoop
     uid=3008(hadoop) gid=3008(hadoop) 组=3008(hadoop)

    
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     [root@liu /] # grep '^[Ss]' /proc/meminfo
     SwapCached:            0 kB
     SwapTotal:       2097144 kB
     SwapFree:        2097144 kB
     Shmem:              3296 kB
     Slab:              46576 kB
     SReclaimable:      13148 kB
     SUnreclaim:        33428 kB
     [root@liu /] # grep -i "^s" /proc/meminfo
     SwapCached:            0 kB
     SwapTotal:       2097144 kB
     SwapFree:        2097144 kB
     Shmem:              3296 kB
     Slab:              46584 kB
     SReclaimable:      13160 kB
     SUnreclaim:        33424 kB


7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
知识点:
        grep [OPTIONS] PATTERN [FILE...]

            选项:
                --color=auto: 对匹配到的文本着色显示;
                -v: 显示不能够被pattern匹配到的行;
                -i: 忽略字符大小写;
                -o: 仅显示匹配到的字符串;
                -q: 静默模式,不输出任何信息;
                -A #:after, 后#行
                -B #: before, 前#行
                -C #:context, 前后各#行

                -E:使用ERE;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     [root@liu /] # grep -v "/sbin/nologin" /etc/passwd | cut -d: -f1
     root
     sync
     shutdown
     halt
     liu
     bash
     basher
     testbash
     centos
     useradd1
     mageia
     slackware
     openstacks
     user1
     user2
     user3
     hadoop


8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
     [root@liu /] # grep  "/bin/bash" /etc/passwd | cut -d: -f1
     root
     liu
     bash
     basher
     testbash
     centos
     useradd1
     mageia
     openstacks
     user1
     user2
     user3
     hadoop


9、找出/etc/passwd文件中的一位数或两位数;
知识点:
        基本正则表达式元字符:
            字符匹配:
                .:  匹配任意单个字符;
                []: 匹配指定范围内的任意单个字符
                [^]:匹配指定范围外的任意单个字符
                    [:digit:]、[:lower:]、[:upper:]、[:alpha:]、[:alnum:]、[:punct:]、[:space:]
            
            匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数;
                *:匹配前面的字符任意次;
                    例如: grep "x*y" 
                        abxy
                        xay
                        xxxxxxy

                    贪婪模式
                .*:任意长度的任意字符;
                \?:匹配其前面的字符0或1次;即前面的可有可无;
                \+:匹配其前面的字符至少1次;
                \{m\}:匹配前面的字符m次;
                \{m,n\}:匹配前面的字符至少m次,至多n次;
                    \{0,n\}:匹配前面的字符至多n次;
                    \{m,\}:匹配前面的字符至少m次;

            位置锚定:
                ^:行首锚定;用于模式的最左侧;
                $:行尾锚定;用于模式的最右侧;
                ^PATTERN$: 用于模式匹配整行;
                    ^$: 空行;
                    ^[[:space:]]*$

                \< 或 \b:词首锚定;用于单词模式的左侧;
                \> 或 \b:词尾锚定;用于单词模式的右侧;
                \<PATTERN\>:匹配整个单词;

            分组:
                \(\):将一个或多个字符捆绑在一起,当作一个整体进行处理;
                    \(xy\)*ab

1
     [root@liu /] # grep -E -o "\<[0-9]{1,2}\>" /etc/passwd


10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;

1
2
3
4
5
6
7
8
     [root@liu /] # grep "^[[:space:]]\+" /boot/grub/grub.conf
             root (hd0,0)
   
           kernel  /vmlinuz-2 .6.32-431.el6.i686 ro 
root=UUID=3fcabd0f-e9fb-4618-918d-c9e004fe6a59 rd_NO_LUKS  
KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 
rd_NO_LVM rd_NO_DM rhgb quiet
             initrd  /initramfs-2 .6.32-431.el6.i686.img


11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

1
2
3
4
5
6
7
8
9
10
11
12
13
     [root@liu /] # grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.sysinit
     # /etc/rc.d/rc.sysinit - run once at boot time
     # Taken in part from Miquel van Smoorenburg's bcheckrc.
     # Check SELinux status
     # Print a text banner.
     # Only read this once.
     # Initialize hardware
     # Set default affinity
     # Load other user-defined modules
     # Load modules (for backward compatibility with VARs)
     # Configure kernel parameters
     # Set the hostname.
     ...

12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

1
2
3
4
5
6
7
8
9
10
11
12
13
     [root@liu /] # netstat -tan | grep "LISTEN[[:space:]]\+$"
     tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
     tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
     tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN
     tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
     tcp        0      0 0.0.0.0:33661               0.0.0.0:*                   LISTEN
     tcp        0      0 :::36996                    :::*                        LISTEN
     tcp        0      0 :::111                      :::*                        LISTEN
     tcp        0      0 :::22                       :::*                        LISTEN
     tcp        0      0 ::1:631                     :::*                        LISTEN
     tcp        0      0 ::1:25                      :::*                        LISTEN
     或者使用 egrep 命令
     netstat  -tan | egrep  "LISTEN[[:space:]]+$"

13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

1
2
3
4
5
6
7
8
9
10
     [root@liu /] # useradd bash
     [root@liu /] # useradd testbash
     [root@liu /] # useradd basher
     [root@liu /] # useradd nologin -s /sbin/nologin
     [root@liu /] # grep "^\(\<[[:alpha:]]\+\>\).*\1$" /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
     bash :x:501:501:: /home/bash : /bin/bash
     nologin:x:504:504:: /home/nologin : /sbin/nologin


本文转自 ljohnmail 51CTO博客,原文链接:http://blog.51cto.com/ljohn/1843661,如需转载请自行联系原作者
相关文章
|
1月前
|
编译器 Python
Python正则表达式的7个使用典范(推荐)
Python正则表达式的7个使用典范(推荐)
24 0
|
1月前
|
Python
Python实现正则表达式匹配。
【2月更文挑战第11天】【2月更文挑战第30篇】Python实现正则表达式匹配。
|
2月前
|
Linux
认识Linux指令之 “find grep” 命令
认识Linux指令之 “find grep” 命令
认识Linux指令之 “find grep” 命令
|
1月前
|
存储 监控 Linux
性能工具之linux三剑客awk、grep、sed详解
Linux 三剑客 awk,sed和grep 在性能领域广泛用于性能建模、性能监控及性能分析等方面,也是各大互联网公司测试岗高频面试题,中高端测试人员必备技能之一。
52 1
性能工具之linux三剑客awk、grep、sed详解
|
13天前
|
Linux
Linux 指令|date|cal|find|grep|热键
Linux 指令|date|cal|find|grep|热键
|
1月前
|
算法 Shell Linux
【Shell 命令集合 文档编辑】Linux 文本搜索工具 grep命令使用指南
【Shell 命令集合 文档编辑】Linux 文本搜索工具 grep命令使用指南
30 4
|
1月前
|
Python
请解释Python中的正则表达式以及如何使用它们进行文本处理。
请解释Python中的正则表达式以及如何使用它们进行文本处理。
9 0
|
1月前
|
机器学习/深度学习 Python
请解释Python中的正则表达式是什么?并举例说明其用法。
【2月更文挑战第26天】【2月更文挑战第86篇】请解释Python中的正则表达式是什么?并举例说明其用法。
|
1月前
|
缓存 数据安全/隐私保护 Python
Python快速入门:类、文件操作、正则表达式
Python快速入门:类、文件操作、正则表达式
C4.
|
1月前
|
Python
Python正则表达式
Python正则表达式
C4.
14 1