如何取得/etc/hosts文件的权限对应的数字内容,如-rw-r--r--644,要求使用命令取得644这样的数字。

解答:

[root@oldboyedu-39-nb ~]# stat /etc/hosts

  File: `/etc/hosts'

  Size: 216                                         Blocks:8          IO Block: 4096   regular file

Device: 803h/2051d                                  Inode:260126      Links: 2

Access: (0644/-rw-r--r--) Uid: (    0/    root)  Gid: (    0/    root)

Access: 2017-07-30 00:02:05.983001919 +0800

Modify: 2017-05-20 22:57:33.530934887 +0800

Change: 2017-05-20 22:57:33.533934704 +0800

[root@oldboyedu-39-nb ~]# stat /etc/hosts|awk 'NR==4'

Access: (0644/-rw-r--r--) Uid: (    0/    root)  Gid: (    0/    root)

[root@oldboyedu-39-nb ~]# stat /etc/hosts|awk 'NR==4'|sed's#^.*(0##g'

644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)

方法一:sed去掉前面,去掉后面

[root@oldboyedu-39-nb ~]# stat /etc/hosts|awk 'NR==4'|sed 's#^.*(0##g'|sed's#/.*$##g'

644

方法二:sed反向引用

[root@oldboyedu39 ~]# stat /etc/hosts | sed -n '4p' | sed -r's#^.*\((.*)/-.*$#\1#g'

0644

[root@oldboyedu39 ~]#

[root@oldboyedu39 ~]# stat /etc/hosts | sed -n '4p' | sed -r's#^.*\(0(.*)/-.*$#\1#g'

644

方法三:awk分隔用法菜刀系列

[root@oldboyedu39 ~]# stat /etc/hosts | awk 'NR==4' |awk -F "[0/]"'{print $2}'

644

[root@oldboyedu39 ~]#

[root@oldboyedu39 ~]# stat /etc/hosts | awk 'NR==4' |awk -F "[0/]"'{print $2}'

644

[root@oldboyedu39 ~]#

[root@oldboyedu39 ~]# stat /etc/hosts | awk -F "[0/]" 'NR==4{print$2}'

644

[root@oldboyedu39 ~]#

[root@oldboyedu39 ~]# stat /etc/hosts | awk -F "[(/]" 'NR==4{print$2}'

0644

4.命令的结果里面有你想要的东西----可能会有现成参数

# stat-c%a /etc/hosts

644