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基础知识——文件查找

    Linux系统的核心思想之一就是一切皆文件,可想而知你要靠记忆去查找一个文件该是多么费劲;今天我们介绍几个文件查找命令:whereis,locate,find whereis locate the binary, source, and manual page files for…

    Linux干货 2016-10-11
  • Linux之启动和内核管理

     Linux之启动和内核管理     本文主要包括以下内容一  CentOS 5和6的启动流程二  服务管理三  Grub管理四  自制Linux五  启动排错六  编译安装内核   Linux组成Linux: kernel+rootfskernel: 进程管…

    Linux干货 2016-09-15
  • 第八周-Linux网络配置,软件安装,bash编程

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别 网桥:一种网络设备,负责网络桥接(network bridging)之用。桥接器将网络的多个网段在数据链路层(OSI模型第2层)连接起来(即桥接)。 集线器(Hub):是指将多条以太网双绞线或光纤集合连接在同一段物理介质下的设备。集线器是运作在OSI模型中的物理层。 二层交换机:工…

    Linux干货 2016-11-14
  • 硬盘分区的三种方式

    以前在初步接触linux的时候,只知道一种分区方式,现在学会了三种分区方式,所以下面就给大家简单的演示一下 ,以供参考。 第一种:fdisk /dev/sd?    具体步骤请看下图:首先有一个空的硬盘/dev/sdb, 第一步:输入命令fdisk /dev/sdb,会弹出一个帮助信息Command(m for help),意思就是m键是…

    2017-08-20
  • Find工具实例

      1、显示当前系统上root、fadora或user1用户的默认shell。         [root@localhost ~]# grep -E "^(root|fadora|user1)\>"&n…

    Linux干货 2016-11-27
  • 马哥教育-第一周作业

    第一部分:计算机组成及功能 电子真空管的出现促使了计算机的诞生; 根据冯●诺依曼的哲学思想,计算机的组成分为五大部分。分别为: 1.     运算器 2.     控制器 3.     存储器 4.   &nb…

    Linux干货 2016-12-04