Unix file permissions and chmod explained in 5 minutes
, 3 min read
In Unix-based systems, files and directories have access permissions.
If you open a terminal and run:
ls -l
You will get something like: !file-permissions-explained
-
drwx------
: file type and permissions.d
indicates that it is a directory. The following nine characters represent the permissions for the file or directory. In this example,rwx------
indicates that the owner of the directory (aristot
) has read, write, and execute permissions, while others have no permissions. -
3
: number of links to the file or directory. In this case, the directoryApplications
has three links associated with it. -
aristot
: owner of the file or directory. -
staff
: group associated with the file or directory. -
96
: file size in bytes. For directories, the size generally represents the amount of disk space used by the directory’s metadata. -
Feb 15 10:20
: date and time of the last modification to the file or directory. -
Applications
: name of the file or directory.
More about file permissions permissions:
d
: directory. Files don’t have this.r
: readw
: writex
: execute
The reason there are 9 characters apart from d
, is that each file/directory has permissions on three levels:
user
: file ownergroup
: members of file’s groupothers
: everyone else
Examples:
rwx------
: file owner can read, write and execute the file, and everyone else can do nothing.r-xr-x--x
: file owner and group can read and execute. Everyone else can only execute.
Each permission is associated with a number: r
: 4, w
: 2, x
: 1. This results in a unique sum for each permission set:
rwx
: 4+2+1 = 7rw-
: 4+2 = 6r-x
: 4+1 = 5-wx
: 2+1 = 3
chmod
is just a command-line tool that allows you to change permissions:
chmod 755 /some/file
sets the permissions torwx
for owner andr-x
for group and others.chmod +x /some/file
adds execute permission to all.chmod -w /some/file
removes write permission from all.