File modes: read, write and execute
Video 112 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:55, with English and Hindi audio.
Every file on Linux carries a short set of rights, called its mode. The mode says who may read the file, who may change it, and who may run it. Once you can read a mode, you can tell why an engineer is refused access to a file, and you can fix it on purpose. This article explains the three rights, the three groups of people they apply to, and how the same rights are written as letters or as numbers.
What you will learn
- There are three rights: read, write and execute. On a folder, execute means enter.
- Each file has three sets of rights, for the owner, the group and others, checked in that order.
- Letters and numbers describe the same rights: read is four, write is two, and execute is one.
- The mode 640 gives the owner read and write, the group read, and others nothing.
- Only the owner, or root, may change a file's mode.
The three rights and what they allow
- Read: see the contents of a file, or list the names in a folder.
- Write: change a file, or add and remove names in a folder.
- Execute: run a file as a program, or enter a folder.
There are only three rights. For a program, execute lets you start it. For a folder, execute lets you go inside and reach the files there.
Three groups of people, checked in order
Each file has three sets of rights. The owner is the one account that owns the file. The group set applies to every member of the file's group. The others set applies to everyone else on the machine.
The rights are checked in order: owner, then group, then others. The set that matches decides. Only the owner, or root, may change a file's mode, so the owner decides who gets in.
Letters or numbers for the same rights
In letters, r means read, w means write, x means execute, and a dash means that right is missing. In numbers, read is four, write is two, and execute is one. Add them up for each set, so read and write together is six.
So the mode 640 means read and write for the owner, read for the group, and nothing for everyone else. In letters, the same mode reads as rw- r-- ---.
Seeing a file's mode with stat
stat -c '%A %a %n' notes.txtThe percent A part prints the letters, and the percent a part prints the number. The name comes last. On a folder, the same letters apply, but execute means enter. Without it, you cannot reach the files inside, even if you can see their names.
Terms
- File mode
- The short set of rights on a file, which says who may read, write or execute it.
- Owner
- The one account that owns a file, and whose set of rights is checked first.
Check yourself
How do you read the mode 640?
The owner reads and writes (6), the group reads (4), and others have no access (0).
What does execute mean on a folder?
It lets you enter the folder. Without it, you cannot reach the files inside, even if you can see their names.
In which order does Linux check the three sets of rights?
Owner first, then group, then others. The first set that matches decides the access.
Previous: Who am I? The id and whoami commands. Next: Changing owners and rights: chown and chmod.