1.如何取得/etc/hosts文件的权限对应的数字内容,-rw-r–r–为644,要求使用命令取得644这样的数字。
解答:
思路:
1.通过stat输出包含目标的内容。
2.通过head,tail,sed,awk,grep定位到单行。(取行惯用命令)
3.通过cut,awk等设置分隔符取出需要段内容。(取列惯用命令)
[root@ianLinux ~]# stat /etc/hosts
awk多分隔符方法:
[root@ianLinux ~]# stat /etc/hosts|sed -n '4p'|awk -F "[0/]" '{print $2}'
# 或
[root@ianLinux ~]# stat /etc/hosts|awk -F "[0/]" 'NR==4 {print $2}'
sed后向引用方法:
[root@ianLinux ~]# stat /etc/hosts|sed -nr '4s#^.*\(0(.*)/-.*$#\1#gp'
犯的错误:
①
^.*(0(.*)改为^.*\(0(.*)
②
把双引号改为单引号。
stat加参数的方法:
stat可以列出文件的详细信息。
man一下stat,看这些信息是否能通过参数取出。
-c –format=FORMAT
use the specified FORMAT instead of the default; out-put a newline after each use of FORMAT
[root@ianLinux ~]# stat -c %a /etc/hosts
获取Linux文件权限
本文介绍如何使用Linux命令获取/etc/hosts文件的权限,并将其转换为数字形式,如将-rw-r--r--转换为644。文中提供了多种方法,包括使用stat、sed、awk等命令组合实现这一目标。
1310

被折叠的 条评论
为什么被折叠?



