Chapter 2: Directories and files

Permissions

face Robert Craven

As well as listing the names of files and directories within a directory, ls provides a lot more information on their properties. Much of this is very useful, so let’s see it.

First, make sure you’re in your home directory. Then type the following:

username@MACHINE:~$ ls -l

This will write lots of information to the terminal—one line per file or directory. You might see something like:

total 0
drwx------ 2 username mai 177 Sep 29 12:34 Desktop
drwx------ 2 username mai  10 Sep 29 12:34 Documents
drwx------ 2 username mai  10 Sep 29 12:34 Downloads
drwx------ 2 username mai  10 Sep 29 12:34 Music
drwx------ 2 username mai  10 Sep 29 12:34 Pictures
drwx------ 2 username mai  10 Sep 29 12:34 private
drwx------ 2 username mai  10 Sep 29 12:34 Public
drwx--x--x 2 username mai  10 Sep 29 12:34 public_html
drwx------ 2 username mai  10 Sep 29 12:34 Templates
drwx------ 2 username mai  10 Sep 29 12:34 Videos

(There may be some helpful colouring to differentiate different sorts of file and directory.)

Let’s take each part of this information in turn, focusing on the line for the directory public_html.

  • drwx--x--x gives the filetype and access permissions (more details below).
  • 2 is the ‘link count’. You can ignore this.
  • There follows the name of the owner of the file—here, your user name. Then, the group to which that user belongs—this should be mai, mcs, or mres, depending on which degree you’re taking.
  • Next is the size of the file, in Bytes. (Note: the size for a directory is not the aggregate size of the contents of that directory. Instead, it’s the size of the file used to store information about what’s in the directory.)
  • Next is the date the file was last modified—here, September 29th, at 12:34.
  • Finally, the name of the file or directory.

Implicit here is the idea that each registered user on a Linux machine is the member of one or more groups.

Filetype and access permissions

What does the drwx--x--x string mean for the public_html directory?

  • The d signifies that it’s a directory. If it’s a regular file, this would be - instead.
  • There follow three groups of three characters. Here, those are rwx, --x, and --x.
  • The first group gives the permissions the ower of the file/directory has. Here, the owner can read (r), write to (w) or edit, and execute (x) or run the file/directory.
  • The next group of characters gives the permissions anyone else in the group (mai) has for the file/directory. - means there is no permission for the relevant action. So members of mai (except username) cannot read or edit the file, but can execute it.
  • The next group of characters (--x again) gives the permissions any other user registered on the computer has for the file—anyone who’s not username, and who isn’t in the group mai.

Hidden files

As well as the standard files and directories listed by ls, there are often hidden files and directories.

The name of a hidden file starts with a full stop, followed by something other than a full stop.

They’re often configuration files, program files that a user isn’t expected to edit but which a program needs, hidden directories to store these sorts of file, and so on.

Hidden files and directories can be listed by ls using the -a option. Let’s combine this with -l, to get fuller information on them. Make sure you’re in your home directory, then type the following.

username@MACHINE:~$ ls -la

You should see information on hidden (and non-hidden) files and directories.

Exercise 5

Consider the following output of ls -la:

drwxr-x--- 22 lvb  comp   4096 Aug 18 11:24 .
drwxr-xr-x  3 root root   4096 May 19  2022 ..
drwx------ 25 lvb  comp   4096 Jun  6 12:45 .cache
drwxrwxr-x 22 lvb  comp   4096 Apr 28 00:41 compositions
drwx------ 33 lvb  comp   4096 Aug 15 12:53 .config
-rw-------  1 lvb  comp    150 Jun  8  2022 .gitconfig
-rw-r--r--  1 lvb  comp   3244 Jun 13  2023 gross_fuge.txt
-rw-rw-r--  2 lvb  comp   1567 Aug 14 14:17 heiligenstadt.txt
-rwxr-xr--  1 lvb  comp   4555 May 24  2022 play_fuge.py
drwx------  2 lvb  comp   4096 Mar  7 17:05 .ssh
-rw-r--r--  1 lvb  comp      0 May 19  2022 .sudo_as_admin_successful
drwxr-xr-x  9 lvb  comp   4096 Aug 14 08:53 tmp

Let the user wam be a member of the same groups as lvb.

  1. Which group does the user lvb belong to?
  2. Is .gitconfig a file or directory?
  3. Can lvb execute the program .gitconfig?
  4. Can wam write to the file .sudo_as_admin_successful?