TrumanWong

umask

Displays or sets the permission mask for creating files.

Summary

umask [-p] [-S] [mode]

The main purpose

  • Displays the current file permission mask.
  • Set the permission mask of the created file in octal number.
  • Set the permission mask for creating files through a combination of symbols.

Parameters

mode (optional): octal number or combination of symbols.

Options

-p: Specify this option when there are no parameters, and the output format generated by execution can be reused as input;
-S: Output the permission mask of the created file in the form of a combination of symbols. If this option is not used, it will be output in the form of an octal number.

return value

The return status is success unless illegal options or illegal parameters are given.

example

*The following examples assume that the file permission mask is 0022. *

# Output the permission mask of the created file in the form of octal number.
umask -p
# Results of the:
umask 0022
# Output the permission mask of the created file as a combination of symbols.
umask -S
# Results of the:
u=rwx,g=rx,o=rx

Refer to the DESCRIPTION paragraph of the man chmod document to learn:

  • The u symbol represents the current user.
  • The g symbol represents users who are in the same group as the current user, hereafter referred to as group users.
  • The o symbol represents other users.
  • The a symbol represents all users.
  • The r symbol represents read permission and the octal number 4.
  • The w symbol represents write permission and the octal number 2.
  • The x symbol represents execute permission and the octal number 1.
  • The + symbol represents adding the corresponding permissions of the target user.
  • The - symbol represents deleting the corresponding permissions of the target user.
  • The = symbol represents adding the corresponding permissions of the target user and deleting the permissions not mentioned.

Then the result u=rwx,g=rx,o=rx just output in symbolic form is converted into an octal number equal to 0755;

To set the same permissions using octal numbers, umask requires the additional subtraction 0777 - 0755 or 0022 , while chmod does not.

Add, delete, and assign permissions for symbol combination mode.

#Add permissions:
# Add write permissions to group users.
umask g+w
# Delete permissions:
# Delete other users’ write and execution permissions
umask o-wx
#Assign permissions:
# Assign all permissions to all users, equivalent to umask u=rwx,g=rwx,o=rwx
umask a=rwx
# Clear the read, write, and execute permissions of other users.
umask o=

Create folders and files (assuming the current directory does not exist)

# Create a file
touch test.sh
# Check the permissions and find that the execution permission setting does not work.
stat test.sh
#Create folder
touch newdir
# Check the permissions and find that the execution permission settings can work.
stat newdir

Notice

  1. This command is a built-in bash command. For related help information, please see the help command.

  2. chmod is used to change the permissions of existing objects, and umask affects the permissions of new objects later.

  3. Please use this command with caution, especially do not cancel the read permission of the current user. This will cause an error when you use the TAB key to complete in the terminal.