Navigating Directories – ls, cd

  • ls (list) – Listing every file and folder inside the current working directory
  • cd (change directory) – Changing the directory to work on a different directory or folder

As you know from the previous tutorial, when you open your terminal, it opens on your /home directory. To see what files and folders are stored inside this directory, use the ls command, which stands for list.

up1 1 1

On the left side, I opened the home directory from my file manager. On the right side, my terminal opened at the home directory by default. So when I entered the command ls, bash listed out all the files and folder’s names. As you can see, the terminal should illustrate different types of files and folders with different colors. The colors may differ for you.

Now you want to work on the Documents folder. So you need to navigate into that directory. To get into that folder, run:

$ cd Documents
up2 1 2

Notice both of the images carefully. Make sure you understand what’s shown above. The rest of the tutorial will deal only with the terminal.

Navigating by Absolute Path

As paths are of 2 types, navigating can be done by both types of paths, which are Absolute path and Relative path. When using an absolute path, you need to use the entire path starting from ‘/’. Say from the home directory, you want to go to the Backups directory using an absolute path. Here’s what you can do:

$ cd /home/<user>/Documents/Backups

Now to get back to the home directory using absolute path, you can do this:

$ cd /home/<user> # make sure to change <user> to your user name
up3 4

Navigating by Relative Path

Relative paths can be of several types. One is using some special characters, another one is by truncating the absolute path. Say you’re now in the /home directory. To navigate to the Documents folder, you don’t need to use the absolute path like /home/fahim/Documents. Rather you can just use:

$ cd Documents

Why? Because you’re currently at /home/<user> directory, and by just adding Documents at the end of the current working directory, you can get your desired directory. So you just can truncate the absolute path. A forward slash ‘/’ is automatically added between them. You can use the same technique for longer directories too. For example, to navigate to the Backups directory from the home directory:

$ cd Documents/Backups # which corresponds to cd /home/<user>/Documents/Backups

Here are 3 special characters that can be used as relative paths:

  • ~ : stores the home directory path. So for my case, it’s equal to /home/fahim .
  • . : Single dot, represents the current directory.
  • .. : Double dots, represent one level parent directory.
  • cd : Not among the special characters, but it has a default value, i.e. the home directory.
up4 2

Listing Without Navigating

You’re inside Backups folder. You want to see what scripts are present in the Scripts folder, so that you can retrieve your deleted scripts from Backups. But navigating to Scripts. listing files, then again coming back to Backups seems a little bit pain. So we can list the files in one directory while not changing our working directory. Here’s how.

~/Documents/Backups$ ls ~/Scripts
up5 1

Notice even after listing the files in the Scripts folder, our prompt is still in the previous working directory. You may see in the second command that bash just skipped the command without showing anything. This means I don’t have anything in the Backups folder.

Multiple Commands at Once

If you want to run multiple commands at once, separate them by colon. For example:

$ cd Desktop; ls

Errors and Cautions

  • If you want to navigate to a directory, that doesn’t exist, or can’t be navigated by the path you’ve provided, it’ll show an error saying: Unknown: No such file or directory
  • If you have a file name that includes space, like the Text Files folder in the first picture, then you need to wrap up the folder name by single quotes. Otherwise, this will throw an error. Because in bash, you separate command arguments by space. That’s why you see developers mostly using dash or underscore (-,_) instead of space for longer names.
up6 1

So that’s all for now, keep practicing!

Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others