Skip to content

File versus directory permissions

Module
Users and Basic Permissions
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

Finish Reading permission strings. Keep your B04 lab directory. Work only inside disposable ~/lab/b11/access:

Terminal window
mkdir -p ~/lab/b11/access/gate
cd ~/lab/b11/access
printf 'Path read\n' > gate/note.txt
chmod 644 gate/note.txt
chmod 700 gate

These 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.

First verify that the initial path works:

Terminal window
cat gate/note.txt
Captured output
Path read

The 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.

Change only the owner’s search bit on gate, then inspect that directory itself:

Terminal window
chmod u-x gate
Terminal window
ls -ld gate
Captured output
drw------- 2 learner learner 4096 Nov 14 15:49 gate

Its 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?

Terminal window
ls gate
cat gate/note.txt
Run it, then compare with your guess
Captured output
ls: cannot access 'gate/note.txt': Permission denied
note.txt
Captured output
cat: gate/note.txt: Permission denied

Yes. 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:

Terminal window
type ls
Captured output
ls 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:

Terminal window
command ls gate
Captured output
note.txt

The 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.

A parent can block a readable file Search permission is needed on gate to resolve note.txt. Reading names alone does not grant search permission. gate: owner rw- (no search) Names can still be listed gate/note.txt cannot be reached Restore gate x to reach the file
A parent can block a readable file Search permission is needed on gate to resolve note.txt. Reading names alone does not grant search permission. gate: owner rw- (no search) Names can still be listed gate/note.txt cannot be reached Restore gate x to reach the file
Simplified model. Without owner search permission on gate, listing can reveal note.txt but the path cannot reach the readable file.

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:

Terminal window
chmod u+x gate
Terminal window
cat gate/note.txt
Captured output
Path read

Search permission was restored; no bytes or read bits on the note were changed.

A readable file behind a parent without search permission
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Removing search permission on gate blocks the file path; restoring it makes the same note readable.
Steps (24)
  1. Type cd ~/lab/b11/access
  2. Press Enter
  3. Type chmod u-x gate
  4. Press Enter
  5. Type ls -ld gate
  6. Press Enter
  7. Output: drw------- 2 learner learner 4096 Nov 14 15:49 gate
  8. Type type ls
  9. Press Enter
  10. Output: ls is aliased to `ls --color=auto'
  11. Type ls gate
  12. Press Enter
  13. Output: ls: cannot access 'gate/note.txt': Permission denied note.txt
  14. Type command ls gate
  15. Press Enter
  16. Output: note.txt
  17. Type cat gate/note.txt
  18. Press Enter
  19. Output: cat: gate/note.txt: Permission denied
  20. Type chmod u+x gate
  21. Press Enter
  22. Type cat gate/note.txt
  23. Press Enter
  24. Output: Path read

Try the opposite separation. Remove directory read permission while leaving search permission available:

Terminal window
chmod u-r gate
Terminal window
ls -1 gate
Captured output
ls: cannot open directory 'gate': Permission denied
Terminal window
cat gate/note.txt
Captured output
Path read

Listing 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:

Terminal window
chmod u+r gate

Prepare a file whose owner can read but cannot write:

Terminal window
printf 'Keep the contents\n' > locked.txt
chmod 400 locked.txt

Predict first: if replacing the contents fails, must renaming the file also fail?

Terminal window
printf 'Replacement\n' > locked.txt
cat locked.txt
mv locked.txt renamed.txt
ls -l renamed.txt
Run it, then compare with your guess
Captured output
bash: locked.txt: Permission denied
Captured output
Keep the contents
Captured output
-r-------- 1 learner learner 18 Nov 14 15:49 renamed.txt

No. 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.

Renaming a read-only file depends on its 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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. A failed content overwrite leaves the bytes intact, while a directory-entry rename succeeds and preserves the file's mode.
Steps (13)
  1. Type chmod 400 locked.txt
  2. Press Enter
  3. Type printf 'Replacement\n' > locked.txt
  4. Press Enter
  5. Output: bash: locked.txt: Permission denied
  6. Type cat locked.txt
  7. Press Enter
  8. Output: Keep the contents
  9. Type mv locked.txt renamed.txt
  10. Press Enter
  11. Type ls -l renamed.txt
  12. Press Enter
  13. Output: -r-------- 1 learner learner 18 Nov 14 15:49 renamed.txt

Restore file write permission and verify an actual content change:

Terminal window
chmod u+w renamed.txt
printf 'Updated contents\n' > renamed.txt
Terminal window
cat renamed.txt
Captured output
Updated contents

Then remove directory write permission, keeping its read and search permissions:

Terminal window
chmod u-w gate
Terminal window
touch gate/extra.txt
Captured output
touch: cannot touch 'gate/extra.txt': Permission denied

There is no existing extra.txt in this run. Creating its entry requires directory write permission as well as search permission. Restore it and retry:

Terminal window
chmod u+w gate
touch gate/extra.txt
Terminal window
ls -A gate
Captured output
extra.txt note.txt

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.

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
Terminal window
chmod 600 gate/note.txt
Terminal window
ls -l gate/note.txt
Captured output
-rw------- 1 learner learner 10 Nov 14 15:49 gate/note.txt

The 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.

Finish the restoration commands above, then inspect the disposable directory:

Terminal window
cd ~/lab/b11
ls -AR access
Terminal window
rm -r ~/lab/b11/access

Rerun 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 r lists names; x resolves 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.