马哥教育网络班22期+第五周课程练习

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

[xw@localhost ~]$ cat /etc/passwd | grep -E "^(root|fedora|user1)\>" | cut -d: -f1,7
root:/bin/bash
fedora:/bin/bash
user1:/bin/zsh

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

[root@localhost ~]# cat /etc/rc.d/init.d/functions | grep -E -o "[^[:space:]]+\(\)"
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取出其基名;
    扩展:取出其路径名;

[root@localhost ~]# echo "/var/log/cron" | grep -E -o "[^/]+/?$"
cron

[root@localhost ~]# echo "/var/log/cron" | grep -E -o "^(/[^/]+)+/"
/var/log/

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

[root@localhost ~]# ifconfig | grep -E -o "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
192
168
15
255
255
255
192
168
255
64
29
1
6
9
29
73
127
1
255
1
128
4
4
192
168
122
1
255
255
255
192
168
122
255

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

[root@localhost ~]# ifconfig | grep -E -o "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|
2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
192.168.0.15
255.255.255.0
192.168.0.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255

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

[xw@localhost ~]$ grep -E -o "[^[:space:]]+@[^[:space:]]+(com|cn|net)" mail.txt 
ewrwer@qq.com
2335466@163.com
3434@sina.com
bjbjk454@yahoo.com.cn
jsj-df2@hotmail.com
kdiwse234@263.net

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

[root@localhost ~]# find /var/ -user root -a -group mail -ls
134321240    4 drwxrwxr-x   2 root     mail         4096 Sep 16 15:51 /var/spool/mail
134905165  104 -rw-------   1 root     mail       104325 Sep 16 15:51 /var/spool/mail/root

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

[root@localhost ~]# find / \( -nouser -o -nogroup \) -type f

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

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

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

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

[root@localhost ~]# find /etc/ -size +1M -type f -exec ls -lh {} \;
-r--r--r--. 1 root root 6.7M Aug  8 16:02 /etc/udev/hwdb.bin
-rw-------. 1 root root 1.3M Aug 10 23:36 /etc/selinux/targeted/contexts/files/file_contexts.bin
-rw-r--r--. 1 root root 3.6M Aug 10 23:36 /etc/selinux/targeted/policy/policy.29
-rw-r--r--. 1 root root 1.4M Mar  6  2015 /etc/brltty/zh-tw.ctb

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

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

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

[root@localhost ~]# find /usr/ -not \( -user root -o -user bin -o -user hadoop \)  -type f

[root@localhost ~]# find /usr \( -not -user root -a -not -user bin -a -not -user hadoop \) -type f

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

[root@localhost ~]# find /etc/ -not -perm -222 -type f

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

[root@localhost ~]# find /etc/ -mtime -7 -a -not \( -user root -o -user hadoop \) -type f

[root@localhost ~]# find /etc/ -mtime -7 -a -not -user root -a -not -user hadoop -type f

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

(0)
萝卜萝卜
上一篇 2016-10-09
下一篇 2016-10-09

相关推荐

  • 2016/10/19作业:软链接与硬链接的区别

    软链接和硬链接的区别 linux中链接分为两种,一种是硬链接(Hard Link),一种是符号链接(Symbolic Link),其中,符号链接又称为软链接。 硬链接: 在Linux文件系统中 ,磁盘中的所有文件都有一个编号,这个编号称为索引节点(Inode)。在Linux中,多个同一索引节点可以有多个文件名,这就是硬链接。硬链…

    Linux干货 2016-10-20
  • vim编辑器

      Linux文本编辑器vim     Linux下的编辑器最常用的就是vim或者vi文本编辑。vi和vim编辑器的区别是vim是vi的改进版本,在vi编辑器的基础上上扩展了很多实用的功能。 vim的使用 用vim打开文件:     vim [options] [file ..]…

    Linux干货 2016-12-04
  • linux文本处理三剑客—grep

      cat:concatenate 文本文件查看工具 cat [option] filename… -n:给显示出来的文本行加上编号 -b:非空行编号 -V:显示 ^ -E:显示行结束符$ -T:显示制表符 -A:显示所有控制符-A=-VET -s:压缩连续…

    系统运维 2016-08-05
  • 迁移home到独立分区

    1,fdisk  /dev/sda  创建10G的分区 Lsblk 查看下新创建分区是否同步 partx  -a  /dev/sda 同步 2,mkfs.ext4  /dev/sda6  -L  /home 创建文件系统跟卷标 3,mkdir  /mnt/home  创建一个挂载点 mount  /dev/sda6  /mnt/home 挂载 4,cp  -…

    2017-12-14
  • 常用命令总结

    ifconfig查看IP地址 date查看日期和时间 cd跳转到任何目录 useradd新建一个普通用户 passwd给用户设置密码 poweroff   halt    关机 reboot  重启 cal  查看日历 cal -y 查看一年日历 env  export&nbsp…

    Linux干货 2017-04-04
  • Linux 文 本 处 理 工 具

    Linux 文 本 处 理 工 具 一.学习大纲: ◎各种文本工具来查看、分析、统计文本文件 文件内容查看工具:cat, tac,rev,more,less 文件截取:head和tail 按列抽取:cut,paste 分析文本的工具:wc , sort , uniq,diff和patch 命令使用练习题 ◎文本过滤与处理工具: grep与正则表达式…

    Linux干货 2016-08-05

评论列表(1条)

  • luoweiro
    luoweiro 2016-10-11 23:37

    对于find命令其实还有一些高级用法,比如时间轴相关的,这部分其实在日常工作中用到的也很多,可以再总结一番。