Chapter 3 – The Linux Command-Line
In this chapter, you will make your first steps in the Linux command-line environment. If you’re a long time Windows user, then this is your chance to explore a new way to interact with your computer and to approach it in a totally different way.
To follow along, you need to have Linux on your machine. If you’re reading this tutorial in the right order, then you should have already installed Linux by now. If not, I invite you to visit the previous chapter to see how you can do that.
Without wasting any more time, let’s go ahead and get started.
Graphical User Interface (GUI) vs Command-Line Interface
Everyone is familiar with the Graphical User Interface (GUI), the interface that allows users to interact with the OS using graphical icons, menus, and windows. GUI is a friendly and colorful environment that is easy to use and interact with.
Now when you launch your Linux machine, the first thing you will see is a friendly GUI. You can see how intuitive and simple it is, and you shouldn’t have a hard time operating it.
You can see that Linux isn’t really as scary as people say. The Graphical User Interface makes it as easy to use as Windows and macOS.
But to be honest, we’re not really here to talk about the graphical interface of Linux. First of all, it isn’t that different from other non-Linux interfaces (Like Windows and macOS), and so, you can explore it yourself by navigating through the menus and applications; Second, the real power of Linux comes from the command-line environment. That’s where the magic happens, and that’s what we’ll be learning in this tutorial.
A command-line interface is a text-based environment that invites you to type in commands which are then interpreted by a program called a shell.
To open a command-line interface in Linux Mint, click on the black screen icon that says Terminal.
Another easier way is to simply press Ctrl+Alt+T.
The Command Prompt
When you open the terminal, a window will appear that looks something like this.
This is how a command-line interface looks like in Linux Mint.
Let’s try and understand what each element in this window means:
- “To run a command as an administrator….“: This is the welcome message that you get every time you open the terminal. You may have a different text than the one that I have, or you may not have any text at all. Note that it is possible to customize this message or change it into a more welcoming one that is more to your taste. But don’t worry about that for the moment, we’ll learn how to do it in a later chapter.
- amine: This is my username. For you, it will be the username that you use when you access your machine.
- patchthenet: This is the name of my machine. You should have here the name that you’ve chosen for your machine when you installed Linux.
- ~: This sign here means that you are currently at your home directory. In my case, this is ‘/home/amine/‘ which is equivalent to “C:\Users\amine” in Windows. We’ll talk more about Linux directories in the next section, for now just remember that every user has their own home directory.
- $: This sign means that you are in a normal user mode as opposed to a superuser (or root) mode. The normal user mode limits you in the commands that you can execute. If you want to have full control, then you need to switch to root. In this case, you would see a dash sign (#) instead of a dollar sign. However, it is not always good to have full privileges, because sometimes you might accidentally damage your system. So, as a safety precaution, make sure to only switch to root when certain commands require it.
- Blinking cursor: This is where we’ll type our commands.
Files and Directories in Linux
Before we type in our first command, we need to learn some concepts about directories and files in a Linux system. We will only discuss here four key concepts that are crucial to understanding the basic commands we’ll be learning in the next section.
In contrast to Windows, where files can be placed in data drives like C:\, D:\, E:\, and so on; in Linux, everything is stored in a tree structure that has the same origin called root “/”. Consequently, every file in Linux has a pathname that starts with /.
Every user in a Linux machine has his own personal folder, which is called the home directory. It is located in “/home/” and has the same name as the username. For instance, in my case, my home directory is “/home/amine/“. When you open a command-line interface, you are by default placed in your home directory, which is denoted by a tilde sign “~”.
Current and Parent Directories
In a Linux pathname, a dot (.) represents the current directory and two dots (..) represent the parent directory. For example, “/home/amine/.” is the same as “/home/amine/“, and “/home/amine/..” is the same as “/home/“.
You will often see files in Linux with no file extension. Linux can determine the type of a file from its header and, therefore, does not need to know its extension.
The Basic Commands
We are now ready to type in our first commands. I invite you to test everything along with me and not limit yourself to reading. Only by getting your hands dirty can you learn and progress.
Print Working Directory
To print your current directory, nothing is more simple, just type in the pwd command, which stands for print working directory.
As you can see, I am in my home directory, which is no surprise to me because I already had the tilde sign (~) in my command prompt.
Now that I know I am in my home directory, I’d like to see what files and folders I have there. To do that, I will use the ls command.
This command lists all the folders and files that are in the current directory. We can get more information from ls by adding additional options. For instance, by using the option -l, you will get a detailed list with some valuable information.
The first column provides information about user permissions. We will talk about this in a later chapter. For now, you only need to know the meaning of the first character: For directories, you will have a ‘d‘ character instead of the hyphen sign ‘–‘ which is used for files only.
We’ll skip the second column for now. The third and fourth columns represent the file owner and the group owner. We’ll talk about these in a later chapter as well.
The fifth column contains the size of the file, and finally, you have the date and time when the file was last updated.
Whenever you need help with a certain command, you can read its manual.
Just type in “man” followed by the command that you wish to learn about to open the manual.
For instance, this is the manual that appears when you type in “man ls”.
The manual contains the name and description of the command, its synopsis (syntax), options that the command supports, and more information that we’ll learn about as we progress in this tutorial.
Navigating within the manual can be tricky if you’ve never used it before. You cannot simply use your mouse to scroll up and down. Once you’re in the manual, you will only rely on your keyboard. Here are the keys that you need to remember :
- j: Move down one line.
- k: Move up one line.
- Space bar: Move down one page.
- b: Move up one page.
- q: Exit.
So far, we’ve been stuck in the same directory, our home sweet home. But honestly, Linux would be quite boring if we couldn’t change our current folder.
Fortunately, it is easy to move around when working in the Linux command-line. To change your current directory, you can simply use the command cd as in “change directory”. For this, type in cd followed by the directory where you wish to go.
To go back one directory upward, just type in “cd ..” (Remember that the two dots “..” refer to the parent directory).
If you run cd without a directory name, then you will go back to your home directory.
The shell will interpret the given directory as the absolute path if it starts with a slash “/”. Otherwise, the path is considered relative to the current directory.
Here are some examples that will make all this clear.
This is it for this chapter. I didn’t want to overwhelm you with commands. The goal was to introduce you to the Linux command-line environment. In the next chapter, we will learn how we can interact with files and directories.
I invite you to have fun exploring the directories and familiarizing yourself with the command-line. If you want to know more about the commands we’ve learned today, you can read the manual.