+++ date = 2022-10-04 title = "Linux Directory Permissions" +++
<style> table.styled { border-collapse: collapse; } .styled td, .styled th { border: thin solid black; text-align: center; padding: 0.3rem; } .styled thead tr { background-color: #F0F0F0; text-align: left; } .read { color: #904000; } .write { color: #8B0000; } .execute { color: #008000; } .term { font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; border: thin solid black; padding: 0.5rem; margin: 1rem; } .term.command { background-color: #F0F0F0; } .term .output { white-space: pre; display: none; overflow: scroll; } </style>Read, write and execute permissions are used for directories as well as files on Linux but their meaning for directories is not as straight forward1.
The permissions needed for each action are:
| Action | Required Permission(s) |
|---|---|
| Listing the entries of the directory | Read |
| Adding or removing entries from the directory | Write and Execute (see also "Restricted Deletion" below) |
| "cd" into the directory | Execute |
| Accessing the directory entries' contents or entry metadata | Execute |
| Modifying the directory entries' contents or entry metadata | Execute |
| Remove the directory. | As with normal files, the permissions of the parent directory and not the directory itself determine if you can remove it. The directory must be empty. |
Or listed inversely each permission allows:
| Permission | Symbol | Value | Description for directories |
|---|---|---|---|
| Read | r | 4 | Allows listing the names of entries in the directory (e.g. using "ls") but not entry metadata or contents. |
| Write | w | 2 | Allows adding, removing or moving entries (but not their contents). |
| Execute | x | 1 | Allows accessing and modifying the entry contents and metadata. Also needed to add, delete or remove entries. Also called "Search" for directories2. |
See how the following commands are handled when executed on a directory named testdir that starts with a single file named A.txt when the executing user has the permissions you choose:
{{ linux_directory_permissions_commands() }}
<script> const r = document.getElementById("read"); const w = document.getElementById("write"); const x = document.getElementById("execute"); const modeLabel = document.getElementById("mode"); const form = document.getElementById("permissions_form"); function updateOutputs() { let modeString = (r.checked ? "r" : "") + (w.checked ? "w" : "") + (x.checked ? "x" : ""); modeLabel.innerHTML = modeString if (modeString == "") { modeString = "EMPTY"; } const outputs = document.querySelectorAll(".term .output"); for (output of outputs) { output.style.display = output.classList.contains(modeString) ? "block" : "none"; } } updateOutputs(); form.addEventListener("input", (evt) => { updateOutputs(); }); </script>- A directory where your user has execute but not read permissions would allow you to edit or modify files that you can not list with "ls" if you already know their path.
- A directory where your user has write and execute but not read would allow you to add new files and modify existing files but not see them.
- A directory with only write permissions is the same as a directory with no permissions3.
- Most of the time you will always want to set execute if you are also setting either read or write.
- Restricted Deletion (also called Sticky Bit) - For directories only lets the file or directory owner move or delete a file inside the directory.
- Set Group Identity (setgid) - For directories causes new files to be created with the same group as the directory rather than the group of the process creating the file.
- Set User Identity (setuid) - Ignored by Linux and most Unix systems.
1. This article is focused on directory permissions. For an intro to Linux permissions in general you can read this Linux Foundation article, the chmod man page or search for more.
2. The chmod man page is the main source of this page's information.
3. See this SO question. I like the OPs analogy of "execute" permitting access to inodes.