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

举例如下:

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

使用stat命令可以查看
[linuxidc@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/ linuxidc) 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

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

sed,cut

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

sed,awk

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

方法2:stat -c 命令

[linuxidc@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        最后更改的时间的时间戳

使用参数结果如下:
[linuxidc@localhost ~]$ ls -l 1.txt
-rw-r--r-- 1 linuxidc wheel 38 Oct 12 16:29 1.txt
[linuxidc@localhost ~]$ stat -c %a 1.txt
644
[linuxidc@localhost ~]$ stat -c %A 1.txt
-rw-r--r--
[linuxidc@localhost ~]$ stat -c %b 1.txt
8
[linuxidc@localhost ~]$ stat -c %B 1.txt
512
[linuxidc@localhost ~]$ stat -c %d 1.txt
64768
[linuxidc@localhost ~]$ stat -c %F 1.txt
regular file
[linuxidc@localhost ~]$ stat -c %g 1.txt
10
[linuxidc@localhost ~]$ stat -c %G 1.txt
wheel
[linuxidc@localhost ~]$ stat -c %u 1.txt
503
[linuxidc@localhost ~]$ stat -c %U 1.txt
linuxidc
[baby@localhost ~]$ stat -c %h 1.txt
1
[linuxidc@localhost ~]$ stat -c %i 1.txt
390954
[linuxidc@localhost ~]$ stat -c %n 1.txt
1.txt
[linuxidc@localhost ~]$ stat -c %o 1.txt
4096
[linuxidc@localhost ~]$ stat -c %s 1.txt
38

stat用法:获取文件对应权限的数字的更多相关文章

  1. seek和tell的用法--获取文件内容大小(字节)

    /*获取文件中存取的数据内容的大小(字节数) ellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get ...

  2. dirname的用法:获取文件的父级目录路径

    命令:dirname 获取文件的路径(到父级目录)用法:dirname file_name [root@bogon opt]# a=$(dirname /mnt/a/b/c/d/a.sh) [root ...

  3. 获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件

    演示如何获取文件的缩略图 FileSystem/ThumbnailAccess.xaml <Page x:Class="XamlDemo.FileSystem.ThumbnailAcc ...

  4. PHP获取文件扩展名的多种方法

    PHP获取文件扩展名的N种方法. 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1): } 第2种方法: fun ...

  5. ASP php获取文件URL地址等方法

    $_SERVER["HTTP_REFERER"] rss中可用 echo next(explode("=", $_SERVER["QUERY_STRI ...

  6. PHP获取文件的绝对路径

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ===========PH ...

  7. 【apache tika】apache tika获取文件内容(与FileUtils的对比)

    Tika支持多种功能: 文档类型检测 内容提取 元数据提取 语言检测 重要特点: 统一解析器接口:Tika封装在一个单一的解析器接口的第三方解析器库.由于这个特征,用户逸出从选择合适的解析器库的负担, ...

  8. JAVA 获取文件的MD5值大小以及常见的工具类

    /** * 获取文件的MD5值大小 * * @param file * 文件对象 * @return */ public static String getMD5(File file) { FileI ...

  9. node 的fs.state 获取文件信息

    1. fs.stat()可以获取文件的信息,用法如下: const fs = require('fs'); fs.stat('./book.js',(err,stats)=>{ if(err) ...

随机推荐

  1. [LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  2. Python Pandas找到缺失值的位置

    python pandas判断缺失值一般采用 isnull(),然而生成的却是所有数据的true/false矩阵,对于庞大的数据dataframe,很难一眼看出来哪个数据缺失,一共有多少个缺失数据,缺 ...

  3. pycharm Unresolved reference 无法引入包

    1. 问题描述: 在项目中P存在文件夹A.B.C,A有文件夹a和b,在a中引入b的一个类, a.py: from b import func1 虽然运行成功,但是在Pycharm中显示: Unreso ...

  4. Liferay中request

    在liferay中的请求分为renderRequest和actionRequest这两种请求的方式,portletRequest的子类有三个1renderRequest,2EventRequest3C ...

  5. cocos代码研究(7)即时动作子类学习笔记

    理论部分 即时动作是会立即被执行的动作.他们没有持续时间动作(ActionInterval)的持续时间属性.继承自 FiniteTimeAction.被 CallFunc, FlipX, FlipY, ...

  6. mysql的Navicat查看数据库的ER图

    1.mysql数据库表间的关系图可以通过navicat查看.

  7. 使用PowerMockito 对静态类进行mock

    Mock的中文的意思就是模拟,Mockito是mock的扩展,但是Mockito并不支持对静态类的mock,所以我们引入PowerMockito实现对静态类的mock. 首先pom添加PowerMoc ...

  8. linux常用命令:tar 命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...

  9. PDF常见问题总结

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  10. 发布QT exe

    https://blog.csdn.net/u014453443/article/details/85837138