stat用法:获取文件对应权限的数字

题目:文件属性为-rw-r--r-- 对应权限为644,如何使用命令获取权限对应的数字??


举例如下:

[baby@localhost ~]$ ll -l
-rw-r--r-- 1 baby wheel 38 Oct 12 16:29 1.txt

使用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

[baby@localhost ~]$ stat 1.txt |head -n4|tail -n1|cut -d "/" -f1|cut -d "(" -f2
0644


sed,cut

[baby@localhost ~]$ stat 1.txt |sed -n '4p'|cut -d "/" -f1|cut -d "(" -f2
0644

sed,awk

[baby@localhost ~]$ stat 1.txt |sed -n '4p'|awk -F"/" '{print $1}'|awk -F"(" '{print $2}'
0644


方法2:stat -c 命令

[baby@localhost ~]$ stat -c %a 1.txt
644

注意:如何想到法二的思考过程,比答题更重要。当命令结果包含我们需要的内容的时候,我们要想到是否有具体的参数能够一步达到我们需要的结果。

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
1
[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老男孩博客。非常感谢。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值