Directories and Files

YouTube walk through video

Acquiring Workshop data

The first step to download the data for the workshop to your home directory.

Changing directories

Before downloading you will need to change the directory to your home directory.

  • cd is the command to Change Directory. It is followed by the directory you want to change to.
  • “~” represents your home directory.

Change directory to your home directory by running the following command in your terminal:

cd ~ 

In Linux the default of cd is to change directory to your home directory. Therefore the following command will do the same as the above.

cd

To determine your current working directory you can either look at the part of the terminal which displays it or you can use the command pwd (print working directory). Enter the following command:

pwd

In this case it will not show a “~” but the full path of your home directory. An example of a home directory is “/pub14/tea/nsc206/”.

  • The first “/” is the root of the system. Every directory, subdirectory, file and program of the machine is within the root.
  • “pub14/”: A directory within the root.
  • “tea/”: A subdirectory of “pub14/” and a sub-subdirectory of the root (“/”).
  • “nsc206”: The home directory of user nsc206. It is a subdirectory of “tea/” which is a subdirectory of “pub14/” which is a subdirectory of the root (“/”).

Downloading and unzip the data

Now that you are in our home directory we can download the data we need. To download data the Linux/bash terminal we can use the wget command.

Run the below to download the zipped directory Linux.zip.

wget https://neof-workshops.github.io/BFX_resources/Linux.zip

Optionally, you can manually download the file and move it into your home directory of the Linux based system you are using.

Check the file was downloaded to your current directory with ls. ls lists the contents of directories and we will go into more detail of it use later.

ls

If you see the file is listed you can then unzip the directory with the unzip command

unzip Linux.zip

Now change directory into the unzipped directory.

cd Linux

Print to screen the path of your current working directory.

pwd

Additionally, it always good to list the contents of the current directory when you move directory.

ls

Directory structure

You can think of the directory structure in two different ways.

The Directory tree

This starts as the root (“/”) which branches out into directories and files. Directories contain files and subdirectories which contain files and subdirectories etc.

Below is an example of visualising the location of the “Linux” directory within the user ncs006’s home directory as a tree. This only includes a subset of directories.

Boxes

Another analogy to the directory structure is boxes and items. In this case there is one large box that contains all the boxes and items, this is the root (“/”). In the root are items and boxes which hold items and boxes etc.

Below is an example of visualising the location of the “Linux” directory within the user ncs006’s home directory as boxes. This only includes a subset of directories.

Paths

On the command line directories and files are referred to by paths e.g. “/pub14/tea/nsc206/Linux/” is a path.

Paths are case sensitive. The path “directory/file.txt” is different than “diRectory/File.txt”.

Spaces should always be avoided in path names. It is highly recommended to use “_” instead. This is because spaces are used to separate options and parameters in commands.

There are multiple ways to refer to a path. The two main ways are through absolute paths and relative paths.

Absolute paths

Absolute paths are paths that start from the root e.g.

  • “/pub14/tea/nsc206/Linux/”
  • “~/Linux/” (In this case ~ is a shortcut which includes the root)
  • “/pub14/tea/nsc206/file.txt”

Relative paths

Relative paths are paths that are relative to another location besides the root e.g.

  • “.” (This means the current working directory).
  • “..” (This refers to one directory up e.g. if the current directory was “/pub14/tea/nsc206/”, the “..” directory would be “/pub14/tea/”).
  • “1_directory/”, would refer to the directory “1_directory/” in your current directory

Change directory examples

Below is a subset of valid methods to change directory into your “Linux/” directory

Note: You will have different file paths in your machine so you will not be able to run the below commands.

Method 1

cd /pub14/tea/nsc206/Linux/

Method 2

cd ~
cd Linux/

Method 3

cd /pub14/tea/nsc206/Linux/1_directory/
cd ..

Method 4

cd /pub14/
cd tea/
cd nsc206/
cd Linux/

Listing Directory content

To list the contents (files and directories) within a directory you can use the ls command.

The output of ls will include file names that are coloured black and directory names that are coloured blue in our VNC terminals.

Before carrying out the below commands make sure you are in your “Linux” directory with the pwd command.

List the contents in your current directory:

ls

List the contents in your home directory:

ls ..

List the contents in the root directory and put each separate file/directory on a separate line. In the below command the -1 is a parameter that indicates there will be only 1 piece of content on each line. Note: -2, -3 etc are not parameters that work with ls.

ls -1 /

Reminder: you can use the man command to look at more options for commands.

ls is my most typed command. I am consistently using it to see what directories and files are in my current directory and other directories. I suggest you do the same.

MCQs: Files & Directories

Please attempt to answer the below Multiple-Choice Questions to reinforce what you have learnt in this chapter.

  1. What command lists the contents of directories?
  1. What command changes directory?
  1. Choose the option that represents the root directory.
  1. Choose the option that represents your home directory.
  1. Choose the option that represents one directory above.
  1. Which path is an absolute path?
  1. Which path is a relative path?