Friday, 1 January 2016

Guide to the Linux Terminal: Introduction and Basic Navigation

For first time users, the Linux terminal can be quite an intimidating aspect of the operating system. Instead of imagining the terminal as an interface to issue commands to a computer, imagine it to be a chat window which can be used to talk to your computer. The only difference here is, your computer is exactly like your annoying friend who cannot tolerate bad grammar. A terminal will only accept commands (messages) which are framed and constructed correctly. Understanding how it works is a lot simpler than it may seem at first. I will be using a terminal in Linux Mint. But, the concepts remain pretty much the same in all Linux distributions. There may be a few variations which I will cover in another guide.

Location and Paths

The moment you open your terminal you'll see a window which looks something like this.

Now the first line in the terminal says: piyush@piyush-laptop ~ $ 

Here, "piyush" stands for the username of the user which is using the terminal window. Since we are using this terminal from the user account that we've logged in with, this is the username it will show you.

The second part, "@piyush-laptop" stands for the name of the computer you are currently using the terminal in. My laptop's computer name is "piyush-laptop". In your case, it will show you whatever you've set your computer name as.

As far as the "~" is concerned, I will get to that part in a bit when I explain paths. The "$" indicates that you are using a normal terminal window which is sending your commands to Linux using what we call a "Bash shell". If this is too technical for you, all you need to understand is that as long as you see a "$" before your text input cursor, you're using a standard Linux terminal.

The concept of paths is very simple. A path is a unique address which points to any file or directory in your computer. For example, if I wanted the path to a file called resume.pdf which is located inside the Documents folder in my default Linux home directory, the path to the file would read something like this:


Suppose I want the path to a video called webinar.mp4 located in the Videos folder of my Linux home directory, the path will be:


In both the above paths, "/home/piyush/" is the path to the default home directory of the current user logged in to the system. Suppose your username is "john". Then your default home directory would be "/home/john/". To make things simpler, Linux abbreviates the path of the current default home directory to "~". So, my webinar.mp4 file can also be referenced by the path:


Now, coming back to the terminal window where we were discussing the "~" icon; this refers to the present working directory of the terminal. So in case you're asking the computer to perform some operations within a particular directory, you need to make sure that your present working directory path is correct.

Navigation and Basic Commands

The key to using the Linux terminal is not remembering commands, but understanding how commands work. Finding a command is only a Google search away. There are a few commands though, which one ought to know when using a terminal. These are usually used for working your way to your required working directory and then performing whatever operations you wish within the directory.

We spoke about the present working directory in the previous section. When we open a new terminal windows, our present working directory is the default home directory referenced by "~". Suppose we want to change our present working directory, we use the command cd (change directory). Using this command is fairly simple. Suppose we want to move inside the Documents folder present inside the home directory, we will type:

cd Documents/

This will change our present working directory to "~/Documents" 

We can use this command to go literally anywhere in the system. We need not necessarily reference a directory within the current working directory. For example, suppose I wanted to navigate to a folder called Work within my Documents folder from a new terminal window, I can simply type:

cd Documents/Work/

This will change my present working directory to "~/Documents/Work". Note that you can jump to any directory in the system using just the required path after cd. You need not hop from directory to directory to get to where you want.

Very often, we don't know the contents of our present working directory. To find out the contents of the directory, we type ls.

In the above Work directory, there are 3 entries visible immediately after issuing the ls command. Here, "reports.txt" and "database_entries.txt" listed in white are files, whereas "Employees" listed in bold blue is a directory.

This should give you a good sense of where you are within your system and what your working directory contain. If you want to go up a single directory, you can type:

cd ..

This should take you up one directory. You may use this command multiple times to go up multiple directories.

Here are two more pointers to get you started with typing commands like a pro:
  1. Typing a filename/foldername/command partially and then pressing Tab will auto complete it for you. So if you want to type cd Documents, you could simply type cd Doc, and then press Tab. This should auto complete the command for you. Note that auto complete only works if the partial content you have typed is enough to uniquely identify a particular command, folder or file. So if you type cd Do and press Tab, it may refer to the Documents folder or the Downloads folder; hence the terminal won't auto complete the command.
  2. Pressing the Up Arrow key will automatically type in the last command that you entered in the terminal. You can press it multiple times to cycle through all the commands you have previously entered in chronological order. You can also press the Down Arrow to cycle back within the list.
This tutorial should be enough to get you started with basic navigation within the Linux terminal. I'll be covering parameters and file and folder operations in the next tutorial, so stay tuned. Please let me know your feedback in the comments section, and I will try to get back to you as soon as possible.