1、取得/etiantian文件的权限对应的数字(考试题答案系列)

说明:本文为老男孩linux培训某节课前考试试题及答案分享博文内容的一部分,也是独立成题的,你可以点下面地址查看全部的内容信息。
http://oldboy.blog.51cto.com/2561410/791245
答题的思维比做题本身更重要,就是老男孩如何想到的解决问题的思路。

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

解答:


实践过程:

[root@oldboy ~]# touch /ett #==>创建测试文件/ett

[root@oldboy ~]# stat /ett #==>通过stat命令可以看到文件的数字权限

  File: `/ett'

  Size: 0               Blocks: 0          IO Block: 4096   regular empty file

Device: 803h/2051d      Inode: 98211       Links: 1

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

Access: 2012-02-20 08:04:24.000000000 +0800

Modify: 2012-02-20 08:04:24.000000000 +0800

Change: 2012-02-20 08:04:24.000000000 +0800

那么我们如何获得这一文件的数字权限呢?

法一过程:(statsedcut

[root@oldboy ~]# stat /ett|sed -n '4p'#==>首先通过管道把stat结果传给sed处理取出需要的行。

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

[root@oldboy ~]# stat /ett|sed -n '4p'|cut -d "/" -f1 #==>对上面的结果以/线为分割符,然后取第1个字段。这里以斜线分割不是唯一的方法,大家注意下。

Access: (0644

[root@oldboy ~]# stat /ett|sed -n '4p'|cut -d "/" -f1|cut -d "(" -f2 #==>这就是法一答案

#==>对上面的结果以(号为分割符,然后取第2个字段,就会得出我们需要的结果。

0644

特别说明:

1)上题中的sed -n '4p'是取结果的第二行的意思,也可以用笨办法head -4|tail -1替代。例:

[root@oldboy ~]# stat /ett|head -4|tail -1|cut -d "/" -f1|cut -d "(" -f2 #==>法二答案

0644

2)上题中的cut -d "/" -f1|cut -d "(" -f2部分,也可以用awk,sed等命令替代。如例:

[root@oldboy ~]# stat /ett|head -4|tail -1|awk -F "/" '{print $1}'|awk -F "(" '{print $2}'

0644 #==>法三答案,awk法如果大家有不懂的,也不用纠结,本文后面问题里会细讲。

提示::此题考察了大家对stat ,cut,awk,head,tailsed等命令组合用法,有对这些命令还不熟悉的同学,可以分步分批总结下。

注意:敲字符时成对出现的’’,{}内容,最好连续敲完,以免后续落下。

法二过程:(stat

当然还有更简单的方法:

[root@oldboy ~]# stat -c %a /ett

644

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

特别说明:

有关stat -c的用法可以通过stat --helpman statinfo stat,这是所有命令的三大帮助杀手锏,必须要掌握了。

 

[root@oldboy ~]# stat --help

Usage: stat [OPTION] FILE... #==>这是语法格式

Display file or file system status.

...省略部分...

  -f, --file-system     display file system status instead of file status

  -c  --format=FORMAT   use the specified FORMAT instead of the default;

                          output a newline after each use of FORMAT

...省略部分...

 

#==>这是可用的参数,如-c

The valid format sequences for files (without --file-system):

#==>这里是对于文件适用的格式,既-c后接的格式。

  %a   Access rights in octal #==>8进制形式显示,即为本文的答案

  %A   Access rights in human readable form #==>拓展以人类可读的形式显示权限

  %b   Number of blocks allocated (see %B)

  %B   The size in bytes of each block reported by %b

  %d   Device number in decimal

  %D   Device number in hex

  %f   Raw mode in hex

  %F   File type

  %g   Group ID of owner

  %G   Group name of owner

  %h   Number of hard links

  %i   Inode number

  %n   File name

  %N   Quoted file name with dereference if symbolic link

  %o   I/O block size

  %s   Total size, in bytes

...省略部分...

本题的拓展部分

[root@oldboy ~]# ls -li /ett

98211 -rw-r--r-- 1 root root 0 Feb 20 08:04 /ett

 

[root@oldboy ~]# stat -c %a /ett 

644

[root@oldboy ~]# stat -c %A /ett #==>获取字符权限

-rw-r--r--

[root@oldboy ~]# stat -c %B /ett

512

[root@oldboy ~]# stat -c %b /ett

0

[root@oldboy ~]# stat -c %i /ett #==>inode信息

98211

[root@oldboy ~]# stat -c %n /ett

/ett

[root@oldboy ~]# stat -c %o /ett #==>block size

4096

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值