題目:檔案屬性為-rw-r--r-- 對應權限為644,如何使用指令擷取權限對應的數字??
舉例如下:
1
2
<code>[baby@localhost ~]$ ll -l</code>
<code>-rw-r--r-- 1 baby wheel 38 Oct 12 16:29 1.txt</code>
使用stat指令可以檢視
[baby@localhost ~]$ stat 1.txt
File: `1.txt'
Size: 38 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 390954 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 503/ baby) Gid: ( 10/ wheel)
Access: 2015-10-12 16:29:34.674990005 +0800
Modify: 2015-10-12 16:29:32.248990536 +0800
Change: 2015-10-12 16:29:32.248990536 +0800
取出對應的數字則需要使用正則sed awk 或cut ,head,tail指令;
方法1:使用正則或指令取
head,tail,cut
<code>[baby@localhost ~]$ stat 1.txt |</code><code>head</code> <code>-n4|</code><code>tail</code> <code>-n1|</code><code>cut</code> <code>-d </code><code>"/"</code> <code>-f1|</code><code>cut</code> <code>-d </code><code>"("</code> <code>-f2</code>
<code>0644</code>
sed,cut
<code>[baby@localhost ~]$ stat 1.txt |</code><code>sed</code> <code>-n </code><code>'4p'</code><code>|</code><code>cut</code> <code>-d </code><code>"/"</code> <code>-f1|</code><code>cut</code> <code>-d </code><code>"("</code> <code>-f2</code>
sed,awk
<code>[baby@localhost ~]$ stat 1.txt |</code><code>sed</code> <code>-n </code><code>'4p'</code><code>|</code><code>awk</code> <code>-F</code><code>"/"</code> <code>'{print $1}'</code><code>|</code><code>awk</code> <code>-F</code><code>"("</code> <code>'{print $2}'</code>
方法2:stat -c 指令
<code>[baby@localhost ~]$ stat -c %a 1.txt</code>
<code>644</code>
注意:如何想到法二的思考過程,比答題更重要。當指令結果包含我們需要的内容的時候,我們要想到是否有具體的參數能夠一步達到我們需要的結果。
man stat 檢視幫助
-c --format=FORMAT
use the specified FORMAT instead of the default; output a new line after each use of FORMAT
使用特殊格式代替預設輸出;
常用的參數有如下:
%a Access rights in octal 8進制顯示通路權限,0644
%A Access rights in human readable form 以人類可讀的形式輸出,
%F File type 檔案的類型
%g Group ID of owner 所屬組gid的号碼
%G Group name of owner 所屬組的名稱
%h Number of hard links 硬連接配接的數量
%i Inode number inode的值
%n File name 檔案名
%o I/O block size IO塊大小
%s Total size, in bytes 檔案的總大小,位元組顯示;
%u User ID of owner 所屬主的uid号碼
%U User name of owner 所屬主的名稱
%x Time of last access 最後通路的時間
%X Time of last access as seconds since Epoch 最後通路時間的時間戳
%y Time of last modification 最後修改的時間
%Y Time of last modification as seconds since Epoch 最後修改時間的時間戳
%z Time of last change 最後更改的時間
%Z Time of last change as seconds since Epoch 最後更改的時間的時間戳
使用參數結果如下:
[baby@localhost ~]$ ls -l 1.txt
-rw-r--r-- 1 baby wheel 38 Oct 12 16:29 1.txt
[baby@localhost ~]$ stat -c %a 1.txt
644
[baby@localhost ~]$ stat -c %A 1.txt
-rw-r--r--
[baby@localhost ~]$ stat -c %b 1.txt
8
[baby@localhost ~]$ stat -c %B 1.txt
512
[baby@localhost ~]$ stat -c %d 1.txt
64768
[baby@localhost ~]$ stat -c %F 1.txt
regular file
[baby@localhost ~]$ stat -c %g 1.txt
10
[baby@localhost ~]$ stat -c %G 1.txt
wheel
[baby@localhost ~]$ stat -c %u 1.txt
503
[baby@localhost ~]$ stat -c %U 1.txt
baby
[baby@localhost ~]$ stat -c %h 1.txt
[baby@localhost ~]$ stat -c %i 1.txt
390954
[baby@localhost ~]$ stat -c %n 1.txt
1.txt
[baby@localhost ~]$ stat -c %o 1.txt
4096
[baby@localhost ~]$ stat -c %s 1.txt
38
題目和方法2的思想來源于51cto老男孩部落格。非常感謝。
本文轉自 模範生 51CTO部落格,原文連結:http://blog.51cto.com/mofansheng/1703428,如需轉載請自行聯系原作者