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

简介:

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


举例如下:

1
2
[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

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


sed,cut

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

sed,awk

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


方法2:stat -c 命令

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






本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1703428,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
C++
C++学习系列---读取文件名存入txt和从txt读取每行信息
C++学习系列---读取文件名存入txt和从txt读取每行信息
|
4天前
|
安全 Linux
使用 stat命令查看文件信息
使用 stat命令查看文件信息
6 0
|
28天前
|
Shell Linux
linux shell 脚本实现:根据文件内容中的每行分隔符放入数组,根据规则打印日志并重新创建目录 备份文件
linux shell 脚本实现:根据文件内容中的每行分隔符放入数组,根据规则打印日志并重新创建目录 备份文件
22 0
|
12月前
C --- 自定义一个宏打印当前文件名、函数名、行号
C --- 自定义一个宏打印当前文件名、函数名、行号
169 0
|
2月前
|
Linux Perl
【linux】awk用法:在每行新增额外字符串
【linux】awk用法:在每行新增额外字符串
81 0
|
9月前
|
Linux
详细介绍 `ls -ltr` 命令的功能和用法
详细介绍 `ls -ltr` 命令的功能和用法
141 1
详细介绍 `ls -ltr` 命令的功能和用法
|
11月前
|
Shell Linux
Shell /dev/null 文件的含义
Shell /dev/null 文件的含义
53 0
|
12月前
|
Unix Linux PHP
php获取文件的权限信息(获取权限信息、返回字符串涵义、二进制的转换方式、权限修改)
php获取文件的权限信息(获取权限信息、返回字符串涵义、二进制的转换方式、权限修改)
156 0
|
安全 Linux Shell
让我带你一起了解一下 ls -l 命令输出的内容都有哪些意义,以及文件权限如何调整
让我带你一起了解一下 ls -l 命令输出的内容都有哪些意义,以及文件权限如何调整
72 0
|
Ubuntu Linux
[Linux] 输入命令ls -laF后的各字段含义解析
在登陆Ubuntu之后,我们切换超级管理用户root: su root 然后切换到其所在的主目录: cd ~ 然后以该目录下的所有文件以及文件夹为例进行介绍,我们输入命令查看该目录下面的所有文件以及文件夹(包括隐藏文件): ls -laF 然后显示的内容为:
169 0
[Linux] 输入命令ls -laF后的各字段含义解析