linux/unix 下的文件 rwx 想必大家很熟悉了!但对目录的rwx是怎么理解的呢?
今天碰到了这个问题,一时还真回答不上来,例如:
- 目录执行权限能干吗?
- 目录只有写权限就可以写入文件到该目录了吗?
于是查看了一下资料,《Advanced Programming in the UNIX》中解释如下:
Note that read permission for a directory and execute permission for a directory mean different things. Read permission lets us read the directory, obtaining a list of all the filenames in the directory. Execute permission lets us pass through the directory when it is a component of a pathname that we are trying to access. (We need to search the directory to look for a specific filename.)
原来读权限仅仅是读取目录下的list(文件列表), 执行权限是access(访问)目录下的文件。
这下明白了,见用大学宿舍一哥们唱的一首《18摸》来解释:
- 读权限:“只能让你看,不能让你摸”
- 执行权限:“只能让你摸,不能让你看”
- 写权限:“先让摸,后让写” 换句话 “ 摸都不让,写也就没门了” (歌词没这句,呵呵!)
做实验证明:
root$ mkdir test/d1 -p
root$ touch test/f1
root$ echo "test" > test/f2
验证读权限:
root$ chmod 004 test (r读权限)
robin$ ls test (可以看到d1, f1, f2)
robin$ cat test/f2 (Permission denied)
root$ chmod 005 test (rx读执行权限)
robin$ cat test/f2 (看到“test”了)
验证执行权限:
root$ chmod 001 test (执行权限)
robin$ ls test (这边没有东西了哦!)
robin$ cat test/f2(可以看到“test”)
验证写权限:
root$ chmod 002 (w写权限)
robin$ echo "test2" > test/f3 (Permission denied)
root$ chmod 003 (wx写执行权限)
robin$ echo "test2" > test/f3 (成功)
这下清清楚楚! 不迷惑了!
由此可以看出:如果目录没有执行权限,搜索时也不会找到文件及其内容哦!