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


    题目:文件属性为-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

  • 相关阅读:
    PTA——List Leaves
    pta——电话聊天狂人(c二叉树实现)
    Anti-SG游戏 与 SJ定理笔记(反Nim博弈)
    Unicode代码点与代码单元
    奇偶校验位
    IPv6与IPv4的位数
    0- win10配置java环境变量问题
    小计划
    路径问题
    getResource(path)的注意事项
  • 原文地址:https://www.cnblogs.com/sea-stream/p/9924080.html
Copyright © 2020-2023  润新知