vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

8.30 linux文件权限(ll/stat/getfacl命令) #56

Closed vieyahn2017 closed 5 years ago

vieyahn2017 commented 5 years ago

linux文件权限命令

ll 1.sh
stat -c %a 1.sh
getfacl 1.sh
vieyahn2017 commented 5 years ago

ll image

vieyahn2017 commented 5 years ago

Linux 查看文件权限命令ls -l 输出信息每列所代表的含义 https://blog.csdn.net/huxinguang_ios/article/details/81026811

vieyahn2017 commented 5 years ago

getfacl命令

getfacl 2.2.49 -- get file access control lists

$ getfacl trap.sh
# file: trap.sh
# owner: Administrator
# group: 197121 <unknown>
user::rwx
group::r-x
other:r-x

对应也有setfacl,不过没有chmod那么通用

vieyahn2017 commented 5 years ago

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

stat -c 命令

[linuxidc@localhost ~]$ stat -c %a 1.txt 644 [linuxidc@localhost ~]$ stat -c %A 1.txt -rw-r--r--

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

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

vieyahn2017 commented 5 years ago

stat方法二 文本截取

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命令;

使用正则或命令取 head,tail,cut

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

vieyahn2017 commented 5 years ago

通过stat命令获取/etc/hosts文件的权限对应的数字

2017年09月04日 18:22:14 weixin_34320159 阅读数 20 原文链接:http://blog.51cto.com/nange/1962612 命令获取/etc/hosts文件的权限对应的数字

第一步 查看文件的权限

stat /etc/hosts

  File: `/etc/hosts'

  Size: 158         Blocks:8          IO Block: 4096   regular file

Device: 803h/2051d   Inode: 915740      Links: 2

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

Access: 2017-09-0410:54:49.197752741 +0800

Modify: 2017-08-2321:57:12.148687266 +0800

Change: 2017-08-2321:57:12.149687266 +0800

第二步 截取第4行

stat /etc/hosts | sed -n '4p'
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)

stat /etc/hosts | awk "NR==4"
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)

stat /etc/hosts | head -n4 | tail -n1 
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)

第三步 进行截取

方法一 反向引用

stat /etc/hosts | sed -nr '4s#^.*\(0(.*)/-.*$#\1#gp'

644

方法二 awk指定分隔符

stat /etc/hosts | awk -F "[0/]" 'NR==4{print $2}'

644

方法三 正则表达式

stat /etc/hosts | awk "NR==4" | egrep "[1-7]{3}" -o

644

方法四 正则表达式

stat /etc/hosts | sed -n '4s#[^1-7]# #gp'

644                                                           

这个例子方法三、四,还有干扰结果项。需要修正

vieyahn2017 commented 5 years ago

上面的文本截取,还是最awk给力 stat /etc/hosts | awk -F "[0/]" 'NR==4{print $2}'