A directory stores names
A directory is a special kind of file. Its contents are a table that maps each name to an inode, which is the object that holds the data and the mode of a file. The permission bits of the directory do not control the files in it. They control what you may do with the table itself. A file deep in a tree is reached by walking the table one name at a time, and each step needs the right bits on the directory it passes through.
This is the reason directory permissions behave differently from file permissions. A file with mode 000 can still be deleted by its owner, if the directory allows the owner to change the table. A file with a perfect mode can be unreachable, if one directory on its path blocks the walk. When you debug a permission error, check every directory from the top of the path down to the file.
Read means list the names
The read bit on a directory lets you read the table, which means you can list the names it contains. The ls command depends on this bit to print the names. Without read, a plain ls fails with Permission denied, even though the files inside might be perfectly readable.
Read alone is not enough to use the files. Listing a name tells you that a file exists, but it does not let you open it. Opening it requires the execute bit on the directory, which is the next meaning.
Execute means enter the directory
On a directory, the execute bit means search. It lets the kernel look up a name in the table and reach the inode behind it. You need search permission to cd into the directory, to open a file by its path, and to run stat on an entry. A directory with read but without execute lets you see names that you can never reach, which is a common source of confusion.
mkdir -p demo/inner
echo note > demo/inner/file.txt
chmod 644 demo
ls demo
ls -l demoThe directory demo has mode 644, which gives the owner read and write but no execute. The plain ls demo lists the name inner, because reading the table is allowed. The long form ls -l demo fails for the entries, because it must stat each name, and stat needs search permission. You can see the same effect by trying to read the file through the path.
Write means change the names
On a directory, the write bit lets you add entries, remove them, and rename them. Creating a file, deleting a file, and moving one within the tree all need write on the directory that contains the name. The permission bits of the file itself do not decide whether you may delete it. The directory decides.
This rule is the reason a read-only file can still be deleted. The command rm on a file with mode 444 succeeds when the parent directory is writable by you, and it fails when the directory is not. Many people find this surprising the first time, and it is one of the most useful things to know about the system.
Write on a directory lets you remove any name in it, including files that belong to someone else. Give write on a shared directory only when every user should be able to change its contents. The sticky bit, in a later unit, is the usual way to prevent that.
Creating and removing entries
Creating a file in a directory needs write and search on that directory. Creating a subdirectory needs the same, because the new directory is an entry in its parent. The mode of the new object is set by the umask, as the umask unit explains, but whether the create succeeds at all depends only on the bits of the directory.
Removing an entry follows the same rule. You need write and search on the directory that contains the entry. The entry itself can have any mode, and it can belong to another user. When you run rm on a file that you cannot write, an interactive shell may ask for confirmation, but the answer does not change the directory check. The directory decides first, and the file mode comes into play only in the confirmation step.
Renaming works the same way, and so does moving a file between two directories. Moving a file from one directory to another needs write on both directories, because the move removes a name from one table and adds it to the other. This is why a move that works inside a single directory can fail across two directories with different permissions.
Why the parent matters for a path
The kernel checks the search bit on every directory in a path, from the top down. Your permission to read a file therefore depends on the whole path, not only on the file and its parent. A common surprise is a deep file in a directory you own, sitting under a parent that you cannot search. The file is unreachable, and changing the file mode does nothing, because the blocking step is higher in the tree.
When a command fails and the file itself looks fine, run stat on each parent directory in turn. The first directory without search permission for you is the culprit, and that is the directory whose mode, or whose owner, needs to change. This walk up the tree is a short and reliable procedure, and it avoids the trial and error of changing modes at random.
Hands-on: see the three meanings
Make a directory, then remove the owner execute bit and try to reach a file inside it. The lab for this unit does exactly that and checks the final mode. The second step makes a directory read-only and asks you to remove a file from it, which is the case where the directory write bit decides the outcome. Work through both, and notice that each check depends on a different bit.