*nix下如何处理带'-'的文件(夹) -xxx 在xfce4中设置快捷键[更新]
Dec 28

刚才在#gentoo-cn 的irc里有个Guest10086的人问为啥一个目录需要(+x)execute权限,可能很多人都会觉得奇怪.为什么需要.记得以前也有过同样的疑问.今天google了一把找到了原因.

The chdir() system call requires execute permission on a directory.  Of course a directory isn't really a program that you can run, even if it has execute permission.  The execute bit is reused rather than waste space with additional permission bits. Besides controlling a user's ability to cd into some directory, the execute permission is required on a directory to use the stat() system call on files within that directory.  The stat() system called is used to access the information in a file's inode, and must be done before you can open or delete (via the unlink() system call) that file.  Because of its role in file access the execute bit on a directory is sometimes called search permission.  For example, to read a file foo/bar, you must have read permission for the file itself, but before the file can be accessed you must first search the directory foo for the inode of file bar.  This requires search ("x") permission on the directory foo.  (Note you don't need read permission on the directory in this case!  You only need read permission on a directory to list its contents.)

接下来做测试.


rory@gentoo ~/test $ mkdir a
rory@gentoo ~/test $ ll
total 4
drwxr-xr-x 2 rory rory 4096 2008-12-28 13:20 a
rory@gentoo ~/test $ chmod -r a
rory@gentoo ~/test $ ll
total 4
d-wx--x--x 2 rory rory 4096 2008-12-28 13:20 a
rory@gentoo ~/test $ mkdir a/sub
rory@gentoo ~/test $ ls a
ls: cannot open directory a: Permission denied
rory@gentoo ~/test $ touch a/file.txt
rory@gentoo ~/test $ echo "File Content">>a/file.txt
rory@gentoo ~/test $ more a/file.txt
File Content
rory@gentoo ~/test $ chmod +r a
rory@gentoo ~/test $ ls a
file.txt  sub
rory@gentoo ~/test $ ll a
total 8
-rw-r--r-- 1 rory rory   13 2008-12-28 13:22 file.txt
drwxr-xr-x 2 rory rory 4096 2008-12-28 13:20 sub
rory@gentoo ~/test $ chmod -x a
rory@gentoo ~/test $ more a/file.txt
a/file.txt: Permission denied
rory@gentoo ~/test $ ls a
ls: cannot access a/sub: Permission denied
ls: cannot access a/file.txt: Permission denied
file.txt  sub

参考


Like others

5 Responses to “为什么*nix下目录需要有可执行权限”

  1. Sutra Says:

    如果目录没有x权限,那你在这个目录里什么权限都没有。

  2. 莫多(somebody) Says:

    对比起来如果没有read权限还能做不少事情.

  3. r0bertz Says:

    如果你看过APUE,所有类似这样的问题都会感觉是顺理成章的

  4. 莫多(somebody) Says:

    确实没有看过APUE,因为我涉及的领域都是上层的开发.当时在学校学习linux基础的时候也没有太过重视.

    我想大多数有这个疑问的人都是受windows的影响吧.总觉得可执行就是 xxx.exe .

    都是受windows的毒害啊.

    看来有时间可以看看大版主推荐的这本Advanced Programming in the UNIX Environment.补补功课

  5. findsw Says:

    Besides controlling a user's ability to cd into some directory, the execute permission is required on a directory to use the stat() system call on files within that directory.  The stat() system called is used to access the information in a file's inode, and must be done before you can open or delete (via the unlink() system call) that file.  

Leave a Reply

Identifying Code