File versus directory permissions
On this page
You can see a filename in a directory, yet reading that file reports Permission denied. The file itself has read permission. Checking only its nine permission bits cannot explain the failure.
By the end, you can separate access to file contents from access to directory names, and locate the parent permission that blocks a path.
Before you start
Section titled “Before you start”Finish Reading permission strings. Keep your B04 lab directory. Work only inside disposable ~/lab/b11/access:
mkdir -p ~/lab/b11/access/gatecd ~/lab/b11/accessprintf 'Path read\n' > gate/note.txtchmod 644 gate/note.txtchmod 700 gateThese chmod commands prepare a readable file and a directory fully accessible to its owner. The experiments below use u for the owning user, - to remove a bit, and + to restore it. Full permission-changing syntax belongs to B11.4 in the module outline; here each change isolates one operation.
Use Reset this lesson before repeating the exercise. Keep the current shell in access, outside gate, throughout the experiments.
Give the same letters different jobs
Section titled “Give the same letters different jobs”First verify that the initial path works:
cat gate/note.txtPath readThe file’s r permits reading its contents. The directory’s x permits resolving a name inside it. Both are needed here.
Read this permission matrix as two small tables so each operation remains readable on a phone.
| Bit on a regular file | Operation it permits |
|---|---|
r |
Read the file’s contents |
w |
Modify or truncate the file’s contents |
x |
Attempt execution through the pathname |
| Bit on a directory | Operation it permits |
|---|---|
r |
List names stored in the directory |
w |
Modify directory entries, normally together with search permission |
x |
Search for a known name and traverse the directory in a path |
Directory entries associate names with filesystem objects. Creating, removing, and renaming names change those entries. They are different operations from writing the bytes inside an existing file.
The tables describe ordinary mode-bit checks, not every possible access rule. The experiments use your own directory without a sticky bit or extra ACL entries. Parent traversal and other restrictions still apply.
Remove search permission from a parent
Section titled “Remove search permission from a parent”Change only the owner’s search bit on gate, then inspect that directory itself:
chmod u-x gatels -ld gatedrw------- 2 learner learner 4096 Nov 14 15:49 gateIts owner field is rw-. Inspecting gate from its accessible parent still works. Predict first: can ls reveal the name while cat fails to reach its contents?
ls gatecat gate/note.txtRun it, then compare with your guess
ls: cannot access 'gate/note.txt': Permission deniednote.txtcat: gate/note.txt: Permission deniedYes. Directory read permission supplies the name; missing search permission blocks the lookup needed to inspect or open it. Keep the diagnostic as part of the result.
Check what this interactive Ubuntu shell runs when you type ls:
type lsls is aliased to `ls --color=auto'The alias adds --color=auto. To choose a color, ls looks up each entry’s details, which requires directory search permission; reading names alone does not require it. B08’s command bypasses the alias, so plain ls prints the name without that lookup error:
command ls gatenote.txtThe file’s mode has not changed. A path lookup checks its directory components before the final file can be opened. Making the final file more permissive would not repair this missing parent search permission.
In words: knowing a name and being allowed to resolve that name are separate permissions. The diagram isolates gate; the earlier ancestors of this practice path also need search permission and remain accessible in this run.
Restore the missing bit and read through the same directory:
chmod u+x gatecat gate/note.txtPath readSearch permission was restored; no bytes or read bits on the note were changed.
learner@rootvolt:~/lab$ cd ~/lab/b11/accesslearner@rootvolt:~/lab/b11/access$ chmod u-x gatelearner@rootvolt:~/lab/b11/access$ ls -ld gatedrw------- 2 learner learner 4096 Nov 14 15:49 gatelearner@rootvolt:~/lab/b11/access$ type lsls is aliased to `ls --color=auto'learner@rootvolt:~/lab/b11/access$ ls gatels: cannot access 'gate/note.txt': Permission denied
note.txtlearner@rootvolt:~/lab/b11/access$ command ls gatenote.txtlearner@rootvolt:~/lab/b11/access$ cat gate/note.txtcat: gate/note.txt: Permission deniedlearner@rootvolt:~/lab/b11/access$ chmod u+x gatelearner@rootvolt:~/lab/b11/access$ cat gate/note.txtPath readlearner@rootvolt:~/lab/b11/access$ Steps (24)
- Type
cd ~/lab/b11/access - Press Enter
- Type
chmod u-x gate - Press Enter
- Type
ls -ld gate - Press Enter
- Output:
drw------- 2 learner learner 4096 Nov 14 15:49 gate - Type
type ls - Press Enter
- Output:
ls is aliased to `ls --color=auto' - Type
ls gate - Press Enter
- Output:
ls: cannot access 'gate/note.txt': Permission denied note.txt - Type
command ls gate - Press Enter
- Output:
note.txt - Type
cat gate/note.txt - Press Enter
- Output:
cat: gate/note.txt: Permission denied - Type
chmod u+x gate - Press Enter
- Type
cat gate/note.txt - Press Enter
- Output:
Path read
Search a known name without listing names
Section titled “Search a known name without listing names”Try the opposite separation. Remove directory read permission while leaving search permission available:
chmod u-r gatels -1 gatels: cannot open directory 'gate': Permission deniedcat gate/note.txtPath readListing is denied, but reading the known pathname succeeds. Directory x allows the lookup; file r allows the read. Search permission does not reveal every name for you. Restore the listing permission before continuing:
chmod u+r gateSeparate a file’s bytes from its name
Section titled “Separate a file’s bytes from its name”Prepare a file whose owner can read but cannot write:
printf 'Keep the contents\n' > locked.txtchmod 400 locked.txtPredict first: if replacing the contents fails, must renaming the file also fail?
printf 'Replacement\n' > locked.txtcat locked.txtmv locked.txt renamed.txtls -l renamed.txtRun it, then compare with your guess
bash: locked.txt: Permission deniedKeep the contents-r-------- 1 learner learner 18 Nov 14 15:49 renamed.txtNo. Bash cannot open the file for writing, so the original text remains. mv succeeds without output because this rename modifies entries in the writable, searchable access directory. The renamed file still has owner r--. The destination did not already exist in this fresh practice directory.
learner@rootvolt:~/lab/b11/access$ chmod 400 locked.txtlearner@rootvolt:~/lab/b11/access$ printf 'Replacement\n' > locked.txtbash: locked.txt: Permission deniedlearner@rootvolt:~/lab/b11/access$ cat locked.txtKeep the contentslearner@rootvolt:~/lab/b11/access$ mv locked.txt renamed.txtlearner@rootvolt:~/lab/b11/access$ ls -l renamed.txt-r-------- 1 learner learner 18 Nov 14 15:49 renamed.txtlearner@rootvolt:~/lab/b11/access$ Steps (13)
- Type
chmod 400 locked.txt - Press Enter
- Type
printf 'Replacement\n' > locked.txt - Press Enter
- Output:
bash: locked.txt: Permission denied - Type
cat locked.txt - Press Enter
- Output:
Keep the contents - Type
mv locked.txt renamed.txt - Press Enter
- Type
ls -l renamed.txt - Press Enter
- Output:
-r-------- 1 learner learner 18 Nov 14 15:49 renamed.txt
Restore file write permission and verify an actual content change:
chmod u+w renamed.txtprintf 'Updated contents\n' > renamed.txtcat renamed.txtUpdated contentsThen remove directory write permission, keeping its read and search permissions:
chmod u-w gatetouch gate/extra.txttouch: cannot touch 'gate/extra.txt': Permission deniedThere is no existing extra.txt in this run. Creating its entry requires directory write permission as well as search permission. Restore it and retry:
chmod u+w gatetouch gate/extra.txtls -A gateextra.txt note.txtMistakes to recognize
Section titled “Mistakes to recognize”Changing the file when its parent blocks the path. Compare the successful read before the gate change with the failed read after it. Diagnose the changed parent rather than broadening file access.
Treating read-only as protection against every name change. The rename above preserves the file’s read-only mode. Ordinary removal also modifies directory entries; special restrictions such as sticky directories are covered later.
Assuming Permission denied identifies a single missing bit. The message names the failed operation’s target. Use the operation and the path components to decide which permissions to inspect.
Try it yourself
Section titled “Try it yourself”Goal: Keep gate fully accessible to its owner, but restrict the note to owner read and write only. Inspect the result and explain why this does not remove or rename its directory entry.
Hint
The setup’s numeric mode 644 gave read access beyond the owner. Mode 600 keeps only owner read/write. Inspect the final file directly.
Solution
chmod 600 gate/note.txtls -l gate/note.txt-rw------- 1 learner learner 10 Nov 14 15:49 gate/note.txtThe owner field is rw-; group and other are ---. This changes the file’s access bits. Its name remains in gate, whose directory-entry permissions were restored separately.
Reset this lesson
Section titled “Reset this lesson”Finish the restoration commands above, then inspect the disposable directory:
cd ~/lab/b11ls -AR accessrm -r ~/lab/b11/accessRerun Before you start. Skip removal if the directory is absent. The capture verifies removal and setup recreation after the completed experiments.
- File permissions control contents and execution attempts; directory permissions control names and path lookup.
- Directory
rlists names;xresolves known names and permits traversal. - Creating, removing, or renaming entries normally requires directory write and search permissions.
- A readable file can remain unreachable when an ancestor lacks search permission.
Next, Changing ownership and permissions changes modes and owning identities, then tests access as the intended accounts.
Further reading
Section titled “Further reading”- On your system:
man chmod,man path_resolution,man rename, andman unlink. - Linux man-pages: Path resolution, rename, and unlink.
- GNU Coreutils: Mode Structure.