#1 How to navigate in the command-line

beginner

Introduction

In this first episode we will give a quick introduction to the command line and how you can use a few commands to navigate around the filesystem. This would be similar to what one do in File Explorer for those coming from Windows or Finder for those coming from Mac.

Outcomes

The command prompt

After opening a terminal you will be presented with the "command prompt" sometimes simply just referred to as the "prompt". It will look similar to something to the example below:

What a typical command prompt will look like

phil@shellcasts:~$ █

The prompt can be broken down into 7 different parts:

Where are you?

For those coming from a GUIs you may find an address bar showing your current location such as C:\Users\phil\Documents. To acheive the same result from the command line you would enter the command pwd followed by the return key to execute the command. pwd is short for "present working directory".

Example of how find out the present working directory

phil@shellcasts:~$ pwd
/home/phil

If you are not sure where you've ended up on the filesystem, run this command at anytime to find your feet again.

Displaying the contents of the current directory

Now that you know what directory you are in, you may want to know the contents of the current directory. The command to run to find this out is ls which is short for "list structure".

Example displaying files and folders in the present directory

phil@shellcasts:~$ ls
personal  todo.csv  work

Depending on how your configuration you might see directories as a different colour compared to files, if not, everything may all just be one colour.

Changing directories

For the purposes of this exercise, let's imagine the following directory structure:

Visualisation of the directory structure used for the purposes of the examples

work
├── project1
│   ├── file1.txt
│   └── file2.txt
└── project2
    ├── file3.txt
    └── file4.txt

When using a GUI file explorer changing directories is trivial and typically accomplished by double clicking on any existing directory name. Unfortunately we do not have this luxury on the command line. To change directory we use the command cd, short for change directory. So if we want to navigate into the "work" directory we run the following command cd work. To confirm where we are will run pwd. Below is the full example:

Example of traversing down into a named directory

phil@shellcasts:~$ pwd
/home/phil
phil@shellcasts:~$ cd work
phil@shellcasts:~/work$ pwd
/home/phil/work
phil@shellcasts:~/work$ cd project1
phil@shellcasts:~/work/project1$ pwd
/home/phil/work/project1

The above works when we want to traverse down into directories and their sub-directories. But how do we traverse back up the directory tree structure? This can be done with the following command cd ... If you wanted to traverse up two directories you would run cd ../... And this pattern repeats depending on the number of directories you wish to go up by.

Example of traversing up a directories

phil@shellcasts:~/work/project1$ pwd
/home/phil/work/project1
phil@shellcasts:~/work/project1$ cd ..
phil@shellcasts:~/work$ pwd
/home/phil/work
phil@shellcasts:~/work$ cd ../..
phil@shellcasts:/home$ pwd
/home

Viewing the contents of a file

At this stage you may want to dive deeper and view the contents of a particular file. This can be done with the cat command follwed by the name of the file. cat is short for "concatenate". This command only works for text based files. So if you run this command for an image or binary file the output maybe a load of garbage. However, don't worry if you do accidentally do this as this is no adverse effect in doing so.

See example below for outputting the contents of file1.txt.

Example displaying the contents of a named text file

phil@shellcasts:~/work/project1$ cat file1.txt
this is line 1 of file1.txt
this is the second line

Creating a new directory

Creating a new empty directory is trivial and can be accomplished with the mkdir command followed by the name of the directory. mkdir is short for "make directory". It is possible to make directories with spaces in the name however I would strongly advise against doing this. Instead you can use an underscore character _ or hyphen character - instead of a space. Below is example of creating a new directory called my-new-folder

Example of making a new named directory

phil@shellcasts:~/work/project1$ mkdir my-new-folder
phil@shellcasts:~/work/project1$ cd my-new-folder/
phil@shellcasts:~/work/project1/my-new-folder$ pwd
/home/phil/work/project1/my-new-folder

Creating an empty file

On occasion you may want to create an empty file for use later. This can be acheived with the touch command followed by the name of the file you want to create. See the example below to create the file my-new-file.txt.

Example of making a named empty file

phil@shellcasts:~/work/project1/my-new-folder$ touch my-new-file.txt
phil@shellcasts:~/work/project1/my-new-folder$ ls
my-new-file.txt

Conclusion

Hopefully the command line is now a little less intimidating than before. You should now know a handful of basic commands that you can use to move around the filesystem and make some basic changes. There is much more to learn but this introduction will give you some confidence to start using the command line.