chown
權限與擁有者
用法
sudo chown [user]:[group] [path]修改檔案的擁有者和群組;把檔案送給別人只有 root 能做,所以它幾乎總要配 sudo。
常用選項
| 選項 | 含義 |
|---|---|
| user:group | Set owner and group at once; :group alone sets only the group |
| -R | Apply to a whole tree |
| -h | Change the symlink itself, not what it points at |
| -v | Name each file as it changes |
| --reference | GNU: copy owner and group from another file |
範例
sudo chown -R www-data:www-data /var/wwwHands the web root to the server user.
sudo chown :staff report.pdfChanges only the group.
sudo chown -R $USER ~/.npmTakes back a directory root created for you.
數字模式和符號模式說的是同一件事:755 和 u=rwx,go=rx 是同一個權限。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. chown 是做什麼的?
修改檔案的擁有者和群組;把檔案送給別人只有 root 能做,所以它幾乎總要配 sudo。
Q. 怎麼寫?
sudo chown [user]:[group] [path] —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 5 個,完整列表在 man chown。這條命令屬於權限與擁有者。
相關命令
man 手冊: man chown