如何取得/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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM