umask

Displays or sets the permission mask for creating files.

Summary

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

The main purpose

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:

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