命令获取/etc/hosts文件的权限对应的数字
第一步 查看文件的权限
[[email protected] ~]# stat /etc/hosts File: `/etc/hosts‘ Size: 158 Blocks:8 IO Block: 4096 regular file Device: 803h/2051d Inode: 915740 Links: 2 Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-09-0410:54:49.197752741 +0800 Modify: 2017-08-2321:57:12.148687266 +0800 Change: 2017-08-2321:57:12.149687266 +0800 [[email protected] ~]# |
第二步 截取第4行
[[email protected] ~]# stat/etc/hosts | sed -n ‘4p‘ Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) [[email protected] ~]# stat/etc/hosts | awk"NR==4" Access:(0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) [[email protected] ~]# |
第三步 进行截取
方法一 反向引用
[[email protected] ~]# stat /etc/hosts|sed -nr ‘4s#^.*\(0(.*)/-.*$#\1#gp‘ 644 [[email protected] ~]# |
方法二 awk指定分隔符
[[email protected] ~]# stat /etc/hosts|awk -F "[0/]" ‘NR==4{print $2}‘ 644 [[email protected] ~]# |
方法三 正则表达式
[[email protected] ~]# stat /etc/hosts|awk"NR==4"|egrep"[1-7]{3}" -o 644 [[email protected] ~]# |
方法四 正则表达式
[[email protected] ~]# stat /etc/hosts|sed -n ‘4s#[^1-7]# #gp‘ 644 [[email protected] ~]# |