grep,find用法-2

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

grep -E “^(root|fedora|user1)>” /etc/passwd | cut -d: -f1,7
[root@bogon Desktop]# grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7
root:/bin/bash
fedora:/bin/bash
user1:/bin/bash

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

grep “[[:alpha:]]+()” /etc/rc.d/init.d/functions
[root@bogon Desktop]# grep "[[:alpha:]]+()" /etc/rc.d/init.d/functions
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取出其基名;扩展:取出其路径名;

echo /etc/passwd | grep -o “[^/]+/\?$” passwd
[root@bogon ~]# echo /etc/passwd | grep -o "[^/]\+/\?$"
passwd
[root@bogon ~]#
echo /etc/passwd | grep -oP ‘/.*/(?=.?\b)’
[root@bogon ~]# echo /etc/passwd | grep -oP '/.*/(?=.?\b)'
/etc/
[root@bogon ~]#

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

ifconfig | grep -E ‘\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))>’
[root@bogon ~]# ifconfig | grep -E '\<([0-9]|([0-9][0-9])|(1[0-9][0-9])|(2[1-4][0-9])|(25[1-5]))\>'
        inet 192.168.101.15  netmask 255.255.255.0  broadcast 192.168.101.255
        inet6 fe80::20c:29ff:fe00:b610  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:00:b6:10  txqueuelen 1000  (Ethernet)
        RX packets 90557  bytes 114145934 (108.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32344  bytes 15212308 (14.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 4  bytes 340 (340.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 340 (340.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 00:00:00:00:00:00  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@bogon ~]#

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

ifconfig | grep -o -E “(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[0-9][0-9]|1[0-9])”
[root@bogon ~]# ifconfig | grep -o -E "(([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9][0-9]|1[0-9])"
192.168.101.15
255.255.255.0
192.168.101.25
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.25
[root@bogon ~]#

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

grep -E “([[:alph^C]]|[0-9]|[[:punct:]]*|.|\@)”

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

find /var/ -user root -group mail -ls
[root@bogon ~]# find /var/ -user root -group mail -ls
100664302    4 drwxrwxr-x   2 root     mail         4096 Aug  2 22:57 /var/spool/mail
[root@bogon ~]#

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

find / -nouser -nogroup -ls
[root@bogon ~]# find / -nouser -nogroup -ls
find: ‘/proc/11927/task/11927/fd/6’: No such file or directory
find: ‘/proc/11927/task/11927/fdinfo/6’: No such file or directory
find: ‘/proc/11927/fd/6’: No such file or directory
find: ‘/proc/11927/fdinfo/6’: No such file or directory
find: ‘/run/user/1000/gvfs’: Permission denied
   137    0 drwxr-xr-x   2 2006     2006            6 Aug  3 20:44 /root/abc
35461830    0 drwxr-xr-x   2 2006     2006            6 Aug  3 20:44 /root/asdf
68898945    0 drwx------   3 2006     2006           74 Aug  3 20:44 /home/qqq
102702695    0 drwxr-xr-x   4 2006     2006           37 Jul 11 07:27 /home/qqq/.mozilla
1622887    0 drwxr-xr-x   2 2006     2006            6 Jun  9  2014 /home/qqq/.mozilla/extensions
35461829    0 drwxr-xr-x   2 2006     2006            6 Jun  9  2014 /home/qqq/.mozilla/plugins
68898946    4 -rw-r--r--   1 2006     2006           18 Nov 20  2015 /home/qqq/.bash_logout
68898949    4 -rw-r--r--   1 2006     2006          193 Nov 20  2015 /home/qqq/.bash_profile
68898950    4 -rw-r--r--   1 2006     2006          231 Nov 20  2015 /home/qqq/.bashrc
[root@bogon ~]#

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

find /etc -prem -222 -ls
[root@bogon ~]# find /etc/ -perm -222 -ls
33790768    0 lrwxrwxrwx   1 root     root           10 Jul 11 07:27 /etc/rc6.d -> rc.d/rc6.d
100801421    0 lrwxrwxrwx   1 root     root           16 Jul 11 07:27 /etc/ssl/certs -> ../pki/tls/certs
36511098    0 lrwxrwxrwx   1 root     root           38 Jul 11 07:36 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
33902848    0 lrwxrwxrwx   1 root     root           56 Jul 11 07:27 /etc/favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
34570567    0 lrwxrwxrwx   1 root     root           25 Jul 11 07:32 /etc/pam.d/smtp -> /etc/alternatives/mta-pam
33904308    0 lrwxrwxrwx   1 root     root           14 Jul 11 07:36 /etc/pam.d/system-auth -> system-auth-ac
33904306    0 lrwxrwxrwx   1 root     root           12 Jul 11 07:36 /etc/pam.d/postlogin -> postlogin-ac
33904305    0 lrwxrwxrwx   1 root     root           16 Jul 11 07:36 /etc/pam.d/password-auth -> password-auth-ac
33904303    0 lrwxrwxrwx   1 root     root           19 Jul 11 07:36 /etc/pam.d/fingerprint-auth -> fingerprint-auth-ac
33904307    0 lrwxrwxrwx   1 root     root           17 Jul 11 07:36 /etc/pam.d/smartcard-auth -> smartcard-auth-ac
1660802    0 lrwxrwxrwx   1 root     root           14 Jul 11 07:29 /etc/libvirt/qemu/networks/autostart/default.xml -> ../default.xml
34181389    0 lrwxrwxrwx   1 root     root           19 Aug  1 21:15 /etc/httpd/logs -> ../../var/log/httpd
34181390    0 lrwxrwxrwx   1 root     root           29 Aug  1 21:15 /etc/httpd/modules -> ../../usr/lib64/httpd/modules
34181391    0 lrwxrwxrwx   1 root     root           10 Aug  1 21:15 /etc/httpd/run -> /run/httpd
101468179    0 lrwxrwxrwx   1 root     root           35 Jul 11 07:32 /etc/cifs-utils/idmap-plugin -> /etc/alternatives/cifs-idmap-plugin
71753217    0 lrwxrwxrwx   1 root     root           21 Jul 11 07:31 /etc/gdm/Xsession -> ../X11/xinit/Xsession
33556151    0 lrwxrwxrwx   1 root     root           22 Jul 11 07:32 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
36134996    0 lrwxrwxrwx   1 root     root            8 Jul 11 07:32 /etc/scl/conf -> prefixes

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

find /etc -size +1M -ls
[root@bogon ~]# find /etc/ -size +1M -ls
72502453 3772 -rw-r--r--   1 root     root      3858924 Nov 20  2015 /etc/selinux/targeted/policy/policy.29
36328392 6852 -r--r--r--   1 root     root      7014922 Jul 11 07:37 /etc/udev/hwdb.bin
101554968 1336 -rw-r--r--   1 root     root      1367395 Mar  5  2015 /etc/brltty/zh-tw.ctb
[root@bogon ~]#

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

(0)
N27_DanryN27_Danry
上一篇 2017-08-04
下一篇 2017-08-04

相关推荐

  • 磁盘管理

    磁盘管理 本文将按顺序以实例演示磁盘管理的所有操作,让我们开始吧! 一,磁盘的添加 ① 先来查看linux系统总共有几个磁盘,由图可知是两个,分别是sda,sdb。我们再加一个,按照磁盘命名顺序,应是sdc,他们都在/dev目录下。 补充: 1,磁盘命名规则: 不同磁盘,按照a-z依次标识,如sda,sdb,sdc 同一磁盘的不同分区,按照1,2,&#823…

    2017-08-19
  • 马哥教育网络班21期+第1周课程练习

    (一)计算机的基础认识       1. 冯诺依曼体系:          运算器(ALU Arithmetic Logical Unit):完成算数和逻辑运算。          控制器(CU Control Unit):发出各…

    Linux干货 2016-07-07
  • LVS常见的类型实现方式

    前言  由于lvs的基础知识已经在第一篇lvs中讲解了,所以在这里只做实验,包括lvs-nat,lvs-dr,以及基于Freiwall标记和实现会话绑定实验。 一、lvs-nat:也是MASQERADING,简称为m(masquerading)    实验图:     地址规划:     …

    Linux干货 2015-06-29
  • 大概认识linux

             linux简介 其实对Linux系统不是很了解,都是在百度和谷歌搜索出来,才发现Linux很多版本。各版本各优缺点。首先简单说下发展史,Linux在1991年10月5号(这是第一次正式向外公布的时间)在芬兰诞生,以后借助于Internet网络向全世界各地传播,由计算机爱好者的再次开发新功能和…

    Linux干货 2016-05-29
  • LInux用户和组管理详解

    一、用户和组的概念 Linux系统是多任务、多用户的分时操作系统。用户要使用系统资源,需要向管理员验证身份,由管理员进行认证和授权,并对用户的行为进行追踪和审计,以保障系统资源的合理分配以及系统的安全。 要达到账号管理的目的,需要对用户和用户组进行管理。下面分别来介绍用户管理和用户组管理。 每个用户都有唯一的一个编号来标识,这个标识号叫Uid,每个用户至少属…

    Linux干货 2016-08-02
  • ☞RAID使用手册

    RAID使用手册 RAID演示

    Linux干货 2016-09-02