DEV Community

Cover image for Traverse the Linux CLI - Debian distribution
Onyeanuna prince
Onyeanuna prince

Posted on

Traverse the Linux CLI - Debian distribution

A guide to traversing the command line interface of the Linux operating system using the Debian distribution.

Introduction

The command line interface (CLI) is a powerful tool available on every operating system (OS). It offers a wide range of possibilities ranging from creating folders and files, editing files, and carrying out operations as the root user of a computer. Other advantages include resolving basic networking and troubleshooting issues and greater control over the operating system.

Today, the Linux operating system has a wide range of applications. Servers, smartphones (Android), and networking devices such as routers all use the Linux OS. Therefore, using the Linux CLI to interact with the Linux OS is vital.

Debian is a distribution of the Linux OS. It is one of the oldest and most widely used distributions and is known for its stability and security. It is the foundation for other popular distributions such as Ubuntu and Raspbian. It is a widely used choice for web servers and cloud environments.

With the vast capabilities of the Linux OS, it is critical to understand how to manipulate its CLI for effective interaction. This article will explore the Linux command line and the essential commands to use its interface.

Prerequisites

This article is suitable for anyone looking to learn about the Linux CLI, regardless of their experience level. Its beginner-friendly approach makes it accessible to all readers. Even advanced developers can benefit from reviewing the article to refresh their memory on specific concepts. It is beneficial for beginners transitioning to the Linux OS.

Before beginning this article, you must understand the following concepts and tools:

  1. Operating systems and how they work.

  2. Experience using a terminal emulator or command prompt, such as the Windows Command Prompt or macOS Terminal. (Windows command prompt would be used as a reference in this article).

  3. A working installation of a Debian distribution, either on a physical machine or in a virtual environment.

What is the command line?

As the name implies, the command line is an interface that allows you to type in commands. It is a text-based interface where you input a command, the command gets interpreted, and then it is executed by the kernel.

The command line uses the keyboard, not the mouse, unlike the graphical user interface. It has a blinking cursor called a prompt, ready to receive your commands. Once a command is typed into the prompt, the enter key is pressed, and the command gets interpreted.

The command line can be accessed using a terminal in macOS/Linux or the command prompt in Windows. The terminal/command prompt is an application that allows you to interact with the OS through commands from the command line.

In the Windows OS, you can get to the terminal by pressing the Windows key and the letter "R" on your keyboard to open the Run dialog. Then, type the cmd command and press enter.

We have discussed a general overview of the command line. Now let's see what the Linux CLI looks like.

The Linux command line interface

In Linux, a shell interprets commands. The shell is a command line interpreter that reads and executes your commands. There are several shells in Linux. The following is a list containing the various shells available on the Linux OS:

  1. Bourne Again Shell (bash).

  2. C Shell (csh).

  3. The Korn Shell (ksh).

  4. The Z Shell (zsh).

Note: Bash is the default shell in the Debian distribution, as with many other distributions of Linux.

To access the terminal in Linux, you use the shortcut Ctrl+Alt+T.

Before navigating the Linux command line, you must understand how Linux organizes and arranges files. We will discuss this next.

The Linux file system

The file system is the way an operating system arranges or stores files. Linux uses a hierarchical file structure, with the root directory (/) at the top. Below is a representation of the Linux file system.

The Linux file system

Figure 1. The Linux file system.

To see the file system on your Linux machine, you must navigate to the root directory and list all the files. To do this, follow the steps stated below:

  1. Open your terminal, and type the command cd /. This command changes the directory to the root directory.

  2. Type the ls command to see a list of all directories.

Linux file system

Figure 2. Debian Linux distribution file system.

Figure 2, shows all the directories in the Debian Linux OS. Every directory contains files and sub-directories that perform specific functions.

Note: The root directory as seen in figure 2 differs from the root directory (/) at the top of the Linux file system. The root directory is the home directory of the root user.

The Linux prompt

The prompt in the CLI is the blinking cursor where you type commands. In Windows, the prompt contains the (C:) hard disk partition where the OS resides on the PC, the forward-slash (\) representing the root user, and the current working directory.

windows prompt

Figure 3. Windows command line

From figure 3, the C: drive is where the OS currently resides, the forward slash indicates that it is the root user and the "user\com" is the current working directory.

In Linux, the prompt varies. The prompt displays the current user, hostname, and current working directory.

Linux prompt

Figure 4. Linux command line.

From figure 4, the current user is "onyeanuna." The hostname is Kali, and the current working directory is the root directory (/).

The dollar sign ($) in the prompt indicates that the user doesn't have administrative/root access (also known as superuser). If the user had a hashtag symbol (#), it would imply that they can execute programs as the admin/root user.

Note: To grant the current user administrative/root access, we use the sudo su command. To exit, use the exit command. You can observe these changes by taking note of the dollar sign ($) or the hashtag symbol (#) in the prompt.

With our knowledge of how powerful the CLI is and what the terminal, file system, and command prompt are, we want to know how to find our way around the Linux CLI. Let's dive into understanding commands essential for navigating the command line.

Basic Linux commands

Everything done in the CLI is carried out using commands. There are over a hundred (100+) commands in Linux, and knowing them and their respective options and arguments can be difficult.

Some commands help you understand other commands' purpose, usage, and functionality.

Before we begin, let's see Linux's general syntax for input commands.

#The syntax for using any command in Linux
command [argument] [options]
Enter fullscreen mode Exit fullscreen mode

Linux commands come with arguments and options, which provide extended use cases for that command.

Present working directory command

The present working directory, pwd, is one of the most basic commands in the Linux CLI. The command line is a text-based interface; therefore, there is a constant need to know exactly where you are. The pwd command is the exact command that provides you with that information.

Knowing exactly where you are is essential as soon as you open your terminal, as it will determine how you traverse the command line. The pwd command outputs the path of your current working directory. The syntax to use this command is also straightforward.

pwd
Enter fullscreen mode Exit fullscreen mode

The output of this command can be seen below;

Present working directory

Figure 5. Present working directory command in Linux.

The output, as seen in figure 5, is the current path to my working directory.

When you have ascertained your current working directory, you will now decide if you want to remain or move to your desired directory. The change directory command is used to execute this function. Let's talk about it next.

Change directory command

As the name implies, the change directory command cd is used to change the current directory. When you enter the terminal, you will want to move from one directory to another. This command makes such movements possible.

This command is not new for those coming from the Windows OS, and the syntax for using the cd command is as follows;

cd [file path]
Enter fullscreen mode Exit fullscreen mode

You can move to any directory with the correct file path. Let's take an example.

Let's move to the /etc/init.d directory. This directory stores scripts used to start, stop or restart system services in Linux.

We would do this using the command below.

cd /etc/init.d
Enter fullscreen mode Exit fullscreen mode

Change directory

Figure 6. Change directory command.

You can notice the change in the prompt from figure 6 above. Interpreting the new prompt would mean we are inside the init.d directory, which resides in the etc directory. These files are located in the root directory indicated by the slash (/).

Note: You can also move one step above that directory using .. instead of a file path.

We are currently in the /etc/init.d directory, but we cannot see the files or directories it contains. To see these files, we will use the list command. Let's learn about it now.

The list command

The list command, ls, is used to list all the directories/files within a particular directory. When navigating the command line, you would want to know the files in your current working directory, and this command is made for that exact purpose.

Let's now view the files under the /etc/init.d directory. We can do this by using the following syntax;

ls /etc/init.d
Enter fullscreen mode Exit fullscreen mode

list command

Figure 7. The list command.

We can view the content of any file in two (2) methods. We can first navigate to the directory and then list all its files. Alternatively, from our current working directory, we can use the ls command with the exact path of our desired file.

The example above illustrates how to list files using the first method, let's now take another example using the second method. We will list the files inside the /dev directory from the /etc/init.d directory.

list command used from another directory

Figure 8. Application of the list command used from another directory.

From figure 8, we see that from the /etc.init.d directory; we can list the contents of the /dev directory.

Several options and arguments can be attached to the ls command for extended functionalities. The command that correctly defines all these arguments and options and their respective uses is called the man command. Let's talk about that next.

The manual command

The man command stands for manual; as the name implies, it is a manual for commands. The man command can be seen as a textbook that defines a command. This manual (textbook) outlines all the arguments and options as they apply to a particular command.

We will now use the man to see the arguments and options associated with the ls command. The syntax for this action is as follows:

man ls
Enter fullscreen mode Exit fullscreen mode

manual command

Figure 9. The manual command.

Figure 9 shows that the manual pages provide you with the command name, synopsis, and description of the command. In this case, it provides options that can be used with the ls command.

Let us take an example. Let's execute the ls command with the -a option, which lists all files, including hidden files. To do this, we use the following syntax.

ls -a
Enter fullscreen mode Exit fullscreen mode

list command with the  raw `-a` endraw  option

Figure 10. Execution of the list command with the -a option.

Figure 10 shows the difference between using ls alone and attaching the option -a with the command. The second output displayed in figure 10 contains two (2) additional contents which are the. and .. directory.

We can also explore and apply all the other options on the man pages. All options have been adequately explained, making their use relatively easy.

Note: Due to the length of the manual, you may need to move to other pages to view the remaining content. To achieve this, you can use the arrow left ⬅ and right ➡ buttons on your keyboard to move to the previous and next pages, respectively. You can also use the arrow up ⬆ and down ⬇ keys to view other options slowly.

You can exit the manual by pressing your keyboard's 'Q' key.

The manual is very long and can sometimes be overwhelming. There is another command that functions similarly to the man command, but the only difference is that this command provides a summarised version of the man command. Let's dive into the help command.

The help command

The help command works similarly to the man command but it gives a brief description of any command, unlike the man command. Let us see the output of executing the --help command with the ls command.

The syntax to use this command is as follows:

ls --help
Enter fullscreen mode Exit fullscreen mode

The help command

Figure 11. The help command.

Figure 11 shows the output of executing the --help command with ls. The output is shorter than the manual and straight to the point. Accurate descriptions of the options are given here too.

The main difference between the manual and the help command is that the manual can be used as a textbook for the in-depth study of a particular command while help can be used as a reference.

Advanced Linux concepts

Some other concepts are more advanced than the ones we discussed earlier, and they can be beneficial when navigating the Linux CLI.

Piping

This is a technique where the output of a command is passed in as the input of another command. You can put the output of command A (source) as the input of command B (destination).

This can be very efficient as it allows you to write multiple commands into a single-line command, passing data from a command from left to right.

Piping is done using the vertical bar "|" key, also known as the pipe. Let's take an example:

ls -al /etc | grep cron
Enter fullscreen mode Exit fullscreen mode

Interpreting the command above, we are saying the following:

  1. Give a long listing of all the subdirectories in the /etc directory.

  2. Filter the list to only those subdirectories with the keyword "cron" in them.

Application of the piping technique

Figure 12. Application of the piping technique.

From figure 12 above, instead of returning the long list of subdirectories in the /etc directory, we have a small list of subdirectories containing the "cron" keyword.

To appreciate how useful the piping technique is, let's execute the same command, but this time, without the pipe.

ls -al /etc
Enter fullscreen mode Exit fullscreen mode

Listing files without the piping technique

Figure 13. Listing files without the piping technique.

Figure 13 shows how lengthy the output of running our command would have been without the piping technique. This list goes on...

Visual representation of the piping technique

Figure 14. Visual representation of the piping technique.

Note: The grep command used in the example above finds and searches for strings in a text file. That is why it is commonly used with the " | "symbol.

Redirection

The redirection technique is similar to the piping technique. Just as the name implies, it redirects the output of a command into a different location in the file system. You can simply put command A will be executed, and its output will be stored in your desired location.

Redirection is done using the greater than keys, > and >>. With each having the following functionality:

  1. The > symbol replaces the content of the desired location with the output of the executed command

  2. The >> symbol appends the output of the command to the content already present in the desired location.

Let's take an example. We will create a file, add some text to it, then redirect some commands into the file using the > and >> commands.

nano example.txt
Enter fullscreen mode Exit fullscreen mode

The nano command is used to open a text editor. We will now add the following text to the text editor.

This is an example file for testing the redirection command.
Enter fullscreen mode Exit fullscreen mode

Press "ctrl + O" to save and "ctrl + X" to exit nano. We will now test the > command

ls -al > example.txt
Enter fullscreen mode Exit fullscreen mode

From the command above, we are returning a long list and redirecting the output to the example.txt file.

To view the content of our file, we will use the cat command.

cat example.txt
Enter fullscreen mode Exit fullscreen mode

Application of the redirection technique using  raw `>` endraw

Figure 15. Application of the redirection technique using >.

Figure 15 shows that our previous text has been replaced with our ls -al command. This is the effect of the > command. It replaces everything in the selected location. Next, we will look at the >> command.

ls | grep linux >> example.txt
Enter fullscreen mode Exit fullscreen mode

From our command, we get a list of the current directory from which we would filter every subdirectory that contains the string "Linux" and direct this output to the example.txt file.

Application of the redirection technique using  raw `>>` endraw .

Figure 16. Application of the redirection technique using >>.

From figure 16, we can observe that, unlike the > command, the content of the desired location doesn't get replaced, but the output of the ls | grep Linux command gets appended to the end of the example.txt file.

Note: Piping and redirection are practical techniques often used when scripting and automating tasks.

Conclusion

In conclusion, Linux CLI provides robust and flexible functionality for interacting with Linux systems. No matter what level you are at or what experience level you have, learning to navigate and use the CLI can significantly enhance your productivity and ability to work with Linux systems.

Several reasons make the Debian distro an excellent choice for anyone learning and working with Linux, including its stability and security. Following the tips and techniques described in this article, you should now confidently use the Linux CLI on a Debian system.

In addition to what you already know about Linux CLI, there is much more that you can learn and discover. I encourage you to continue learning and exploring and to seek out additional resources if you need them. Once you gain familiarity with the Linux CLI, you'll be able to utilize it effectively.

Thank you for reading this article. I hope you learned something helpful.

Top comments (0)