Linux Commands | File Management | chmod Change File/Directory Permissions
Linux/UNIX Permissions
Linux/UNIX permissions are represented with 10 characters.
For example, if you check directory files with the ls -lrt command, you can see output like this.
% ls -lrt
total 8
-rwxrw-r-- 1 kimkc staff 37 5 13 08:40 README
drwxr-xr-x 8 kimkc staff 256 5 13 08:40 java--tutorial
To understand the 10 characters, split them into “1 character / 3 characters / 3 characters / 3 characters”.
- Directory or file indicator (1 character)
- Owner permissions (characters 2-4)
- Group permissions (characters 5-7)
- Other permissions (characters 8-10)
The permission section to check is the part such as drwx at the beginning.
r: read permissionw: write permissionx: execute permission
Using the example again:
- The first character
dmeans directory, and-means file. In the example, one entry is a directory and one entry is a file. - The next three characters, owner permissions
rwx, mean the owner has read, write, and execute permissions. - The next three characters, group permissions
rw-, mean the group has read and write permissions only. - The last three characters, other permissions
--r, mean others have read permission only.
chmod Command
The chmod command changes file permissions. chmod is short for “change mode”.
When r/w/x permissions are represented as numbers, they are 4/2/1. Permissions for each section are granted by simple addition.
7/7/7means owner permissions / group permissions / other permissions. The number7is4+2+1.
For example, running chmod 777 README changes the permissions of the README file to -rwxrwxrwx (777).
Syntax
chmod [option (example: 744)] [file path/name to change]
- Permissions are represented as numbers: read (
4), write (2), and execute (1). - Read and write =
4+2=6, read and execute =4+1=5, write and execute =2+1=3, read, write, and execute =4+2+1=7. - Permissions are set in three sections: me / the group I belong to / other groups.
- If I can read, my group can write, and others can execute, the value is
421.