Understanding the Linux terminal

You are currently viewing Understanding the Linux terminal

The Linux terminal is one of those tools that makes Linux, Linux. Understanding how commands work is a crucial skill you need to know. You might be new to the Linux terminal or already have some exposure to it. In this post, I’ll describe the Linux terminal and some commands you could start running.   

Comparing a CLI vs. a GUI

You can interact with computers and other devices in several ways, such as through a graphical user interface (GUI) or a command-line interface (CLI). The first (GUI) allows you to interact through icons, menus, buttons, etc. However, a CLI is text-based, and as opposed to a GUI, interaction happens in the form of lines of text.   

Depending on the Linux distribution, some can include a desktop environment (e.g., GNOME, KDE) or not. If it doesn’t have one installed, it probably means there isn’t a graphical user interface, and it only offers a command-line interface. 

Terminal? Shell? Prompt?

The Linux terminal, also called by some shell, prompt and other names, allows you to run programs via text. These programs (a.k.a. commands) enable you to manipulate files, manage rights, configure the system (run updates, install or remove software) and more. However, you might need to run some commands with root (superuser) privileges.

A great benefit of Linux commands is that they are nearly compatible with Linux distributions (e.g., Debian, Ubuntu, CentOS, etc.). You could have some slight differences, but not too big.

Running commands as root: sudo vs. su

As mentioned previously, some commands require elevated privileges before you execute them. You can run them in 2 ways:

a) Running the command with sudo at the start, line by line. This option will provide root access to run a specific program (e.g., sudo apt update OR sudo rm example.txt). In this option, you’ll need to start with “sudo” for each command that requires sudo privileges.

OR

b) Having a superuser prompt. In this case, you’ll see a hashtag (#) which indicates you can run commands as root. You can get into this prompt by running the “su” command (providing the root password) or “sudo su” command (providing your user password).

In this case, you will execute commands with admin rights while in this mode.

How to know if you’re running commands as root or regular user?

From the terminal, you will see characters such as $ or #.

$: Indicates you’re running commands as a regular user.

#: Indicates that you can execute commands as a root user (elevated privileges).

Linux commands

As we have seen, Linux commands are programs that you can run from the command line. You can find many commands, depending on what you want to accomplish: Navigating the filesystem, managing users and groups, performing CRUD operations with files and directories, mounting filesystems, and much more. You can also pipe commands together, where the output of one command can be the input of another.

Some basic Linux commands you can start playing with are:

pwd

The pwd command (short of print working directory) will display the absolute path of the current directory. In order words, this command will print the current directory location starting from the root folder.

Below, you can see the absolute path for user johndoe when the current directory is Desktop.

johndoe@computer:~/Desktop$ pwd
/home/johndoe/Desktop

ls

You can use the ls command to list files and directories. This command (short for list) accepts options, such as displaying hidden files and formatting options.

In the following example, you can see the files and directories when running the ls command in the user’s home directory.

johndoe@computer:~$ ls
Desktop
Documents
Downloads
Pictures

cd

The cd command can help you move between directories. It is useful when you need to move to parent directories, subdirectories, or any other folder you know the absolute path.

Below you can see how the cd command is run to move from the Music directory to the Desktop one, both located in a user’s home directory.

johndoe@computer:~/Music$ cd ~/Desktop
johndoe@computer:~/Desktop$

touch

When creating new files, the touch command can be your friend. It allows you to create single or multiple files from the terminal with a single line of text.

As you can see below, the touch command creates a new txt file called “mynewfile.txt”.

johndoe@computer:~/Desktop$ touch mynewfile.txt

You can also use the touch command to update the timestamps on files. It works the following way: When a file doesn’t exist, touch will create a new one. Otherwise, you will update the file’s timestamp.

mkdir

The mkdir command (short for make directory) can help you create new directories (a.k.a. folders). Sometimes they can help you organize your files better.

You can create one or many directories with a single line of text. In the following example, you can create a new directory called “MyNexDirectory”.

johndoe@computer:~/Desktop$ mkdir MyNewDirectory

cat

Another command you can learn today is the cat command (short for concatenate). This command accepts files as input and prints the file contents in the terminal. You can find it useful for reading log files, for example.

As you can see below, the cat command displays the contents of a file called “numbers.txt”, which displays the numbers from one to ten.

johndoe@computer:~/Desktop$ cat numbers.txt
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

Conclusion

Learning how you can use the Linux terminal and its commands is a great skill you can know. There are so many commands you can practice with. Sometimes using the command line in Linux can be more straightforward. With the terminal, you can even automate tasks or processes to run faster. A CLI uses less computer resources than a GUI, which makes it more efficient.
If you’re new to the Linux terminal, I invite you to practice with some commands and get familiar with them.

Image credits: @Pixabay via Pexels. 

Jose Ponce

Jose is a Linux enthusiast with more than 10 years of Linux experience. His passion for Linux and other Free and Open-Source Software led him to start moreaboutlinux.com. On his free time, he enjoys reading books and drinking coffee.