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

这道题考察的内容是怎么查看文件的权限,以及对权限对应数字的过滤

首先查看权限对应的数字内容使用stat命令来查看

[root@zhang ~]# stat /etc/hosts
File: ‘/etc/hosts’
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 16826902 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-27 19:30:01.140839849 +0800
Modify: 2013-06-07 22:31:32.000000000 +0800
Change: 2019-03-27 19:27:19.304832132 +0800
Birth: -

然后对644进行过滤(方法有多种)

1 awk先取行 再取列

[root@zhang ~]# stat /etc/hosts|awk 'NR==4'
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
[root@zhang~]# stat /etc/hosts|awk -F "[0/]" 'NR==4{print $2}'
644

2 sed 先取行 再去掉开头 去掉结尾
(1)[root@zhang ~]# stat /etc/hosts|sed -n 4p
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
[root@zhang ~]# stat /etc/hosts|sed -nr '4s#^.(0(.)/-.*$#\1#gp'
644

(2)sed的后向引用

[root@zhang ~]# stat /etc/hosts|sed -n 's#^.*0\(.*\)/-r.*$#\1#gp'
644

3利用grep来进行查找(不建议使用)

[root@zhang ~]# stat /etc/hosts|grep '644'

644

posted @ 2019-03-28 08:23  慕男  阅读(2054)  评论(0编辑  收藏  举报