How to calculate linux user umask

Suppose we have umask 0002 for our user default permission.

    $ umask
    0002

Create new directory

    $ mkdir tmpdir

tmpdir directory created with its default permission 775

    $ ls -l
    ..........
    ..........
    drwxrwxr-x  2 harry harry      4096 Oct  8 06:59 tmp
    ..........
    ..........

Create new file

    $ touch tmpfile

tmpfile file created with its default permission 664

    $ ls -l
    -rw-rw-r--  1 harry harry 0 Oct  8 07:06 tmpfile

Now, let’s see how the file permission settings are calculated using boolean expressions with default umask 0002.

For e.g. consider the case where we have default umask value of 0002 – 0000 0000 0000 0010
1’s complement of 0002 – 1111 1111 1111 1101

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

    1111 1111 1111 1101 (1’s complement of 0002)
    0000 0111 0111 0111 (0777, base permission of directory)
    —————-------------- AND
    0000 0111 0111 0101 = 0775

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

    1111 1111 1111 1101 (1’s complement of 0002)
    0000 0110 0110 0110 (0666, base permission of file)
    —————-------------- AND
    0000 0110 0110 0100 = 0664

we can try different combination of umask value, so we can get a clear picture on how umask is applied to determine default permission on newly created files or directories.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *