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.)
原來讀權(quán)限僅僅是讀取目錄下的list(文件列表), 執(zhí)行權(quán)限是access(訪問)目錄下的文件。
這下明白了,見用大學宿舍一哥們唱的一首《18摸》來解釋:
做實驗證明:
root$ mkdir test/d1 -p
root$ touch test/f1
root$ echo "test" > test/f2
驗證讀權(quán)限:
root$ chmod 004 test (r讀權(quán)限)
robin$ ls test (可以看到d1, f1, f2)
robin$ cat test/f2 (Permission denied)
root$ chmod 005 test (rx讀執(zhí)行權(quán)限)
robin$ cat test/f2 (看到“test”了)
驗證執(zhí)行權(quán)限:
root$ chmod 001 test (執(zhí)行權(quán)限)
robin$ ls test (這邊沒有東西了哦!)
robin$ cat test/f2(可以看到“test”)
驗證寫權(quán)限:
root$ chmod 002 (w寫權(quán)限)
robin$ echo "test2" > test/f3 (Permission denied)
root$ chmod 003 (wx寫執(zhí)行權(quán)限)
robin$ echo "test2" > test/f3 (成功)
這下清清楚楚! 不迷惑了!
由此可以看出:如果目錄沒有執(zhí)行權(quán)限,搜索時也不會找到文件及其內(nèi)容哦!