A comprehensive guide to the umask command, its functionality, and how to effectively set default file permissions.

When creating a file, it is not always necessary to specify permissions right away, as default permissions are assigned. These default permissions can be controlled and modified using the umask command.

To check the current umask value, simply type umask without any arguments. The command will display the current umask value, such as 0022.

What does 0022 mean? This is an octal value that represents the permissions. Another commonly used value is 0002.

To view the umask value in a human-readable notation, use the command umask -S. This will display the permissions in a more understandable format. For example:

u=rwx,g=rx,o=rx

In this case, the user (u), who is the owner of the file, has read, write, and execute permissions. Other users belonging to the same group (g) have read and execute permissions, just like all other users (o).

In the numeric notation, the last three digits are typically changed to modify the permissions. Here is a breakdown of the numbers and their corresponding meanings:

  • 0: read, write, and execute
  • 1: read and write
  • 2: read and execute
  • 3: read only
  • 4: write and execute
  • 5: write only
  • 6: execute only
  • 7: no permissions

It is important to note that this numeric notation differs from the one used in the chmod command.

To set a new value for the umask, use the following command in numeric format:

umask 002

Alternatively, you can change the permission of a specific role by using the following format:

umask g+r

The umask command is compatible with Linux, macOS, Windows Subsystem for Linux (WSL), and any other UNIX environment.

By understanding and effectively using the umask command, you can have greater control over the default permissions of your files.