首页 > 代码库 > 第五周作业

第五周作业

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

[root@localhost ~]# grep -E "^(root|fedora|user1)" /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# grep -E "^(root|fedora|user1)" /etc/passwd | cut -d: -f7

/bin/bash


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

[root@localhost ~]# grep -E -o "[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions

fstab_decode_str()

checkpid()

__readlink()

__fgrep()

__kill_pids_term_kill_checkpids()

__kill_pids_term_kill()

__umount_loop()

__source_netdevs_fstab()

__source_netdevs_mtab()

__umount_loopback_loop()

__find_mounts()

__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()

action_silent()

strstr()


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

[root@localhost ~]# echo /etc/passwd |grep -E -o "[^/]+/?$" 

passwd


    扩展:取出其路径名

[root@localhost ~]# echo /a/b/c/e/f | grep -o "^/.*[^/]\+" | awk -F ‘/[^/]*$‘ ‘{print $1}‘

/a/b/c/e

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

[root@localhost ~]# ifconfig|egrep -o "\<[1-9]|[1-9][0-9]|1[0-9][0-9]|2[1-5][1-5]\>" 

29

5

25

19

16

1

20

19

16

1

255

255

255

255

80

20

29

25

64

15

1


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

[root@localhost ~]# ifconfig |egrep -o "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[1-5][1-5])(\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[1-5][1-5])){3}"

192.168.1.20

192.168.1.255


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

[root@localhost ~]# cat email.txt 

ufigh@sohu.com

fuifig@yule.tencent.com



55996599@qq.cn


  

[root@localhost ~]# egrep "[^[:space:]]+@[^[:space:]]+(.com|.cn)" email.txt 

ufigh@sohu.com

fuifig@yule.tencent.com

55996599@qq.cn

[root@localhost ~]# 

                    

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

[root@localhost ~]# find /var \( -user root -a -group mail \) -ls

2622637    4 drwxrwxr-x   2 root     mail         4096 10月 17 16:31 /var/spool/mail

2623828   12 -rw-------   1 root     mail        11424 10月 17 16:31 /var/spool/mail/root

2622816    4 drwxr-xr-x   6 root     mail         4096 8月 25 02:53 /var/www



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

[root@localhost ~]# find /   \( -nouser -a  -nogroup \)  -ls

find: “/proc/13840/task/13840/fd/5”: 没有那个文件或目录

find: “/proc/13840/task/13840/fdinfo/5”: 没有那个文件或目录

find: “/proc/13840/fd/5”: 没有那个文件或目录

find: “/proc/13840/fdinfo/5”: 没有那个文件或目录

[root@localhost ~]# 



     进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;

[root@localhost ~]# find /  \( \( -nouser -o  -nogroup \) -a -atime -3 \)  -ls

find: “/proc/13850/task/13850/fd/5”: 没有那个文件或目录

find: “/proc/13850/task/13850/fdinfo/5”: 没有那个文件或目录

find: “/proc/13850/fd/5”: 没有那个文件或目录

find: “/proc/13850/fdinfo/5”: 没有那个文件或目录

[root@localhost ~]# 


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

[root@localhost ~]# find /etc/  -perm -222 -ls

399101    0 lrwxrwxrwx   1 root     root            9 8月 25 02:52 /etc/sysconfig/network-scripts/ifup-isdn -> ifup-ippp

399084    0 lrwxrwxrwx   1 root     root           20 8月 25 02:52 /etc/sysconfig/network-scripts/ifdown -> ../../../sbin/ifdown

399095    0 lrwxrwxrwx   1 root     root           18 8月 25 02:52 /etc/sysconfig/network-scripts/ifup -> ../../../sbin/ifup

399089    0 lrwxrwxrwx   1 root     root           11 8月 25 02:52 /etc/sysconfig/network-scripts/ifdown-isdn -> ifdown-ippp

401732    0 lrwxrwxrwx   1 root     root           17 8月 25 02:53 /etc/sysconfig/selinux -> ../selinux/config

399077    0 lrwxrwxrwx   1 root     root           13 8月 25 02:52 /etc/rc.local -> rc.d/rc.local

403099    0 lrwxrwxrwx   1 root     root           22 8月 25 03:00 /etc/grub.conf -> ../boot/grub/grub.conf


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

[root@localhost ~]# find /etc/ -type f -a -size +1M -ls 

801525 2144 -rw-r--r--   1 root     root      2194395 8月 25 02:57 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml

533625 8228 -rw-r--r--   1 root     root      8424092 9月 11 05:07 /etc/selinux/targeted/policy/policy.24

405505 8228 -rw-r--r--   1 root     root      8424092 9月 11 05:07 /etc/selinux/targeted/modules/active/policy.kern


11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件

[root@localhost ~]# find /etc/init.d/ -perm -113 -ls


12、查找/usr目录下不属于root、bin或hadoop的文件;

[root@localhost ~]# find /usr/  ! \( -user root -o -user bin -o -user hadoop \) -ls

139131   12 -rwsr-xr-x   1 abrt     abrt        10296 5月 12 04:43 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

[root@localhost ~]# 


13、查找/etc/目录下至少有一类用户没有写权限的文件;

[root@localhost ~]# find /etc   ! -perm -222  -ls 


14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;

[root@localhost ~]# find /etc/ -ctime -7 -a ! \( -user root -o -user hadoop \) -ls


本文出自 “年华似流水” 博客,谢绝转载!

第五周作业