grep 正则表达式 find

1.显示当前系统上root,fedora 或user1用户的默认shell
[root@centos7 ~]# grep -E  “^(root|fedora|user1)” /etc/passwd
root:x:0:0:root:/root:/bin/bash
fedora:x:2003:2003::/home/fedora:/bin/bash
user1:x:2004:2004::/home/user1:/bin/bash
2.找出 /etc/rc.d/init.d/functions文件中某个单词后面跟一组小括号的行,形如:hello();
[root@centos7 ~]# cat /etc/rc.d/init.d/functions |grep -E “[[:alpha:]]{1,}\(\) “
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
3.使用echo命令输出一个绝对路径,使用grep取出莫个基名。
[root@centos7 fedora]# echo /home/fedora/|grep -E “[[:alpha:]]{1,}/$”
/home/fedora/
4.找出ifconfig命令结果中的1-255之间的数字。
[root@centos7 ~]# ifconfig|grep -E -o  “\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>”
5挑战题:写一个模式,能匹配合理的ip地址;
[root@centos7 ~]# ifconfig|grep -E -o  “\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[1-5])\>”
192.168.119.137
255.255.255.0
192.168.119.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
6.挑战题:写一个模式,能匹配出所有的邮件地址;
[root@centos7 ~]# cat email |grep -E “^([0-9]|[a-zA-Z])([[:alnum:]]|[[:punct:]])+@[[:alnum:]]*\.(com|cn)$”
7.查找/var目录下属主为root,且属组为mail的所有文件或目录;
[root@centos7 var]# find /var/ -user root -a -group mail -ls
33701767    0 drwxrwxr-x   2 root     mail          105 Sep 17 07:19 /var/spool/mail
8.查找当前系统上没有属主或属组的文件;
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的目录或文件;
[root@centos7 var]# find / \( -nouser -o -nogroup \) -atime +3 -ls
find: ‘/proc/7176/task/7176/fd/6’: No such file or directory
find: ‘/proc/7176/task/7176/fdinfo/6’: No such file or directory
find: ‘/proc/7176/fd/6’: No such file or directory
find: ‘/proc/7176/fdinfo/6’: No such file or directory
34754918    0 -rw-rw—-   1 1005     mail            0 Sep  2 11:28 /var/spool/mail/mandriva
18360951    4 -rw-r–r–   1 1005     distro         18 Aug  2  2016 /home/mandriva/.bash_logout
18360952    4 -rw-r–r–   1 1005     distro        193 Aug  2  2016 /home/mandriva/.bash_profile
18360953    4 -rw-r–r–   1 1005     distro        231 Aug  2  2016 /home/mandriva/.bashrc
34754923    4 -rw——-   1 1005     distro         11 Sep  2 11:30 /home/mandriva/.cache/abrt/lastnotification
18360956    4 -rw——-   1 1005     distro         24 Sep  2 12:13 /home/mandriva/.bash_history
9.查找/etc目录下所有用户都有写权限的文件;
[root@centos7 etc]# find /etc/ -perm /222 -type f -ls
10. 查找/etc目录下大于1M,且类型为普通文件的所有文件;
find /etc/ -size 1M  -type f -ls
 

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

(0)
469008940469008940
上一篇 2017-09-18 16:09
下一篇 2017-09-18 19:58

相关推荐

  • 学习总结

    课上练习整理

    Linux干货 2017-11-19
  • Nginx的安装及其一些配置

    nginx的编译安装    tar xf nginx-1.12.2.tar.gz   cd nginx-1.12.2     yum install pcre-devel  ./configure –help   groupadd -r nginx     useradd -g nginx -r nginx    id nginx     ./c…

    Linux干货 2017-10-25
  • vim文本编辑器详解

    启动vim                             &…

    Linux干货 2016-08-15
  • Ansible的常用模块

    command模块: 目的:在指定节点上运行hostname命令 命令:ansible 192.168.1.16 -m command  ‘hostname’copy模块:目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 192.168.1.16 -m copy -a ‘src=/roo…

    Linux干货 2017-10-31
  • MySQL流程函数

    MySQL流程函数 IF(value,x y) 如果value是真,返回x,否则返回y MariaDB [learn]> INSERT INTO salary(sal) VALUES (1000),(2000),(3000),(4000),(5000),(6000),(NULL); Query OK, 7 rows affected (0.06 sec…

    Linux干货 2017-05-02
  • linux 下的文件压缩与解压

    文件压缩 压缩原理:把文件的二进制代码压缩,把相邻的0,1代码减少,比如有000000,可以把它变成6个0 的写法60,来减少该文件的空间。 目的:时间换空间,cpu时间–>空间 压缩文件工具:    compress|uncompress 压缩后的文件.z    gzip|gunzip  …

    Linux干货 2016-08-21