Bash File Permissions: Exercises, Solutions, and Explanations
1.
Write a Bash script that sets the permissions of a file named "output.txt" to read, write, and execute for the owner, read and write for the group, and read-only for others.
Code:
#!/bin/bash
# Set permissions for output.txt
chmod 764 output.txt
# Check if permissions were set successfully
if [ $? -eq 0 ]; then
echo "Permissions for 'output.txt' set successfully."
else
echo "Failed to set permissions for 'output.txt'."
fi
Output:
Permissions for 'output.txt' set successfully.
Explanation:
In the exercise above,
The script uses the "chmod" command to set the permissions of the file "output.txt" to 764, where:
- 7: Owner has read, write, and execute permissions.
- 6: Group has read and write permissions.
- 4: Others have read-only permission.
2.
Write a Bash script that removes write permissions for the group and others from a file named "input.txt".
Code:
#!/bin/bash
# Remove write permissions for the group and others from input.txt
chmod go-w input.txt
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Write permissions for group and others removed from 'input.txt' successfully."
else
echo "Failed to remove write permissions for group and others from 'input.txt'."
fi
Output:
Write permissions for group and others removed from 'input.txt' successfully.
Explanation:
In the exercise above,
This script uses the "chmod" command to remove write permissions (denoted by w) for the group and others (denoted by go) from the file "input.txt".
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
3.
Write a Bash script that grants execute permissions to all users for a script named "test.sh".
Code:
#!/bin/bash
# Grant execute permissions to all users for test.sh
chmod +x test.sh
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Execute permissions granted to all users for 'test.sh' successfully."
else
echo "Failed to grant execute permissions to all users for 'test.sh'."
fi
Output:
Execute permissions granted to all users for 'test.sh' successfully.
Explanation:
In the exercise above,
The script uses the "chmod" command with the +x option to grant execute permissions to all users for the file "test.sh".
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
4.
Write a Bash script that sets the permissions of a directory named "workarea" to read, write, and execute for the owner, read and execute for the group, and no permissions for others.
Code:
#!/bin/bash
# Set permissions of the directory "workarea"
chmod 750 workarea
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Permissions set successfully for directory 'workarea'."
else
echo "Failed to set permissions for directory 'workarea'."
fi
Output:
Permissions set successfully for directory 'workarea'.
Explanation:
In the exercise above,
The script uses the "chmod" command to set the permission of the directory "workarea".
The permissions 750 mean:
- Read, write, and execute for the owner.
- Read and execute for the group.
- No permission for others.
5.
Write a Bash script that removes all permissions for the group and others from a directory named "parent_directory".
Code:
#!/bin/bash
# Remove permissions for group and others from the directory "parent_directory"
chmod go= parent_directory
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Permissions removed successfully for directory 'parent_directory'."
else
echo "Failed to remove permissions for directory 'parent_directory'."
Fi
Output:
Permissions removed successfully for directory 'parent_directory'.
Explanation:
In the exercise above,
The script uses the "chmod" command to remove permissions for the group and others from the directory "parent_directory".
The go= means:
- g=: Removes all permissions for the group.
- o=: Removes all permissions for others.
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
6.
Write a Bash script that grants read and write permissions to the owner, group, and others for a file named "output.txt".
Code:
#!/bin/bash
# Grant read and write permissions to the owner, group, and others for the file "output.txt"
chmod a+rw output.txt
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Read and write permissions granted successfully for file 'output.txt'."
else
echo "Failed to grant read and write permissions for file 'output.txt'."
fi
Output:
Read and write permissions granted successfully for file 'output.txt'.
Explanation:
In the exercise above,
The script uses the "chmod" command with the a+rw option to grant read and write permissions to the owner (u), group (g), and others (o) for the file "output.txt".
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
7.
Write a Bash script that removes execute permissions for the owner and group from a script named " my_script.sh".
Code:
#!/bin/bash
# Remove execute permissions for the owner and group from the script "my_script.sh"
chmod u-x,g-x my_script.sh
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Execute permissions removed successfully for the owner and group from 'my_script.sh'."
else
echo "Failed to remove execute permissions for the owner and group from 'my_script.sh'."
fi
Output:
Execute permissions removed successfully for the owner and group from 'my_script.sh'.
Explanation:
In the exercise above,
The script uses the "chmod" command with the u-x and g-x options to remove execute permissions for the owner (u) and group (g) from the script "my_script.sh".
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
8.
Write a Bash script that grants read, write, and execute permissions to the owner and group, and read-only permission for others for a directory named "workarea".
Code:
#!/bin/bash
# Grants read, write, and execute permissions to the owner and group,
# and read-only permission for others for the directory "workarea"
chmod 750 workarea
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Permissions modified successfully for 'workarea'."
else
echo "Failed to modify permissions for 'workarea'."
fi
Output:
Permissions modified successfully for 'workarea'.
Explanation:
In the exercise above,
The script uses the "chmod" command with the mode 750 to set the permissions for the directory "workarea". In the mode 750, the owner has read, write, and execute permissions (7), the group has read and execute permissions (5), and others have no permissions (0).
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
9.
Write a Bash script that removes read and write permissions for the group from a file named "input.txt".
Code:
#!/bin/bash
# Remove read and write permissions for the group from the file "input.txt"
chmod g-w input.txt
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Permissions modified successfully for 'input.txt'."
else
echo "Failed to modify permissions for 'input.txt'."
fi
Output:
Permissions modified successfully for 'input.txt'.
Explanation:
In the exercise above,
The script uses the "chmod" command with the g-w option to remove write permissions (w) for the group (g) from the file "input.txt".
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
10.
Write a Bash script that grants read, write, and execute permissions to the owner, and read-only permissions for others for a script named "my_script.sh".
Code:
#!/bin/bash
# Grant read, write, and execute permissions to the owner
chmod u+rwx my_script.sh
# Grant read-only permissions to others
chmod o+r my_script.sh
# Check if permissions were modified successfully
if [ $? -eq 0 ]; then
echo "Permissions modified successfully for 'my_script.sh'."
else
echo "Failed to modify permissions for 'my_script.sh'."
fi
Output:
Permissions modified successfully for 'my_script.sh'.
Explanation:
In the exercise above,
This script first uses the "chmod" command with the u+rwx option to grant read (r), write (w), and execute (x) permissions to the owner (u) for the file "my_script.sh".
Then, it uses the "chmod" command again with the o+r option to grant read-only (r) permissions to others (o) for the same file.
After modifying the permissions, the script checks the exit status of the "chmod" command to determine if the permissions were modified successfully or not and prints an appropriate message accordingly.
Bash Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/bash-script-exercises/changing-file-permissions.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics