Monday, March 25, 2019

Basic Navigation Using the Linux Command Line

Whether you've begun toying with a Raspberry Pi, switched your desktop over to Linux, or are building your first server, the ability to use the Linux command line can be a useful skill.   The first step is learning how to navigate around your computer, which is what this tutorial will teach you.  Welcome to Linux.

This will be the first in a three part series designed to familiarize you with the basics of using the Linux command line.  In this tutorial I will cover the commands pwd, ls, and cd, as well as introduce you to some basic abbreviations.  Let's get started.


The Directory Tree


A directory is the equivalent of what we call "folders" on a Windows system.  The below image is meant to give you a basic idea of how the Linux directory tree is structured.  It starts in the root directory, which is just represented by a slash, and then branches out in a series of nested directories, meaning each directory typically contains a mix of files and additional directories, which in turn contain their own files and directories.

Every system is different, and this image is not meant to be an all-inclusive description of a full directory tree.  It's just a summary to give you the basic idea of how a file system is structured.  In reality every directory in this diagram will contain additional files and directories, but that would just be too much to draw out.



When you first logon to a Linux system, you will be dropped into the home folder of your user.  For example, if you logged onto the system above with the user hiro, your starting location would be /home/hiro/.  You can follow this path on the image above by starting with the first / which represents the root directory, followed by home/, and finally hiro/.

If you ever need to know exactly where you are in the directory tree, you can use the command pwd, which stands for print working directory.  pwd can be used with no options or arguments, and will display your current location, as shown in the example below.



You can also determine your current location by looking at the command prompt, which is typically in the following format:

<user>@<hostname>:/path-to/current/directory $

If you apply this to the image above, you will see that the user is hiro, the hostname of the system is metaverse, but instead of a path to the current directory, it only shows a ~ (known as a tilde).  This is because, in Linux, the tilde is an abbreviation for the home folder of the current user, so in this case, ~ is equivalent to /home/hiro.

The other command I will cover in this section is ls, which stands for list.  Using ls by itself will list all the non-hidden files and directories located in the current working directory.  So, as the image below will show, if we type ls while we are located in /home/hiro, our terminal will list the one file and three directories contained in our home directory.




How to Change Directories



So now we know where we are and what else is here, let's see how we can move around.  The command cd stands for change directory, and does exactly what you think it does.  Let's say we want to move to the directory /usr/bin, all we need to do is to type cd /usr/local.  In the image below we can tell that we have changed directories because we can see that the prompt has changed.

While we are here we will also try out our commands pwd and ls, just for practice.



Now, let's say we want to get back to our  home directory /home/hiro.  Thanks to some shortcuts there are three ways we can get back there:

cd /home/hiro  - this is the typical way to use cd, utilizing the full path to where we want to go.
cd ~  - as we discussed, ~ is an abbreviation for our home directory, so this will also take us there.
cd  - by itself, cd will always take us back to our home directory.

In the first example above I referred to listing the entire path to the file, starting in the root directory, as the "full path".  I now want to show you how to use some abbreviations that will allow us to refer to the relative path of a file or directory.

As opposed to the "full path", which allows you to navigate to a file or directory regardless of where you are in the directory tree, a "relative path" refers to a file or directory as relative to where you are currently located.  We can do this by using a period to refer to our current location.

For instance, using the directory tree illustrated in the first  image in this tutorial, and assuming that we are in our home directory, both of the following commands would take us to the same place:

cd /home/hiro/scripts/bikes
cd ./scripts/bikes

You can see how this could save us some typing.  When writing scripts or programs it is always better to use the full path to be sure things execute the same no matter where you run them, but when navigating around the file system from the terminal, the relative path is usually the quickest way to get where you are going.

The last abbreviation related to the relative path that I would like to cover in this tutorial is .. (two periods).  Two periods is short hand for the parent directory of your current location - in other words, one step up in the directory tree.

For example, if I was still in the directory /home/hiro/scripts/bikes from the example above, and I wanted to navigate up one level to the /home/hiro/scripts directory, I could use the following command:

cd ..

This can also be used to move sideways.  For example, if I am now in the /home/hiro/scripts directory after the last example, and I wanted to move into the /home/hir/cic directory next to it, I could use the following command:

cd ../cic

The image below is a screen shot of the examples we have just covered in this section.  Pay attention to the prompt to see where we are at in the directory tree.



I know all of this can be a little overwhelming at first glance, but I promise you that with a little practice this will quickly become second nature to you.


So now we know how to show where we are (pwd - print working directory), how to show what is around us (ls - list files and directories), and how to move from place to place (cd - change directory).  We also learned that ~ always  refers to the home directory of the logged-in user, a single period (.) refers to our current working directory, and a double period (..) refers to the parent directory of our current directory.

Before you move on, take some time to navigate around your file system and explore what is there.  Start in your home directory and move up a couple of levels using cd .. until you reach the root directory  Then use ls to see what is there.  Continue exploring by moving down through the directories, listing what is there at each level, and using the relative path to move up and down through the directory tree.  Explore a little.  Have fun!  Below is an example of what I am talking about.



Once you are comfortable navigating around the file system, go ahead and move on to the next tutorial in this series where I will show you how to create, move, copy, and delete files and directories from the Linux command line.

No comments:

Post a Comment