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 (成功)
這下清清楚楚! 不迷惑了!
由此可以看出:如果目錄沒有執行權限,搜索時也不會找到文件及其內容哦!