grep及正则表达式相关练习
1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。
-
~]# cp -r /etc/skel/ /home/tuser1
-
~]# chmod -R 700 /home/tuser1
-
~]# ls -ld /home/tuser1
-
drwx——. 3 root root 17 10月 9 16:20 /home/tuser1/
2、编辑/etc/group文件,添加组hadoop。
-
~]# echo "hadoop:x:3000" >> /etc/group
-
~]# tail -1 /etc/group
-
hadoop:x:3000
3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。
-
~]# echo "hadoop:x:3000:3000:hadoop:/home/hadoop:/bin/bash" >> /etc/passwd
-
~]# tail -1 /etc/passwd
-
hadoop:x:3000:3000:hadoop:/home/hadoop:/bin/bash
4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。
-
~]# cp -r /etc/skel /home/hadoop
-
~]# chmod -d 700 /home/hadoop
-
~]# ls -l /home
-
总用量 4
-
drwx——. 3 root root 74 10月 10 09:46 hadoop
-
drwx——. 14 roo roo 4096 10月 9 09:25 roo
-
drwx——. 3 root root 17 10月 9 16:20 tuser1
5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。
-
~]# chown -R hadoop:hadoop /home/hadoop
-
~]# ls -l /home
-
总用量 4
-
drwx——. 3 hadoop hadoop 74 10月 10 09:46 hadoop
-
drwx——. 14 roo roo 4096 10月 9 09:25 roo
-
drwx——. 3 root root 17 10月 9 16:20 tuser1
6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;
(1)
-
~]# grep ^[S,s] /proc/meminfo
-
SwapCached: 0 kB
-
SwapTotal: 2097148 kB
-
SwapFree: 2097148 kB
-
Shmem: 10424 kB
-
Slab: 291280 kB
-
SReclaimable: 202508 kB
-
SUnreclaim: 88772 kB
(2)
-
[root@localhost ~]# cat /proc/meminfo | grep ^[S,s]
-
SwapCached: 0 kB
-
SwapTotal: 2097148 kB
-
SwapFree: 2097148 kB
-
Shmem: 10424 kB
-
Slab: 291236 kB
-
SReclaimable: 202516 kB
-
SUnreclaim: 88720 kB
7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;
-
~]# egrep -v /sbin/nologin /etc/passwd
-
root:x:0:0:root:/root:/bin/bash
-
sync:x:5:0:sync:/sbin:/bin/sync
-
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
-
halt:x:7:0:halt:/sbin:/sbin/halt
-
roo:x:1000:1000:root:/home/roo:/bin/bash
-
hadoop:x:3000:3000:hadoop:/home/hadoop:/bin/bash
-
~]# egrep -v /sbin/nologin /etc/passwd | cut -d: -f1
-
root
-
sync
-
shutdown
-
halt
-
roo
-
hadoop
8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;
-
~]# grep /bin/bash /etc/passwd | cut -d: -f1
-
root
-
roo
-
hadoop
9、找出/etc/passwd文件中的一位数或两位数;
-
~]# egrep -o "\<[[:digit:]]{1,2}>" /etc/passwd
10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行;(注:CentOS6)
-
~]# egrep "^[[:space:]]+" /boot/grub/grub.conf
-
root (hd0,0)
-
kernel /vmlinuz-2.6.32-573.el6.x8664 ro root=/dev/mapper/vgcentos6-lvroot rdNOLUKS * * rdNOMD rdLVMLV=vgcentos6/lvswap crashkernel=128MCN.UTF-8
-
rdLVMLV=vgcentos6/lvroot KEYBOARDTYPE=pc KEYTABLE=us rdNODM rhgb quiet
-
initrd /initramfs-2.6.32-573.el6.x86_64.img
11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;
-
~]# egrep "^#[[: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.
-
…
12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;
-
~]# netstat -tan | egrep "LISTEN[[:space:]]+$"
-
tcp 0 0 192.168.122.1:53 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
-
tcp6 0 0 :::22 :::* LISTEN
-
tcp6 0 0 ::1:631 :::* LISTEN
-
tcp6 0 0 ::1:25 :::* LISTEN
13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息
-
~]# egrep "^(\<[a-z]>).\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:3001:3001::/home/bash:/bin/bash
-
nologin:x:3004:3004::/home/nologin:/sbin/nologin
原创文章,作者:shiyekun,如若转载,请注明出处:http://www.178linux.com/50583