Essential Guide to Shell Navigation for Beginners

Introduction

You do not have to be a hacker to understand interfaces like this one!

In computer systems, there are two types of user interfaces:
The Graphical User Interface (GUI) and the Command Line Interface (CLI).
Most users are familiar with the GUI but the CLI provides a more powerful and efficient way to interact with the computer.

Graphical User Interface (GUI) vs. Command Line Interface (CLI)

  • In a Graphical User Interface, the user interacts with graphical components of the computer such as icons, buttons, menus etc.

  • A command line user interface is a text-based interface used to interact with the computer, manage files and run programs on the computer.

Your first encounter with a computer was most probably with a graphical user interface (GUI).
In the shell, you make use of the Command Line Interface (CLI) to interact with the computer.

Accessing the Shell:

You can access the shell on a macOS with the terminal and on a Windows computer, you can access the shell with a program called command prompt. Other operating systems also have programs that enable you to access the shell. To unlock the full potential of the CLI, you need to master shell commands and your first step should be learning how to navigate the shell.

Understanding the File System:

You may be familiar with how files are stored in different drives on a Windows machine: Local Disk (C:), DVD RW DRIVE (D:), New Volume (E:)
What this means is that different partitions on your hard disk are created to store these files but in a unix operating system which is similar to macOS, everything is stored inside a root folder which is called root. The root directory is the first directory and every other file folder or subdirectory is contained in the root directory. Unix and Unix-like operating systems do not employ the concept of drive letters.

Take note that a directory is what you call a folder on a Windows machine.

Essential Navigation Commands:

pwd

It stands for print working directory.
The first command you should master to know how to navigate the shell is pwd.
A good analogy to understand pwd is to think of it as a GPS. A GPS can tell you your location anywhere you are in the world while pwd can tell you your location anywhere you are on your computer.
This can come in handy when you do not know the current directory which you’re currently in, you can simply type pwd and it will print the location of your current directory.

Go ahead and type pwd on your terminal and see the output it gives you.
Did you try it? If you did, you should get a result similar to this 👇

[user@mycomputer ~]$ pwd
/home/user

When you open the terminal, by default, the current directory should be the home of the currently logged-in user except if you changed this configuration.

cd

It stands for change directory.
Think of cd as a teleportation device that can take you anywhere in the world. When you use the command cd, you can change your current directory to a different location on your file system. To use the command, type cd followed by the pathname of the location you wish to navigate to

[user@mycomputer ~]$ cd /var/www

ls

It stands for list.
To understand the ls command, think of it as a torchlight that helps you see what’s in a dark room. The ls command helps you see all the files and directories in your present directory.
Go ahead and type the ls command in your current working directory to see all the files present.

Advanced File System Navigation:

Let’s dive deeper into how to navigate the file system. There are two important concepts you need to understand.

  • Absolute pathname: This specifies the location of a directory from the root directory to the target file or directory. The root directory is /

      [user@mycomputer ~]$ cd /usr/john/documents
      [user@mycomputer documents]$ pwd
      /usr/john/documents
    
  • Relative pathname: This specifies the location of a directory relative to the current directory. e.g.

      [user@mycomputer ~]$ cd subdirectory/documents
      [user@mycomputer documents]$ pwd
      /home/user/subdirectory/sub-sub-directory/documents
    

    There are some special notations used as relative pathnames

  • .. means parent directory of the current directory.

      [user@mycomputer ~]$ cd ..
      [user@mycomputer ~]$ pwd
      /home/user
    
  • . means the current directory

      [user@mycomputer ~]$ cd .
      [user@mycomputer ~]$ pwd
      /home/user
    

Advanced ls Commands:

You can write the ls commands with flags.
ls -a
-a stands for all.
This command lists all the hidden files in the current or specified directory. Files that start with . are known as hidden files. You can not view them with the ls command hence, you need to add the -a flag.

[user@mycomputer ~]$ ls -a
.            file1.txt    my_directory
..           file2.txt    .hidden_file

ls -l
-l stands for long format.
This command ls to list all contents of a directory with the following information:

  1. File types and permissions: This displays the read, write and execute permissions for the owner, group and others. Read more on file permissions

  2. Number of hard links: Number of hard links to the file or directory.

  3. Owner: Displays the owner of the file or directory.

  4. Group: Displays the group associated with the file or directory.

  5. File size: Displays the size of the file or directory in bytes. 4096 means an empty directory.

  6. Modification Date and Time: The date and time the file was last modified
    Name: The name of the file or directory.

[user@mycomputer ~]$ ls -l
total 16
-rw-r--r-- 1 user users  200 Sep 16 10:00 file1.txt
-rw-r--r-- 1 user users  300 Sep 16 10:05 file2.txt
drwxr-xr-x 2 user users 4096 Sep 16 10:10 my_directory

ls -n
-n stands for numeric.
This flag tells ls to display user and group IDs as numbers rather than their names.

[user@mycomputer ~]$ ls -n
total 8
drwxr-xr-x 2 1000 1000 4096 Sep 10 14:30 my_directory
-rw-r--r-- 1 1000 1000   32 Sep 10 14:29 file1.txt
-rw-r--r-- 1 1000 1000   32 Sep 10 14:29 file2.txt

You can combine the -a and the -l flag this way ls -aln or ls -lan. The format in which you write it does not matter. This will display all the files (including hidden files) in long format and numerical order.

Practice and Tips

The commands explained above are just a few out of hundreds out there. You do not have to master them all at once but with time and practice, you will gain mastery.
If you come across a command that you are not familiar with, simply type man followed by the command on your terminal.

[user@mycomputer ~]$ man ls

This syntax will display the documentation for the command you specified.

Conclusion

I hope you found this article helpful. If there is any topic or concept that you'd want me to buttress, do not hesitate to leave a comment and I will attend to it ASAP.

12
Subscribe to my newsletter

Read articles from Oluchukwu Ughagwu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Oluchukwu Ughagwu
Oluchukwu Ughagwu