04linux葵花宝典之grep练习

grep

04linux葵花宝典之grep练习

本节博客grep命令的实际操作练习

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

cp -rv /etc/skel /home/tuser1
chmod -R go-rwx /home/tuser1

注释:1.cp 命令复制时,使用-r选项,表示递归复制,如果目标不存在,则创建 2.chmod命令修改目录及目录中的文件权限时,使用-R选项,进行递归修改

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

/etc/group文件中的格式为, 组名:组密码(x为占位符):GID:一此组为附加组的用户

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

vi /etc/passwd

敲G键,跳转至最后一行,敲$键,跳转至行未,然后另起一行,开始编辑

hadoop:x:2003:5110::/home/hadoop:/bin/bash

ESC键退出插入模式,shift+z+z 保存退出

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

cp -rv /etc/skel /home/tuser1
chmod  go-rwx /home/tuser1

此题要求只修改hadoop目录,并没有说明目录中的文件,故未使用-R递归选项

5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。

[root@localhost hdoooop]# ls -la
total 16
drwx------.  2 hadoop hadoop   59 Mar 29 17:05 .
drwxr-xr-x. 12 root   root   4096 Mar 29 17:05 ..
-rw-r--r--.  1 root   root     18 Mar 29 17:05 .bash_logout
-rw-r--r--.  1 root   root    193 Mar 29 17:05 .bash_profile
-rw-r--r--.  1 root   root    231 Mar 29 17:05 .bashrc
[root@localhost hdoooop]# chown -R hadoop:hadoop /home/hdoooop
[root@localhost hdoooop]# ls -la
total 16
drwx------.  2 hadoop hadoop   59 Mar 29 17:05 .
drwxr-xr-x. 12 root   root   4096 Mar 29 17:05 ..
-rw-r--r--.  1 hadoop hadoop   18 Mar 29 17:05 .bash_logout
-rw-r--r--.  1 hadoop hadoop  193 Mar 29 17:05 .bash_profile
-rw-r--r--.  1 hadoop hadoop  231 Mar 29 17:05 .bashrc

使用-R选项,递归修改

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

 grep -i '^s' /proc/meminfo

使用-i选项,忽略大小写,使用^,锚定行首

grep '^[sS]' /proc/meminfo

行首必须是[]个字符集中指定的字符,即s或S

7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

grep -v '.*\(/sbin/nologin\)\>' /etc/passwd | cut -d: -f1
grep -v '/sbin/nologin$' /etc/passwd | cut -d: -f1

-v –invert-match 选择出未被模式匹配到的行;$ 锚定行尾,>锚定词尾,使用(,),将/sbin/nologin试为一个词

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

grep '/bin/bash$' /etc/passwd | cut -d: -f1
grep '\(/bin/bash\)\b' /etc/passwd | cut -d: -f1

9、找出/etc/passwd文件中的一位数或两位数;

 grep -o '\b[0-9]\{1,2\}\b' /etc/passwd

-o 只显示匹配到的字符串,使用锚定词首词尾\b,或者\<>,来指定词

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

grep '^[[:space:]]' /boot/grub/grub.conf

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

grep '^#[[:space:]]\+[^[:space:]]' /etc/rc.d/rc.sysinit
grep -E '^#[[:space:]]+[^[:space:]]' /etc/rc.d/rc.sysinit
grep '^#[[:sapce:]]\{1,\}[^[:sapce:]]' /etc/rc.d/rc.sysinit

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

netstat -tan | grep 'LISTEN[[:space:]]*$'

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

useradd

useradd bash
useradd testbash
useradd basher
useradd -s /sbin/nologin nologin


grep -E -o '^([^:]+\b).*\1$' /etc/passwd

第一,需要使用分组来引用前面已经匹配到的用户名,第二,需要使用\b 或者>来锚定用户的词尾, 如果不锚定词尾grep -E -o ‘^([^:]+).*\1$’ /etc/passwd,则匹配到了basher:x:2007:2007::/home/basher:/bin/bash

[root@localhost home]# grep -E -o '^([^:]+).*\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
nobody:x:99:99:Nobody:/:/sbin/nologin
hadoop:x:1003:1003::/home/hadoop:/bin/bash
hdoop:x:2003:5110::/home/hdoop:/bin/sh
bash:x:2005:2005::/home/bash:/bin/bash
basher:x:2007:2007::/home/basher:/bin/bash
nologin:x:2008:2008::/home/nologin:/sbin/nologin
[root@localhost home]# grep -E -o '^([^:]+\b).*\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:2005:2005::/home/bash:/bin/bash
nologin:x:2008:2008::/home/nologin:/sbin/nologin

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/93736

(0)
上一篇 2018-03-29 21:12
下一篇 2018-03-30 10:48

相关推荐

评论列表(1条)

  • 老白
    老白 2018-03-30 08:30

    恭迎教主