Bash - Permissions in Unix Filesystems

Question Click to View Answer

Create the file ~/Desktop/dubstep/djz and show the permissions of the file.

mkdir ~/Desktop/dubstep/
touch ~/Desktop/dubstep/djz
ls -l ~/Desktop/dubstep/djz

The first portion of the long file listing (-rw-r--r-- in this example) displays the file's permissions.

Identify the owner, group, and world permissions for a file with these permissions:

-rw-r--r--

The first character (-) indicates that this is a file, not a directory. The next three characters (rw-) show the file owner's permissions. The next three characters (r--) show the groups permissions. The final three characters (r--) show the world's permissions (everyone's permissions).

Change to the ~/Desktop/dubstep/ directory and create a file called brostep. Describe the owner's permissions for this file and what these mean.

cd ~/Desktop/dubstep/
touch brostep
ls -l brostep

The permissions in the long listing (-rw-r--r--) show that the file owner has the following permissions: rw-. The r means the file owner can read the file, the w means the file owner can write to the file, and the - means the file owner cannot execute the file.

Remove the file owner's read privilege from the ~/Desktop/dubstep/brostep file and do a long listing of the file to demonstrate the privilege has been revoked.

cd ~/Desktop/dubstep/
chmod u-r brostep
ls -l brostep

The file owners permissions now show -w-. This means that the file owner does not have read privileges, does have write privileges, and does not have execute privileges.

Append the text "Who wants to eat a coyote?" to the brostep file. Then use the cat command to read the contents of the file.

echo "Who want's to eat a coyote?" >> brostep
cat brostep # Permission denied error

The file owner is able to write to the brostep file, so appending text is permitted. However, the file owner is not allowed to read from the brostep file and gets a permission denied error.

Change the permission of the ~/Desktop/dubstep/brostep file so the file owner can read the file. Then use the cat command to read the contents of the file.

cd ~/Desktop/dubstep/
chmod u+r brostep
cat brostep

Create the file ~/Desktop/dubstep/lil_script and add the text 'echo hey' to the file. Then execute the lil_script file.

cd ~/Desktop/dubstep/
touch lil_script
echo 'echo hey' >> lil_script
~/Desktop/dubstep/lil_script

Scripts can be executed by providing the absolute pathname to the file. In this example, the file owner does not have the right to execute the lil_script file, so a 'permission denied' error will be raised.

Make the ~/Desktop/dubstep/lil_script file executable by the file owner. Then execute it.

chmod u+x lil_script
~/Desktop/dubstep/lil_script

Make the ~/Desktop/dubstep/songs/ directory. Then remove read, write, and execute permissions from this directory for the file owner.

cd ~/Desktop/dubstep/
mkdir songs/
chmod u-rwx songs/

List all the files in the ~/Desktop/dubstep/songs/ directory.

ls songs/ # permission error

The files in a directory cannot be listed unless the user is permitted to read the directory.

Add read permissions to the ~/Desktop/dubstep/songs/ directory and then show a long listing of the contents in the directory.

chmod u+r songs/
ls -l songs/

The songs/ directory is empty, so nothing is listed, but there is no longer a permission error.

Add the file pulse_x to the ~/Desktop/dubstep/songs/ directory.

touch songs/pulse_x

This results in a permission denied error because the file owner does not have write access to the songs/ directory.

Grant the file owner write and execute access to the ~/Desktop/dubstep/songs/ directory. Then add the file pulse_x to the songs/ directory.

chmod u+wx songs/
touch songs/pulse_x