Every file in Unix has a set of permissions that decide who can read it, write to it, or run it as a program. It sounds boring, I know, but understanding permissions is what separates someone who just uses the terminal from someone who actually controls it.
Reading permissions with ls -l
Run ls -l and you will see output that starts with a string like -rwxr-xr-x. That string is the permission mask. The first character tells you the type (- for file, d for directory). The next nine characters are three groups of three: owner, group, and others. Each group has r (read), w (write), and x (execute).
$ ls -l myfile.txt
-rw-r--r-- 1 alice developers 1024 Jun 10 14:30 myfile.txt
Try it Yourself โ
Changing permissions with chmod
chmod is the command you use to change permissions. You can use symbolic mode (letters) or numeric mode (numbers). Symbolic mode looks like chmod u+x file.sh โ that means "add execute permission for the user (owner)." The letters are u (user), g (group), o (others), and a (all).
$ chmod u+x script.sh
$ chmod g-w file.txt
$ chmod a+r readme.txt
Numeric mode uses three digits. Read is 4, write is 2, execute is 1. Add them up for each group. So 755 means owner gets 4+2+1=7 (rwx), group gets 4+0+1=5 (r-x), others get 4+0+1=5 (r-x).
$ chmod 755 script.sh
$ chmod 644 file.txt
$ ls -l
-rw-r--r-- 1 alice developers 1024 Jun 10 14:30 file.txt
Try it Yourself โ
Changing ownership with chown
Sometimes you need to change who owns a file. chown lets you change the owner and group. Only the superuser (root) can do this on most systems.
$ sudo chown bob file.txt
$ sudo chown bob:developers file.txt
$ ls -l file.txt
-rw-r--r-- 1 bob developers 1024 Jun 10 14:30 file.txt
Try it Yourself โ