美文网首页
文件权限以及正则表达式实例

文件权限以及正则表达式实例

作者: 50bed3e80e33 | 来源:发表于2018-09-09 16:49 被阅读0次

    一、权限

    权限分类:
    owner:属主,u 
    group:属组,g 
    other:其他,o
    3个bit为一个权限
    
    目录权限:
    r:列出目录文件列表
    x:cd、目录文件内容(基础权限)
    w:能否创建修改目录下的文件。能否删除某个文件是看父目录时候有w权限(需要x权限)
    
    文件权限:
    r 读取文件内容
    w 更改文件内容
    x 运行(文件默认不会有执行权限)
    
    umask:
    umask可以用于保留在创建文件权限
    666-umask=新建文件权限(如果新建权限文件为奇数则加一)
    777-umask=新建目录权限  
    

    二、文本处理工具grep

    grep(egrp、fgrep   fregp不支持正则表达式)(可以读取标准输入)
    功能:文本过滤(模式:pattern)工具,根据用户指定的“模式”对目标文件逐行进行匹配检查,打印匹配到的行
    模式:由正则表达式字符及文本字符所编写的过滤条件
    语法:grep [OPTIONS] PATTERN [FILE...]
              grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
    选项:alias grep='grep --color=auto' 定义grep别名
              grep -v 显示不被pattern匹配的字符
              grep  -i  忽略字符大小写
              grep -n 显示出匹配字符串的行号
              grep -c  统计匹配行的数量
              grep -q 静默模式,不输出任何信息
              grep -o 仅显示匹配到字符串,每匹配一次显示一行
              grep  -A#(#是数字的意思)   包含字符串的后面几行
              grep  -B#(#是数字的意思)   包含字符串的前面几行
              grep  -C#(#是数字的意思)   包含字符串的前后几行
              grep  -e 实现多个选项间的或关系
              grep  -w 匹配整个单词
              grep  -E 使用ERE
              grep  -F  相当于fgrep,不支持正则表达式
    

    三、正则表达式

    正则表达式(匹配字符串,不是字符名):
    REGEXP:由一类特殊字符及文本字符所编写的模式,其中有 些字符(元字符)不表示字符字面意义,而表示控制或通配的功能 
    程序支持:grep,sed,awk,vim, less,nginx,varnish等  
    分两类: 基本正则表达式:BRE 
                   扩展正则表达式:ERE grep -E, egrep 
    正则表达式引擎:
              采用不同算法,检查处理正则表达式的软件模块 
              PCRE(Perl Compatible Regular Expressions) 
    元字符分类:字符匹配、匹配次数、位置锚定、分组
    
    用途1、字符匹配:
    . 匹配任意单个字符
    [] 匹配指定范围内的任意单个字符
    [^] 匹配指定范围外的任意单个字符
    [:alnum:] 字母和数字
    [:alpha:] 代表任何英文大小写字符,亦即A-Z, a-z
    [:lower:] 小写字母[:upper:] 大写字母
    [:blank:] 空白字符(空格和制表符)
    [:space:]水平和垂直的空白字符(比[:blank:]包含的范围广)
    [:cntrl:] 不可打印的控制字符(退格、删除、警铃...)
    [:digit:] 十进制数字
    [:xdigit:]十六进制数字
    [:graph:] 可打印的非空白字符
    [:print:] 可打印字符
    [:punct:] 标点符号
    
    匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数
    * 匹配前面的字符任意次,包括0次 (贪婪模式:尽可能长的匹配)
    .* 任意长度的任意字符
    \? 匹配其前面的字符0或1次
    \+ 匹配其前面的字符至少1次
    \{n\} 匹配前面的字符n次
    \{m,n\} 匹配前面的字符至少m次,至多n次
    \{,n\} 匹配前面的字符至多n次
    \{n,\} 匹配前面的字符至少n次
    
    位置锚定:定位出现的位置
    ^ 行首锚定,用于模式的最左侧
    $ 行尾锚定,用于模式的最右侧
    ^PATTERN$ 用于模式匹配整行
    ^$ 空行
    ^[[:space:]]*$ 空白行
    \< 或\b词首锚定,用于单词模式的左侧
    \> 或\b词尾锚定;用于单词模式的右侧
    \<PATTERN\>匹配整个单词   =  grep -w
    
    分组:
    \(\) 将一个或多个字符捆绑在一起,当作一个整体进行处理,如:\(root\)\+
    分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,这些变量的命名方式为: \1, \2, \3, ...
    \1表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符(后向引用是引用的字符,而不是正则表达式)
    示例:\(string1\+\(string2\)*\)
    \1 :string1\+\(string2\)*
    \2 :string2
    后向引用:引用前面的分组括号中的模式所匹配字符,而非模式本身
    或者:\|
    示例:a\|b: a或b C\|cat: C或cat \(C\|c\)at:Cat或cat
    
    egrep 扩展的正则表达式  = grep -E
    语法:egrep [OPTIONS] PATTERN [FILE...]
    和正则表达式的区别在于 少了\。
    

    实例:

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。

    cp -a /etc/skel  /home/tuser1
    chmod -R 700 /home/tuser1
    ll -d /home/tuser1
    drwx------. 2 root root 4096 Jan  9  2018 /home/tuser1
    

    2、编辑/etc/group文件,添加组hadoop。

    vim /etc/group
    hadoop:x:1010
    cat /etc/group| grep "^hadoop"
    hadoop:x:1010
    

    3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。

    vim /etc/passwd
    hadoop:x:1010:1010::/home/hadoop:/bin/bash
    cat /etc/passwd| grep "^hadoop"
    hadoop:x:1010:1010::/home/hadoop:/bin/bash
    

    4、复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其它用户没有任何访问权限。

    cp -r /etc/skel /home/hadoop
    chmod -R 700 /home/hadoop
    ll -d /home/hadoop
    drwx------ 2 root root 4096 Sep  9 15:43 /home/hadoop
    

    5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop。

    chown -R hadoop:hadoop /home/hadoop 
    ll -a /home/hadoop 
    total 20
    drwx------   2 hadoop hadoop 4096 Sep  9 15:43 .
    drwxr-xr-x. 10 root   root   4096 Sep  9 15:43 ..
    -rwx------   1 hadoop hadoop   18 Sep  9 15:43 .bash_logout
    -rwx------   1 hadoop hadoop  193 Sep  9 15:43 .bash_profile
    -rwx------   1 hadoop hadoop  231 Sep  9 15:43 .bashrc
    

    6、显示/proc/meminfo文件中以大写或小写S开头的行;用两种方式;

    cat /proc/meminfo | grep "^s\|^S"
    cat /proc/meminfo | grep "^[sS]"
    SwapCached:            0 kB
    SwapTotal:             0 kB
    SwapFree:              0 kB
    Shmem:               712 kB
    Slab:              87464 kB
    SReclaimable:      75748 kB
    SUnreclaim:        11716 kB
    

    7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户;

    grep -v "sbin/nologin$" /etc/passwd 
    root:x:0:0:root:/root:/bin/bash
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    syslog:x:996:994::/home/syslog:/bin/false
    admin:x:1000:1000::/home/admin:/bin/bash
    xdm:x:1001:1001::/home/xdm:/bin/bash
    bash:x:1003:1006::/home/bash:/bin/bash
    testbash:x:1004:1007::/home/testbash:/bin/bash
    sh:x:1005:1008::/home/sh:/bin/bash
    hadoop:x:1010:1010::/home/hadoop:/bin/bash
    

    8、显示/etc/passwd文件中其默认shell为/bin/bash的用户;

    grep "/bin/bash$" /etc/passwd 
    root:x:0:0:root:/root:/bin/bash
    admin:x:1000:1000::/home/admin:/bin/bash
    xdm:x:1001:1001::/home/xdm:/bin/bash
    bash:x:1003:1006::/home/bash:/bin/bash
    testbash:x:1004:1007::/home/testbash:/bin/bash
    sh:x:1005:1008::/home/sh:/bin/bash
    hadoop:x:1010:1010::/home/hadoop:/bin/bash
    

    9、找出/etc/passwd文件中的一位数或两位数;

    grep -wo  "[0-9]\{1,2\}" /etc/passwd 
    

    10、显示/etc/grub2.cfg中以至少一个空白字符开头的行;

    grep  "^[[:blank:]]\+.*" /etc/grub2.cfg
    

    11、显示/etc/grub2.cfg文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行;

    grep "^#.*[[:blank:]]\+[^[:blank:]]\+" /etc/grub2.cfg
    # DO NOT EDIT THIS FILE
    # It is automatically generated by grub2-mkconfig using templates
    # from /etc/grub.d and settings from /etc/default/grub
    ### BEGIN /etc/grub.d/00_header ###
    # Fallback normal timeout code in case the timeout_style feature is
    # unavailable.
    ### END /etc/grub.d/00_header ###
    ### BEGIN /etc/grub.d/00_tuned ###
    ### END /etc/grub.d/00_tuned ###
    ### BEGIN /etc/grub.d/01_users ###
    ### END /etc/grub.d/01_users ###
    ### BEGIN /etc/grub.d/10_linux ###
    ### END /etc/grub.d/10_linux ###
    ### BEGIN /etc/grub.d/20_linux_xen ###
    ### END /etc/grub.d/20_linux_xen ###
    ### BEGIN /etc/grub.d/20_ppc_terminfo ###
    ### END /etc/grub.d/20_ppc_terminfo ###
    ### BEGIN /etc/grub.d/30_os-prober ###
    ### END /etc/grub.d/30_os-prober ###
    ### BEGIN /etc/grub.d/40_custom ###
    # This file provides an easy way to add custom menu entries.  Simply type the
    # menu entries you want to add after this comment.  Be careful not to change
    # the 'exec tail' line above.
    ### END /etc/grub.d/40_custom ###
    ### BEGIN /etc/grub.d/41_custom ###
    ### END /etc/grub.d/41_custom ###
    

    12、打出netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行;

    netstat -tan  | grep  "LISTEN[[:blank:]]\+$"
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    tcp6       0      0 :::111                  :::*                    LISTEN     
    

    13、添加用户bash, testbash, basher, nologin (此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息;

    useradd bash
    useradd testbash
    useradd bahser
    useradd -s /sbin/nologin nologin
    grep  "^\(.*\):.*\b\1$"  /etc/passwd 
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    bash:x:1003:1006::/home/bash:/bin/bash
    nologin:x:1006:1009::/home/nologin:/sbin/nologin
    

    相关文章

      网友评论

          本文标题:文件权限以及正则表达式实例

          本文链接:https://www.haomeiwen.com/subject/plfdgftx.html