HMRC Masterclass
Natural Language Processing
Directories and paths


ILCC, School of Informatics
University of Edinburgh

University of Edinburgh logo
$Id: dirsAndPaths.html,v 1.5 2017/08/03 13:17:46 ht Exp $
Henry S. Thompson
18 Jul 2017

1. Introduction

There are two kinds of ways of looking at the organisation of files on a computer:

When working from the command line and typing commands, whether it's a Windows Command Prompt or a Mac Terminal, we have to work in terms of the directory perspective. The rest of this document introduces the basic concepts and tools necessary to get started using this approach.

You may find it helpful to open a terminal now and follow along with the examples given below. The results shown below assume you're starting with a terminal launched as follows:

[W] Start > Programs > Windows System > Command Prompt
[M] Launchpad > Terminal

2. Where am I?

At any given time, a terminal is thought of as being in a single directory, know as the current or working directory. You can ask what it is this way:

[W] >cd
    C:\Users\ht
[M] $ pwd
    /Users/ht

where cd is short for 'current directory' and pwd is short for 'print working directory', and the output is for a terminal starting in what's known as my home directory (my username is 'ht'). We'll explain the different slashes in the last section below.

Note: Here and below whenever you are asked to type in a terminal, the > or $␣ is not meant to be part of what you type: it's just meant as a representation of the terminal prompt, which usually ends with an angle-bracket on Windows and a dollar-sign and a space on a Mac.

To see a list of the files and directories that are available in the current directory (the equivalent of what you see when you open a folder in a visual environment), the command is:

[W] >dir
    17/07/2017 13:04   <DIR>   .
    17/07/2017 13:04   <DIR>   ..
    17/07/2017 13:04   <DIR>   Contacts
    17/07/2017 13:04   <DIR>   Desktop
    17/07/2017 13:04   <DIR>   Documents
    ...
[M] $ ls
    ...
    Desktop
    Documents
    ...

where dir is short for 'directory' and ls is short for 'list'.

3. Where can I go?

Where the visual approach thinks of one folder being inside another, the directory approach thinks of directories being below one another, in an (upside-down) tree, with children (down) and a parent (up):

Visual approach
Four small file folders with contents
Directory approach
Directory tree corresponding to the folders above

So in order to move around, we can either go down into a child directory, by name:

[W] >cd Documents
[M] $ cd Documents

or up, where by convention the parent of any directory has the shorthand name .. (that's two full stops):

[W] >cd ..
[M] $ cd ..

4. What's my full name?

From within a given directory, you can access local files and directories by name. What about ones that are somewhere else? Thinking about the directory tree again, there are two ways to do this, in terms of a path through the tree:

absolute paths
We can always start from the top and trace a path downwards step by step:
[W] >cd C:\Users\ht\Documents
[M] $ cd /Users/ht/Documents
We've already seen an example of this in the results of the 'Where am I?' example. Windows requires that we start with a volume name (that's the C: part), and uses backslashes to separate the names of the directories we go down through. The Mac doesn't need the volume name, and uses forward slashes.
relative paths
You can also get anywhere from anywhere via upward and downward steps:
[W] >cd ..\Desktop
[M] $ cd ../Desktop
to go up and back down to the Desktop folder (assuming you started in Documents).

5. How does the tree grow?

We can add a new sub-directory as the child of the current directory with the mkdir (short for 'make directory') command:

[W] >mkdir notes
[M] $ mkdir notes

This will add a child directory named 'notes', but you would still need to do cd notes to change down into it.

And, you can prune an empty child directory with rmdir (short for 'remove directory'):

[W] >rmdir notes
[M] $ rmdir notes