首页 > 技术文章 > Linux文件属性

hjhsysu 2016-08-03 19:30 原文


1.文件属性

[root@root ~]# ls -al
total 136
dr-xr-x---.  5 root root  4096 Mar 22 10:35 .
dr-xr-xr-x. 27 root root  4096 Feb 22 09:43 ..
-rw-------.  1 root root  1138 May 29  2013 anaconda-ks.cfg
-rw-------.  1 root root 34728 May 13 17:07 .bash_history
-rw-r--r--.  1 root root    18 May 20  2009 .bash_logout
-rw-r--r--.  1 root root   176 May 20  2009 .bash_profile
-rw-r--r--.  1 root root   316 Dec 10  2013 .bashrc
-rw-r--r--.  1 root root   100 Sep 23  2004 .cshrc
drwxr-xr-x.  2 root root  4096 Mar 15 18:10 EcafeDemo
-rw-r--r--.  1 root root 26090 May 29  2013 install.log
-rw-r--r--.  1 root root  7345 May 29  2013 install.log.syslog
-rw-------.  1 root root    50 Mar 16 19:04 .lesshst

其中:install.log的文件权限为:-rw-r--r--

 

第一个字符代表这个文件的类型(如目录、文件或链接文件等等):

当为[ d ]则是目录,例如上表档名为『.gconf』的那一行;

当为[ - ]则是文件,例如上表档名为『install.log』那一行;

若是[ l ]则表示为连结档(link file);

若是[ b ]则表示为装置文件里面的可供储存的接口设备(可随机存取装置);

若是[ c ]则表示为装置文件里面的串行端口设备,例如键盘、鼠标(一次性读取装置)

 

接下来的字符中,以三个为一组,且均为『rwx』 的三个参数的组合

[r]:表示可读

[w]:表示可写

[x]:表示可执行

第一组为『文件所有者的权限』,以『install.log』那个文件为例, 该文件的拥有者可以读写,但不可执行;

第二组为『同用户组的权限』;

第三组为『其他非本用户组的权限』.

 

目录的读权限: 可以查看目录本身的信息(ll -d)。

目录的写权限: 可以在目录下面创建和删除文件。

目录的执行权限:可以获得目录下文件的列表,和进入目录。要对目录下存在的文件进行读取和修改,必须要进入目录,所以必须要目录有执行权限。


2.修改文件属性和权限

chgrp:改变文件所属用户组

chown:改变文件所有者

chmod:改变文件权限

 

其中chown也可以同时修改文件所有者和文件所属用户组,比如:

将install.log的所有者和用户改为user

chown user:user install.log

chown也可以单纯地修改所属用户组,比如:

chown .user install.log

 

chmod可以通过 u, g, o来代表三种权限,比如:

chmod u=rwx, go=rx  .bashrc

也可以通过a+x,或者a-x给每个文件写入/去掉写入的权限

chmod a+x filename

 

推荐阅读