4. File Structure#

Everything in Unix is a file. Unix is mult-iuser. Consequently, for the purposes of security and operability, files need to have properties that distinguish their use and ownership.

4.1. Exercise#

  • Navigate to your work directory

  • Create a new file (you choose the name) - it can be empty if you want.

  • Use the -l option with the ls command to view the properties of the files in your home directory.

4.2. The Basics#

The output from the last exercise will probably look something like this

ls -l
total 4
-rw-rw-r-- 1 grosed grosed 6 Jan  6 10:34 file-1

Using -l with ls shows the properties of the files listed. Here is an annotated view of this information (from someone else’s account).

Terminal Window

The important information here is the field “-rwxr-xr-x”. Reading from left to right this field is made up of four parts.

The first part is one character (a - in this case). This shows the type. The most common fields are -,d,l,p,c, and b. - means it is a “regular file”, d means it is a directory, l means it is a link. These are the most important ones.

The next group of three characters are the user permissions. If this field has an r, then the owner of the file (you in this case), can read the contents of the file. If it has a w, the owner can modify the file (including deleting it). If it has an x in it, the owner can execute (run) the file.

The next two groups of three characters work the same way as the first three except that they correspond to any user who is in the group that the file belongs to and “any other” users respectively.

For now, only the user field is considered. This is safe because your home directory only allows the owner to modify read, write and execute it, and this is applied recursively to the whole directory structure.

4.2.1. Exercise#

Check the permissions on your home directory.

4.3. Changing File Permissions#

4.3.1. chmod#

File permissions can be changed using the chmod command.

4.3.2. Exercise#

Run the following command and see how the permissions are changed.

chmod +x <your-file-name>

4.3.3. Exercise#

Now try.

chmod -x <your-file-name>

followed by

chmod u+x <yourfilename>

What are the permissions now ?

4.3.4. Exercise#

Use the chmod command to allow members of the owning group to run the file.

4.3.5. Exercise#

Remove write permission for all users on your file. What happens if you now try and delete it ?