Navigating Directories

Author

Government Analysis Function and ONS Data Science Campus

The most commonly used commands used in this interface are to navigate through folders to specific locations in your computer’s memory. This is often to get to a file or directory in order to execute a command of some sort.

You will most likely be familiar with how you interact with folders on your computer; for Windows users File Explorer and Mac users Finder. The actions you use to move around in these programs can be replicated using the command line, you move into and out of folders.

In this course we will use “Folder” and “Directory” interchangeably.

1 Paths

The root directory is the highest level directory you can go to in a file system. It is from there that any other file can be located by going deeper and deeper into folders.

Directory diagram

NOTE: In this example the root directory is called “root”, this is not the case in real file systems.

You can imagine file systems as an upside down tree, with the root at the top and branches (representing directories) connected below, each with directories attached to each branch and so on. At each point in the branch there can be files located.

We denote the position of a file in our file system by using it’s “path”. The path is a description of where the file exists, and is also called an address.

The location we are currently in is called the working directory, as it is the place we can work from most easily.

We will discuss two types of paths: absolute and relative.

Absolute paths are the full address of a location in the file system. This means that the address describes how to get from the root directory to the location specified. It lists each directory that you must travel through in order which will get you to the file destination.

Absolute paths are fully descriptive. It doesn’t matter where you are in your file system, you will be able to get to the place you want to go.

Relative paths are a partial address in the file system. Relative paths do not tell you how to get to a location from the root directory, but rather give directions from where you currently are.

Relative paths allow you to get from one location to another without full knowledge of the whole path to the root directory. This means we can from one folder get to another without going via the very top of the inverted tree.

The syntax of path structure is dependent on operating system and language being used. It is good to know both styles.

Paths are written left to right, where the furthest left is either the root directory for absolute paths, or the highest directory travelled through with relative paths.

Generally UNIX-like styles use forward-slashes / between directory names. The root directory is simply given as a single slash /.

The file location of data.csv above would be given as:

/root/Projects/DataGathering/data.csv

In Windows systems a back-slash \ is used to separate directory names that are travelled through. The root directory is the drive that the file system is in. This means that all absolute paths will start with the drive.

The file path would be given as:

\root\Projects\DataGathering\data.csv

NOTE: There are no spaces between or within filenames, this is one of the reasons that naming conventions avoid spaces in programming.

If a file or folder you are using has a space within it you must put the full filepath in quotation marks " " such as “/Name of folder/filename.txt”.

NOTE: in relative file paths we can denote the current directory using ., so a file path starting with the current directory could start with ./ for UNIX-like or .\ for Windows.

2 Changing directory

As mentioned before we can move between directory locations on our system using a range of command line commands. Think about travelling in general, what information do you need to know?

Some key points that are needed include:

  • Where are you currently?
  • What is around you?
  • How do you move from there?

Remember: in a file system we can only move up or down, either out of a folder or into one. The way we travel is dependent on going up to a high enough level that we can then access what we want to go down to.

2.1 Current Directory

Before we move anywhere we should know where we currently are.

NOTE: The below code is a worked example using the file system “root”, you will have the chance to practice the commands during the exercises at the end of this section.

We can get the address of our current location using the command pwd. This stands for present working directory. This will return us the current working directory - our location.

$ pwd
/C/example_folder/greater_depth


My present working directory is within within the C drive (we can think of this as a folder in this case), in a folder called /example_folder/ in a sub folder /greater_depth/.

A drive is a location within a computer capable of storing and reading information. Windows devices allow the user to explicitly choose which drive to store information in.

We can get the address of our current location using the command cd without any additional arguments. This will return us the current working directory - our location.

>cd
C:\example_folder\greater_depth


My present working directory is within theC:\ drive, within a folder called /example_folder/ in a sub folder /greater_depth/.

A drive is a location within a computer capable of storing and reading information. Windows devices allow the user to explicitly choose which drive to store information in.

2.2 Contents of Current Directory

Showing the contents of the directory we are currently in can help:

  • locate files we are searching for.
  • orient where we are in the file system.

We can add different optional parameters in order to display different things in the directory.

In order to list the contents of a directory we use the ls command.

$ ls

Which will return all of the visible files and folders in a directory.

Some items are intentionally hidden in our directories, this is often denoted by a . before the name of the file/folder. For example a .git folder. We can view all items, including those which are hidden by using the following flag.

$ ls -a

Which will return all items in the current directory including hidden ones.

Some other useful flags include:

  • -l which gives the “long” version of the files, including permissions and sizes.
  • -s gives the size of each item.
  • -S sorts the items by size.

In order to list the contents of a directory we use the dir command.

>dir

Which will return all of the files and folders in a directory with information about when each item was last edited, what type it is and any file sizes.

Adding the optional argument /A followed by another letter modifies the command.

For example, to display all the hidden items in a directory type:

>dir /AH

2.3 cd

We can now work out what directory we are currently in, and what items are stored in that directory. We now will want to move between the directories. This is one of the most common operations done in command line.

In both UNIX-like and Windows systems to “change directory” we use the command cd.

The cd command is followed by an argument which denotes were we want to go. This is where our knowledge of paths comes in.

To get anywhere in our file system we can type cd followed by an absolute path, one that contains a full address from the root. No matter where we are in our file system doing this will direct us exactly where we need to go. For example, looking at our example file system from before:

Directory diagram

We are starting in the “root” directory.

$ pwd
/root

If we want to move to the DataGathering directory we would figure out what path we needed to go down:
/root/Projects/DataGathering/
Then type the command:

$ cd /root/Projects/DataGathering/

This will change the location that your command line program displays above/next to the line you type in. We can check that we have changed the current directory by typing:

$ pwd
/root/Projects/DataGathering/

Remember: The “root” directory is the highest one in a file system, the one we are using in this example is imaginary.

Directory diagram for changing directory within unix

We can also move between folders based on the relative path from where we are. To go “up” a file level we use the .. syntax.

Starting in the /DataGathering/ folder to go back up to the /Projects/ folder we would type:

$ cd ..
$ pwd
/root/Projects/

Directory diagram for changing directory within unix

To go “down” a level, into another folder we simply type the folder name as an argument.

$ cd Analysis
$ pwd
/root/Projects/Analysis/

We can add these two things together, by going “up” then “down” files to get anywhere in our file system.

To get from /Projects/ to /root/Documents/ we would write:

$ cd ../Documents
$ pwd
/root/Documents/


Directory diagram for changing directory within unix

To go up two levels we can write ../.. - using the .. synatx twice.


NOTE: Each part of the path is separated by a forward-slash / with no spaces in between.

We are starting in the “root” directory.

Remember: We use cd without any arguments to display the current working directory

>cd
\root

If we want to move to the DataGathering directory we would figure out what path we needed to go down:
\root\Projects\DataGathering
Then type the command:

>cd \root\Projects\DataGathering\

This will change the location that your command line program displays above/next to the line you type in. We can check that we have changed the current directory by typing:

>cd
\root\Projects\DataGathering\

Remember: The “root” directory is the highest one in a file system, the one we are using in this example is imaginary.

Learning academy logo


We can also move between folders based on the relative path from where we are. To go “up” a file level we use the .. syntax.

Starting in the \DataGathering\ folder to go back up to the \Projects\ folder we would type:

>cd ..
>cd
\root\Projects

Learning academy logo

To go “down” a level, into another folder we simply type the folder name as an argument.

>cd Analysis
>dir
\root\Projects\Analysis

We can add these two things together, by going “up” then “down” files to get anywhere in our file system.

To get from \Projects\ to \root\Documents\ we would write:

>cd ..\Documents
>cd
\root\Documents\


Learning academy logo

To go up two levels we can write ..\...


NOTE: Each part of the path is separated by a backwards-slash \ with no spaces in between.

2.4 Changing Drives

We need to sometimes distinguish between a directory and a drive on a computer when using the command line interface.

A drive is a physical partition within a computer capable of storing information. Windows devices allow the user to pick which drive to store information in.

To move between drives in a UNIX-like interface the command line treats the drive as a directory. The methods for changing drives are therefore the same as changing directory.

To go from the root of the computer / to the /D/ drive we write the command:

$ cd D

To move from the /D/ drive to the /C/ drive we would use the command:

$ cd ../C

Windows makes the distinction between drives and directories. We therefore have different methods of moving between them. Starting in the D:\ drive we move to the C:\ drive using:

>C:

Then back to the D:\ drive with:

>D:

3 Viewing Structure

We can now look at what is in each individual directory, and move between them. But that means to find the location of an item, or understand the structure of a folder system we would need to move around the whole system step by step.

Instead, we can use commands that visualise the structure of a directory for us.

In order to produce nice tree structures using a UNIX-like command line interface you need to download a package. We will not be teaching this, as government devices are typically restricted in what they can install, especially at the command line level.

Instead, here are two alternatives which give you insight into the structure of your file system. If you are using a Windows computer, it may be best to use Command Prompt for this.

Option 1

We can use the ls command with a recursive search to show all sub-folders.

$ ls -R
.:
Documents/  Projects/

./Documents:
slides.pptx  Tasks/

./Documents/Tasks:
notes.txt  plan.txt

./Projects:
Analysis/  DataGathering/

./Projects/Analysis:

./Projects/DataGathering:
data.csv

This gives more information about what the contents of each folder is.

Option 2

We can also use the find command, which will recursively search through each sub folder.

$ find
.
./Documents
./Documents/slides.pptx
./Documents/Tasks
./Documents/Tasks/notes.txt
./Documents/Tasks/plan.txt
./Projects
./Projects/Analysis
./Projects/DataGathering
./Projects/DataGathering/data.csv

This lists the path of each item within the current working directory.

In Windows based command line interfaces this is achieved using the tree command. The command returns a visualisation from the current working directory down of each folder, and the contents of each folder, for all folders.

Our example file system produces the following tree diagram with out working directory in the root.

>tree
.
├───Documents
│   └───Tasks
└───Projects
    ├───Analysis
    └───DataGathering

This shows us all the folders in root, and any other folders within them and so on.

If we want to additionally see all the files that are contained within this directory we can add the following flag.

>tree /F
.
├───Documents
│   │   slides.pptx
│   │
│   └───Tasks
│           notes.txt
│           plan.txt
│
└───Projects
    ├───Analysis
    └───DataGathering
            data.csv

4 Exercises

  1. What is your current working directory?

  2. Download the course material to your Downloads folder using File Explorer / Finder, you will need to unzip. For those in the ONS and most other computers, the locations are as follows:

Windows: * The Downloads folder has the following path C:\Users\<your username>\Downloads.

Mac: * The downloads folder in a mac is typically located here: /Users/<your username>/Downloads

Move to the exercises folder and change your working directory to the folder called “file_system”.

Note: You do not need to unzip the files using command line (but this can be done!).

  1. List the contents of the “file_system” folder.

  2. Recursively list / show tree diagram of the “file_system” folder with all sub folders and files.

  3. From “file_system” navigate to the “Documentation” folder. Then to “Code_review”.

  4. From “Code_review” navigate to the “Analysis” then “Plotting” all in one line.

  5. From “Plotting” navigate to the “Data” then “text” folders. Then show all files recursively/with a tree diagram in this folder.

  6. Return to the “file_system” folder from “text” in one line. Check you are in the right current directory.

Question 1

What is your current working directory?

$ pwd

On a Mac this is likely to be /Users/<username>/ On a Windows this is likely to be a drive such as H:\

Question 2

Download the course material to your Downloads folder using File Explorer / Finder, you will need to unzip. For those in the ONS and most other computers, the locations are as follows:

Windows: * The Downloads folder has the following path C:\Users\<your username>\Downloads.

Mac: * The downloads folder in a mac is typically located here: /Users/<your username>/Downloads

Move to the exercises folder and change your working directory to the folder called “file_system”.

Note: You do not need to unzip the files using command line (but this can be done!).

Mac:

$ cd Users/<username>/Downloads/exercises/file_system

Windows:

$ cd /C/Users/<username>/Downloads/file_system/

Question 3

List the contents of the “file_system” folder.

$ ls

Question 4

Recursively list / show tree diagram of the “file_system” folder with all sub folders and files.

$ find 

Or

$ ls -r

Question 5

From “file_system” navigate to the “Documentation” folder. Then to “Code_review”.

$ cd Documentation

$ cd Code_review

Question 6

From “Code_review” navigate to the “Analysis” then “Plotting” all in one line.

$ cd ../../Analysis/Plotting

Question 7

From “Plotting” navigate to the “Data” then “text” folders. Then show all files recursively/with a tree diagram in this folder.

$ cd ../../Data/text

$ find
or
$ ls -r

Question 8

Return to the “file_system” folder from “text” in one line. Check you are in the right current directory.

$ cd ../..

$ pwd

Question 1

What is your current working directory?

>cd

This will likely return a root directory within a drive, such as the H:\ drive.

Question 2

Download the course material to your Downloads folder using File Explorer / Finder, you will need to unzip. For those in the ONS and most other computers, the locations are as follows:

Windows: * The Downloads folder has the following path C:\Users\<your username>\Downloads.

Mac: * The downloads folder in a mac is typically located here: /Users/<your username>/Downloads

Move to the exercises folder and change your working directory to the folder called “file_system”.

Note: You do not need to unzip the files using command line (but this can be done!).


We need to first move to the correct drive to find Downloads.

>C:
>cd Users\<username>\Downloads\file_system\

Question 3

List the contents of the “file_system” folder.

>dir

Question 4

Recursively list / show tree diagram of the “file_system” folder with all sub folders and files.

>tree /F

Question 5

From “file_system” navigate to the “Documentation” folder. Then to “Code_review”.

>cd Documentation

>cd Code_review

Question 6

From “Code_review” navigate to the “Analysis” then “Plotting” all in one line.

>cd ..\..\Analysis\Plotting

Question 7

From “Plotting” navigate to the “Data” then “text” folders. Then show all files recursively/with a tree diagram in this folder.

>cd ..\..\Data\text

>tree /F

Question 8

Return to the “file_system” folder from “text” in one line. Check you are in the right current directory.

>cd ..\..

>cd 

Reuse

Open Government Licence 3.0