The ten characters in ls -l
When you run ls -l, each line begins with ten characters. The first character is the file type. A hyphen means a regular file, a letter d means a directory, and an l means a symbolic link. The remaining nine characters are three groups of three, and each group describes one class of user. The first group applies to the owner of the file, the second to the group, and the third to everyone else.
Inside each group, the letters stand for read, write, and execute, in that order. The letter r means read, w means write, and x means execute. A hyphen in the place of a letter means that permission is missing. So a mode such as -rw-r----- says that the owner can read and write, the group can only read, and everyone else has no access at all.
ls -l /workspaceThe listing shows the mode first, then the link count, the owner name, the group name, the size, the modification time, and the name. Only the first ten characters are the permission bits, but the owner and group columns tell you whose bits apply to you. The link count is a different matter and is covered in the filesystem module.
Which class applies to you
The kernel does not combine the three classes. It picks exactly one of them for each access. If your UID matches the file owner, only the owner triplet is checked, even when the group or the other triplet would allow the access. If your UID does not match but one of your group IDs matches the file group, only the group triplet is checked. Otherwise the other triplet decides.
This rule surprises people who expect permissions to add up. Take a file with mode ----rwx that you own. The owner triplet is empty, so you cannot read, write, or run it, even though the other triplet would let everyone do all three. Being the owner does not borrow bits from the other classes. The owner triplet is the only one that applies to you.
Octal notation
The same nine bits can be written as one octal digit per triplet. Read permission is worth 4, write is worth 2, and execute is worth 1. You add the values inside each triplet. So rwx is 7, r-x is 5, r-- is 4, and --- is 0. A mode of rw-r----- becomes 6, 4, 0, which is written as 640.
Octal is shorter and easier to type, which is why it appears in scripts and in documentation. Symbolic notation is easier to read when you change one bit at a time. You will use both forms in the next unit. The important habit is to be able to translate between them in your head, because a command that changes the wrong digit is one of the most common ways to break a file.
Reading stat output
The stat command reports the mode directly, and you choose the format with -c. The %A format gives the symbolic mode, %a gives the octal mode, and %n gives the file name. Using stat is more reliable than counting characters in ls output, especially when you need the octal value for a script.
touch notes.txt
chmod 640 notes.txt
stat -c "%A %a %n" notes.txtThe output is one line with the symbolic mode, the octal mode, and the name. For a file set to 640, the line reads -rw-r----- 640 notes.txt. The command chmod is explained in the next unit, so for now you only need to recognize the result it prints.
When a permission problem puzzles you, print the symbolic mode, the octal mode, and the owner and group. Then ask which triplet the kernel would use for your UID. Nine times out of ten the answer is visible in that output.
Common mistakes when reading modes
The most frequent mistake is reading the owner triplet as if it described your own access. The owner triplet describes what the owner can do, and it applies to you only when your UID matches the owner. Many people look at a file owned by someone else, see that the owner has no write bit, and conclude that nobody can write it. The group or other triplet may still allow the access, and the kernel uses whichever one matches your identity.
A second mistake is confusing the link count with a permission. In ls -l the column after the mode is the number of hard links, and a directory usually shows at least two. That number is not a permission, and it does not change how the kernel checks access. Keep the mode, owner, and group in mind when you read a listing, and treat the other columns as context.
The third mistake is applying file meanings to directories. On a directory, the x letter means search, not run. A directory with mode rw-r--r-- is readable but not searchable, and that combination produces the strange behavior described in the directory unit. Read the letters in the same order for files and directories, but remember what each letter means for each type.
Symbolic links are a special case. On Linux a symbolic link always shows the mode lrwxrwxrwx, and that value is not used for any check. The permissions that matter belong to the target file, so you must read the target with stat to learn what you may do with it.
Practice reading modes on purpose. Create a few files with different modes, and for each one predict who may read and who may write before you check with stat. The prediction takes a few seconds, and it is the fastest way to build the habit that the later units rely on.
Hands-on: read the bits of a file you own
Create a file, set its mode to 640, and read the result with stat. Then read the same file with ls -l and check that the two views agree. The labs for this unit ask you to record the symbolic and octal forms in a file, and to split the mode of a script into its three triplets. Those are the two skills that every later unit depends on.