Open In App

Shell Scripting – Umask Command

Improve
Improve
Like Article
Like
Save
Share
Report

A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one or more times as per our requirements.

The umask command is used to change the default file permission of newly created files.

The syntax for the Umask command is –

umask [ value ]

The value here is used to specify the file permissions. Like the value of 444  in a Linux file permission means that everyone can only read the file. Now we will subtract this value from 777 to get the umask value.

Note: To know more about this it is recommended to go through the following article – Umask command in Linux with examples. In this article, we are going to discuss Umask in the context of Shell scripting.

Let’s understand using examples –

Script with Umask for changing File Permission

# the owner both read and write, 
# everybody else can only read them
umask 022

# prints the new umask value
umask 

# creates a new file
touch my_gfg_notes.txt 

# shows the newly created files permission
ls -l my_gfg_notes.txt 

Output

0022

-rw-r–r– 1 satyajit satyajit 0 Apr  6 16:04 my_gfg_notes.txt

Below is the terminal shell pictorial depiction after executing the following script – 

 

Here in this example, we have changed the umask value first to 022, which means that the owner can both read and write all newly created files, but everybody else can only read them. After that, we checked whether the umask value is updated properly or not, and then we created a new text file and checked its default permission. The output states the file permission as -rw-r–r–, which indicates the script using umask properly updated the default file permission settings.

Use of umask command from If Statements in a Script 

PER=”OWNER_READ_WRITE”

if [ “$PER” == “OWNER_READ_WRITE” ]; then

umask 077

echo “Files will, by default, be unreadable by anyone else on the system except user”

else

umask 022

echo “The owner can both read and write all newly created files, but everybody else can only read them”

fi

touch my_gfg_notes.txt #creates a new file

ls -l my_gfg_notes.txt #shows the newly created files permission

Output

Files will, by default, be unreadable by anyone else on the system except user

-rw——- 1 satyajit satyajit 0 Apr  6 16:37 my_gfg_notes.txt

Below is the terminal shell pictorial depiction after executing the following script – 

 

Here in this example, we have used if statements to determine the umask value in a shell script. After that like the earlier one, we created a new file and showed its file permissions.

Use of umask command from Switch Statements in a Script 

preference="A"
case "$preference" in

    # case 1
    "A") umask 077
    echo "no permission";;
    
    # case 2
    "B") umask 022
    echo "only owner can write";;
esac

touch my_gfg_notes.txt
ls -l my_gfg_notes.txt

Output

no permission

-rw——- 1 satyajit satyajit 0 Apr  8 11:00 my_gfg_notes.txt

Here in this example, we have used switch statements to determine the umask value in a shell script. After that like the previous ones we created a new file and showed its file permissions. Below is the terminal shell pictorial depiction after executing the following script – 

 


Last Updated : 25 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads