The Execute Bit on a Directory Means Traverse, Not Run
On a file the execute bit means run this program. On a directory it means resolve names through this directory, and the read bit separately means list the names. They are different permissions and they fail in different ways.
At a glance
- Execute on a file
- Permission to run the file as a program
- Execute on a directory
- Permission to traverse it, meaning resolve a name through it to reach what is inside
- Read on a directory
- Permission to list the names it contains. Nothing more
- Read without execute
- You can list the names and cannot stat or open any of them. ls -l shows question marks
- Execute without read
- You can open a path you already know and cannot discover any names
- Write on a directory
- Permission to create, rename and delete entries, regardless of who owns them
- A directory at 644
- Cannot be entered by anyone, including its owner, because no class has execute
In detail
The single most common permissions misunderstanding is that read lets you into a directory. It does not. Reading a directory means obtaining the list of names it holds, and that is the whole of it. Getting to anything inside, including calling stat on it to find out its size or its own permissions, requires the execute bit on the directory.
This is why a directory at 644 is useless. It looks like a sensible file mode, and applied to a directory it removes traversal from every class, so even the owner cannot cd into it or open anything within. The same applies to 664, 640 and 600 on directories: all are unenterable, and all are what a recursive chmod with a file mode produces.
The two failure modes look different, which is a useful diagnostic. Read without execute gives you a listing where every entry shows as question marks, because ls got the names from the directory and then failed to stat each one. Execute without read gives a clean permission denied from ls while cat on a known path succeeds.
Traversal is checked at every component of the path, not just the last one. Opening /home/alice/site/index.html needs execute on /, on home, on alice and on site, and read only on the file itself. One directory anywhere in that chain without execute for the requesting account produces a permission denied that appears to come from the wrong place entirely.
The command that untangles this is namei -l, which prints the mode, owner and group of every component of a path in order. Run it as the user that is failing, and the offending directory is usually obvious in the first three lines. Guessing at modes further down the tree is what turns a two minute problem into an afternoon.
Examples
$ chmod 644 data
$ ls data
report.csv notes.txt
$ ls -l data
ls: cannot access 'data/report.csv': Permission denied
-????????? ? ? ? ? report.csv
$ cd data
bash: cd: data: Permission denied$ chmod 111 data
$ ls data
ls: cannot open directory 'data': Permission denied
$ cat data/report.csv
id,name
1,alice # works, the name was known$ namei -l /home/alice/site/index.html
f: /home/alice/site/index.html
drwxr-xr-x root root /
drwxr-xr-x root root home
drwx------ alice alice alice <- traversal stops here
drwxr-xr-x alice alice site
-rw-r--r-- alice alice index.htmlThings that catch people out
- A recursive chmod with a file mode makes every directory in the tree unenterable, including for the owner. Separate files from directories with find, or use the capital X form.
- The execute bit is needed on every directory in the path, not only the last one. A private home directory blocks a public directory inside it.
- ls -l showing question marks does not mean the files are corrupt. It means ls could read the directory and could not stat the entries, which is read without execute.
- Deleting a file needs write and execute on its directory and nothing at all on the file. That is why a read only file in a writable directory can still be removed.
Try it: Chmod Calculator
Tick the nine permission boxes, or type an octal or symbolic value, and get the three digit and four digit octal, the ls -l rendering and the exact chmod command back.
Chmod Calculator
LOCAL · Browser-OnlyCalculate Unix file permissions with an interactive chmod builder
Permission Matrix
| Entity | Read (4) | Write (2) | Execute (1) | Octal | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 7 | rwx | |||
| Group (g) | 5 | r-x | |||
| Others (o) | 5 | r-x |
Special Bits
Advanced permission flags for executables and directories
Generated Permissions
755rwxr-xr-x-rwxr-xr-xchmod 755 filenamechmod u=rwx,g=rx,o=rx filenamePermission Breakdown
Owner (u)
7rwx- - Read files / list directory contents
- - Write / modify / delete
- - Execute / traverse directory
Group (g)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Others (o)
5r-x- - Read files / list directory contents
- - Execute / traverse directory
Common Permissions Reference
| Octal | Symbolic | Meaning |
|---|---|---|
777 | rwxrwxrwx | Full access for everyone |
755 | rwxr-xr-x | Owner full, others read and execute |
750 | rwxr-x--- | Owner full, group read and execute |
700 | rwx------ | Owner full access only |
666 | rw-rw-rw- | Read and write for everyone |
644 | rw-r--r-- | Owner read/write, others read only |
600 | rw------- | Owner read/write only |
555 | r-xr-xr-x | Read and execute for everyone |
444 | r--r--r-- | Read only for everyone |
400 | r-------- | Owner read only |
Related File permissions
All file permissions →chmod 755: The Standard Mode for Directories and Programs
755 gives the owner full control and everyone else read and execute. Why it is right for directories and executables and wrong for ordinary data files.
chmod 711: Directories You Can Enter but Cannot List
711 gives others execute without read, so they can enter a directory by exact name but cannot list it. Where this is used and what it does not protect.
chmod 644: The Default File Mode and When to Change It
644 gives the owner read and write and everyone else read only. Why it is the default for files under a 022 umask, and the cases where it is wrong.
Recursive chmod and Why 755 Across a Source Tree Is Wrong
chmod -R applies one mode to files and directories alike, which is almost never what you want. The find and capital X patterns that do the job properly.
umask: How the Default Mode of a New File Is Decided
umask removes permission bits from newly created files and directories. Why 022 gives 644 and 755, and why a new file is never executable.
Symbolic chmod Notation: u, g, o, a With Plus, Minus and Equals
Symbolic mode changes one class at a time instead of setting all nine bits. How plus, minus and equals differ, and what the capital X form is for.
Tools for this
- Chmod Calculator Free online chmod calculator - calculate Unix file permissions with an interactive chmod builder