The Linux Command Line: What It Is And How to Use It

Use the Linux command line like a pro

Illustration of a command line in Linux

Getty Images

The Linux command line is a text-based system that passes commands to the computer and returns results to the user. Once upon a time (back in the late nineties) it was next to impossible to use Linux without relying on the command line. It was a necessity. Since then, things have changed considerably. One can use the Linux desktop without ever having to type a single command.

Although that evolution is very helpful in bringing new users into the open source fold, it avoids one inescapable truth: The command line offers a significant amount of power and flexibility to the operating system. This is true for Linux, Windows, and macOS. And even though some commands are incredibly complex, most are not.

Let’s find out what the Linux command line is and how you can make use of it.

What Is the CLI?

A screenshot of a Linux terminal.

CLI stands for Command Line Interface. In other words, it’s the tool you use to run commands. That sounds simple, because it is. The description of what the CLI could end there. There are, however, a few pieces to this puzzle that could use further explanation.

The CLI is actually made up of:

  • The shell - The shell an interpreter that can transform what you type into something usable by the operating system. There are a number of different shells available for Linux, the most common being bash (which stands for Bourne Again Shell). Without the shell, what you type at the command prompt would not be usable by the operating system.
  • The command prompt - This is where you type your commands. Most command prompts will be in the form of USERNAME@HOSTNAME:~$ (Where USERNAME is the username used to log into Linux and HOSTNAME is the hostname of the machine). You type commands after the $.
  • The terminal emulator - This a small window that emulates a Linux terminal. To better understand this, consider that you have two types of Linux machines: One with a GUI desktop and one without. Most often Linux servers do not include a desktop, so when you log into such a machine, you are greeted with a terminal that includes a command prompt and not much more. On a system with a desktop, that terminal has to be emulated. To do that, apps like GNOME Terminal, Konsole, and LXTerminal are used (every Linux desktop distribution includes a terminal emulator). Open the terminal emulator and run your commands at the command prompt, which will be interpreted by the shell.

    Why Use the CLI?

    If you’re using Linux on the desktop, the chances of you needing to use the CLI are not nearly as great as they’d be if you were working on a server. If you’re working on a Linux server, though, you will use the command line. On the desktop, the CLI is (most often) completely optional.

    However, making use of the CLI is an efficient means of handling many tasks. On top of it making your life more efficient, it also brings a level of flexibility to the desktop you won’t find in the GUI tools. Although you’ll find a GUI tool for every single task you need to run on Linux, some of those graphical interfaces don’t cover the entirety of what the CLI option offers. For example: Most Linux desktops use pulseaudio for sound.

    Screenshot of the sound Settings in Elementary OS.

    From the standard sound configuration tool, you can do a number of things. One thing you cannot do, however, is restart the pulseaudio daemon. In some instances (such as when using the Audacity sound recording tool), there are times when pulseaudio needs to be restarted. Instead of rebooting the computer, you could open a terminal emulator and issue the command

    pulseaudio -k
    That’s not something you can do from the desktop settings tool.

    How to Run Commands

    Running commands in Linux is actually really easy. Most commands in Linux are global, which means you can run any command you want (so long as you have permission to do so) from anywhere you want. So if you need to list the contents of a directory, you can open a terminal emulator and issue the command

    Because the command is global, you don’t have to issue the full path for the command
    Screenshot of the ls command.

    Most every command can be run with options, which is what makes the CLI so powerful. The standard format of a command run with options is the command string followed by its options. Sticking with our ls example, let’s say you want to want to view more information about the files and folders within a location. For that you could add the -l option (which is for long list). This new command would be

    ls -l
    The -l option instructs ls to also list information like permissions, owner, group, size, creation date, and name. You could also add hidden files (files that begin with a .) with the -a option with the command
    ls -l -a
    Screenshot of the ls -la command.

    To make that last command more efficient, you can combine options together, as in

    ls -la

    Command Permissions

    There are some commands that standard users can run and some can only be successfully run by users with super user permissions. For example, rebooting a computer requires admin permissions. You can't open a terminal emulator and issue the command

    without being informed you don’t have permission to do so.
    Screenshot of a failed reboot command.

    In order to run a command that requires admin permissions, you’ll have to employ a tool such as sudo. To successfully reboot a Linux computer from the CLI, that command would be

    sudo reboot
    Sudo stands for “super user do” and allows normal users to run command with super user privileges. Always use sudo with caution. For instance, if you were to issue the command
    sudo rm -rf / 
    you would delete everything on your system. Not only would that render your system completely unusable (requiring you reinstall the operating system), but you’d also lose all your data.

    Do not run sudo rm -rf / unless you want to completely erase your entire hard drive, data, operating system, and all.

    That’s how powerful the CLI can be (and why you should run commands with sudo using a good deal of caution).

    Commands Every Beginner Might Want to Know

    There are plenty of commands every beginning Linux user might want to know. These commands aren’t challenging and can make your daily life a bit more efficient.

    To create a new directory (aka folder), you use the mkdir command. For example, if you want to create a new directory, named data, in your home directory, open a terminal emulator and issue the command

    mkdir /home/USER/data
    (where USER is your username). You could make that command easier to type by using the home directory shortcut, ~/ (Which takes the place of /home/USER/). So the new command would be
    mkdir ~/data
    Screenshot of the mkdir command.

    To move into that newly created directory, issue the command cd data. That assumes you are already in your home directory. If you are in any other directory on your system, you could make use of the /home/USER/ shortcut like so

    cd ~/data
    Screenshot of the cd command.

    If you’re not sure which directory you are in, you could issue the

    command (which stands for “print working directory”). This will print out the directory you are currently working within.
    Screenshot of the pwd command.

    If you want to rename a file or directory, you actually use the move command, which is mv. Say you want to rename the newly created data directory to docs. To do that, issue the command

    mv ~/data ~/docs
    In similar fashion, you can use the mv command to also rename files.
    Screenshot of the mv command.

    To delete the ~/docs folder, you use the rm command. However, you couldn’t just run the command

    rm ~/docs
    .Why? Because ~/docs is a directory and we have to instruct the rm command it will be deleting more than just a file. To do that, we use the r option (for “recursive”). That command would be
    rm -r ~/docs
    Screenshot of the rm command.

    Your Journey Awaits

    You are now ready to begin a journey into the world of the Linux command line. You understand the pieces that comprise the CLI, how commands are run, and how to use a few simple commands. This should serve as a solid starting point for further education in the realm of the Linux command line.