What is umask?
The command umask is used to set the file, directory permissions.

In linux, you can set the file or directory permission for all users by editing /etc/profile (or in /etc/bashrc) file or for specific users by editing their respective ~/.bashrc file.

For files, the default permission settings are 0666 (execute permission is disabled) and for directories it is 0777

To produce symbolic output, you can use umask with -s option
$ umask -s


To return the default permission setting type umask with no options
$ umask
022

Having known the umask value, try creating a directory and a file and check what the file settings are

$ mkdir tempdir1

$ ls -l
drwxr-xr-x 2 root root 4096 2009-06-29 10:42 tempdir1

$ touch tempfile1

$ ls -l
drwxr-xr-x 2 root root 4096 2009-06-29 10:42 tempdir1
-rw-r–r– 1 root root 0 2009-06-29 10:43 tempfile1

Change the umask and again create a directory and a file and check the file permission settings

$ umask 027
$ umask

0027

$ mkdir tempdir2
$ ls -l

total 12
drwxr-x— 2 root root 4096 2009-06-29 10:40 tempdir2

Now the directory tempdir2 has a permission setting of 750

$ touch tempfile2
$ ls -l

drwxr-x— 2 root root 4096 2009-06-29 10:40 tempdir2
-rw-r—– 1 root root 0 2009-06-29 10:40 tempfile2

Now the file tempfile2 has a permission setting of 640

Now, let us see how the file permission settings are calculated using boolean expressions after we have issued a umask with 027.

For the directories, you need to take the 1’s complement of the umask value (027) and perform a logical AND operation with 777.

The umask value of 027 is 0000 0010 0111
The 1’s complement of 027 is 1111 1101 1000

For directories perform logical AND operation with 777 (0111 0111 0111). So

1111 1101 1000 (1’s complement of 027)
0111 0111 0111 (777)
—————-
0111 0101 0000 = 750

For files, perfom logical AND operation with 666 (0110 0110 0110), so

1111 1101 1000 (1’s complement of 027)
0110 0110 0110 (666)
—————
0110 0100 0000 = 640

Try different combinations on files, directories to get a clear picture on how umask is applied on files.