Introduction

Today, many of us are familiar with computers (desktops and laptops), smartphones, and tablets which have graphical user interfaces (also referred to as GUIs), allowing us to navigate apps, the web, and our files (like documents and photos) through a visual experience. The Windows, macOS, and Linux operating systems each present varieties of a desktop environment (with images of folders and files, for example), and dropdown menus, all of which provide access to computer programs, applications, and our own media.

Although GUIs can be an intuitive way to use a computer for many users, they often do not provide us with the greatest power over our machines, and they may prevent us from having full administrative access on our computers, including installing, modifying, or deleting software or files. Additionally, as GUIs are largely visual, they are often not as accessible as they could be for all users.

One way of navigating both your own personal computer and remote cloud servers without a GUI is through a text-based terminal or command-line interface (CLI).

Terminal interfaces exist on almost every computer operating system, and terminal emulators are also available as apps for tablets and smartphones. Terminals provide users with greater overall access to their machines through increased administrator access, greater ability to customize environments, and opportunities to automate processes. They also provide users with the ability to access remote computers, such as cloud servers.

This tutorial will provide users who are new to terminal environments with the basics of using a command-line interface through an embedded web terminal in your browser, which you can launch below. If you already have some familiarity with terminals, you may prefer to go through our Introduction to the Linux Terminal tutorial instead. Once you complete this tutorial, you should have an understanding of how to use a terminal on a Linux (or macOS) computer or server.

Understanding the Terminal Window

When you first get access to a new computer or smartphone, you likely want to turn it on and get a feel for how to use it by checking which apps are available, and to learn where things are so that you can customize the device to suit your needs. You can become familiar with a computer through a terminal in a similar way.

The interactive terminal you launched in this browser window, by clicking the Launch an Interactive Terminal! button above, displays a white rectangle on the bottom of your browser window:

In many of these Unix (or *nix-based) operating systems, the symbols at the end of the prompt may be a $ symbol or a # symbol, which mean the following:

  • $ or dollar sign — you are logged in as a regular user
  • # or hashtag/pound symbol — you are logged in as a user with elevated privileges
  • The user that is noted in the # environment is also known as a root user, which is considered to be a super user, or administrator, by default.

    For our purposes within the browser terminal below, you are logged in as a regular user, but you also have administrator privileges via the sudo command. As this is a temporary terminal, you do not need to worry about what you type into the terminal, as we will destroy everything once we are done. Similarly, with a cloud server, it is possible to destroy a server and start fresh if something goes awry.

    At this point, with your terminal launched in the browser, you can begin to type into it using your local computer. Your text will appear at the blinking cursor. We’ll learn about what we can type here in the next sections

    Becoming Familiar with Directories

    We’ll begin working with the terminal by typing a command. A command is an instruction that is given by a user, communicating what it is that the user wants the computer to do. You will be typing your commands into the terminal and then pressing ENTER or RETURN when you are ready for the computer to execute on a given command

    Let’s type the following command followed by ENTER. You can also copy the command, or ask it to run in a launched interactive terminal by clicking on the relevant links in the code block below when you hover over it with a mouse.

    & pwd

    Once you run this command, you’ll receive the following output:

    Output

    /home/sammy

    The pwd command stands for “present working directory,” and it lets you know where you are within the current filesystem.

    In this example, you are in the directory (or folder) called /home/sammy, which stands for the user called sammy. If you are logged in as root, a user with elevated privileges, then the directory would be called /root. On a personal computer, this directory may be called the name of the user who owns the computer. Sammy Shark’s computer may have /sammy or /sammy-shark or /home/sammy as their primary user directory.

    Right now, this directory is empty. Let’s create a directory to store the files we’ll be creating as we go through this tutorial, which we can call files, for example.

    To do this, we’ll use the mkdir command, which stands for “make directory.” After we type the command, we’ll need to write the name of the folder, which will pass the value to the command so that the command can execute on creating this directory. This value (the name of the folder) is known as an argument, which is an input being given to the command. If you are familiar with natural language grammar, you can think of the argument as an object that is being acted upon by the verb of the command.

    In order to create a new directory called files we’ll write the following, with mkdir being the command and files being the argument:

    $ mkdir files

    After you run this command, you won’t receive any output other than a new line with a blinking cursor. With this fresh line on your terminal, you are ready for your next command.

    Listing Contents and Understanding Permissions

    As we have not received any concrete feedback about our new directory yet, we’ll use a command to learn more about what is in our present working directory. You can confirm that the new directory is indeed there by listing out the files in the directory, with the ls command (signifying “list”):

    $ ls

    You’ll receive output that confirms the files directory is there:

    Output

    files

    This gives us general information about what is in our present working directory. If we want to have more details, we can run the ls command with what is called a flag. In Linux commands, a flag is written with a hyphen - and letters, passing additional options (and more arguments) to the command. In our example, we’ll add the -l flag, which — when paired with ls — denotes that we would like to use the option to use a long listing format with our command.

    Let’s type this command and flag, like so:

    $ ls -l

    Upon pressing ENTER, we’ll receive the following output in our terminal:

    Output

    total 4

    drwxr-xr-x 2 sammy sammy 4096 Nov 13 18:06 files

    Here, there are two lines of output. The first line refers to computer memory blocks being allocated to this directory, the second line mostly refers to user permissions on the file.

    To get a somewhat more human readable output, we can also pass the -h or --human-readable flag, which will print memory sizes in a human readable format, as below. Generally, one hyphen - refers to single-letter options, and two hyphens -- refer to options that are written out in words. Note that some options can use both formats. We can build multiple options into a command by chaining flags together, as in -lh.

    For example, the two commands below deliver the same results even though they are written differently:

    $ ls -lh

    $ ls -l --human-readable

    Both of these commands will return the following output, similar to the output above but with greater context of the memory blocks:

    Output

    total 4.0K

    drwxr-xr-x 2 sammy sammy 4.0K Nov 13 18:06 files

    The first line of output lets us know that 4K of computer memory is dedicated to the folder. The second line of output has many more details, which we’ll go over in more detail. A general high-level reference of all the information that we’ll cover is indicated in the table below.

    File type Permissions Link count Owner Group File size Last modified date File name
    d rwxr-xr-x 2 sammy sammy 4.0K Nov 13 18:06 files

    You’ll note that the name of our directory, files, is at the end of the second line of output. This name indicates which specific item in the /home/sammy user directory is being described by the line of output. If we had another file in the directory, we would have another line of output with details on that file.

    At the front of the line, there is a list of characters and dashes. Let’s break down the meaning of each of the characters:

    Character Description
    d directory (or folder) — a type of file that can hold other files, useful for organizing a file system; if this were - instead, this would refer to a non-directory file
    r read — permission to open and read a file, or list the contents of a directory
    w write — permission to modify the content of a file; and to add, remove, rename files in a directory
    x execute — permission to run a file that is a program, or to enter and access files within a directory

    Navigating the Filesystem

    So far, we have learned how to determine where we are in a filesystem, how to make a new directory, how to list out files, and how to determine permissions.

    Let’s next learn how to move around the file system. We have made a new directory, but we are still in the main /home/sammy user directory. In order to move into the /home/sammy/files directory that we have created, we’ll use the cd command and pass the name of the directory we want to move into as the argument. The command cd stands for “change directory,” and we’ll construct it like so:

    $ cd files

    Again, you won’t receive output other than a new line with a blinking cursor, but we can check that we are in the /home/sammy/files directory with the pwd command we used earlier:

    $ pwd

    You’ll get the following output, confirming where you are:

    Output

    /home/sammy/file

    Let’s move to the primary directory of the server. Regardless of where we are in a filesystem, we can always use the command cd / to move to the primary directory:

    $ cd /

    To confirm that we have moved and learn what is in this directory, let’s run our list command:

    $ ls

    We’ll receive the following output:

    Output

    bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run s sbin srv sys tmp usr var

    Creating and Modifying Text Files

    Let’s first be sure that we’re in the files/ directory of the /home/sammy user folder, which we can do by either verifying with pwd, or by changing directories on the absolute path:

    cd /home/sammy/files

    We’ll begin by using the touch command, which can create a new file or modify an existing file. To use it, you can use the command touch and pass the name of the text file you want to create as the argument, as demonstrated below.

    touch ocean.txt

    Once you press ENTER, you’ll receive a new line of the command prompt, and you can list the current contents of files/ to ensure it was created.

    $ ls

    Output

    ocean.txt

    We can use echo directly on the command line to have the interface repeat after us. The traditional first program, "Hello, World!", can be written with echo like so:

    $ echo Hello, World!

    Output

    Hello, World!

    Named for Echo of Ovid’s Metamorphosis, the echo command returns back what we request. In this case, it echoed, “Hello, World!” On its own, however, the echo command does not allow us to store the value of our text into a text file. In order to do that, we will need to type the following:

    $ echo "Sammy the Shark" > sammy.txt

    We can check that our new file exists, again with ls.

    $ ls

    Output

    ocean.txt sammy.txt

    We now have two text files in our /home/sammy/files user folder. Next, we can confirm that the file sammy.txt does have the text we asked the terminal to echo into it. We can do that with the cat command. Short for concatenate, the cat command is very useful for working with files. Among its functions is showing the contents of a file.

    $ cat sammy.txt

    Once we run the command, we’ll receive the following output:

    Output

    Sammy the Shark

    If we were to run cat on the empty file ocean.txt, we would receive nothing in return as there is no text in that file. We can add text to this existing file with echo as well. Let’s add a quote from Zora Neale Hurston to the file.

    $ echo "Some people could look at a mud puddle and see an ocean with ships." > ocean.txt

    Now, if we run cat on the file, we’ll receive output of the text we just entered.

    $ cat ocean.txt

    Output

    Some people could look at a mud puddle and see an ocean with ships.

    Autocompletion and History

    Many versions of the command line, including the interactive terminal embedded in this tutorial, allow you to autocomplete and to reuse commands as you go. This supports you moving more quickly as it saves you typing time

    Try typing cat along with the first few letters of one of the text files you have been working on — for example, cat sa. Before you finish typing the whole file name of sammy.txt, press the TAB key instead. This should autocomplete the full file name, so that your terminal prompt displays the following

    cat sammy.txt

    If you need to replicate all the commands you have done in your terminal, you can also summon the entire history of this session with the aptly named history command:

    $ history

    Depending on how much you have practiced, you should receive 30 or more lines of commands, starting with the following output

    Output

    1 pwd

    2 mkdir files

    3 ls

    4 ls -l

    ...

    Familiarizing yourself with these shortcuts will support you as you become more proficient with the command line interface.

    Working with Files from the Web

    One of the most exciting aspects of working on a command line interface connected to the internet is that you have access to all of the resources on the web, and can act on them in an automated way. With the terminal, you can also directly access cloud servers that you have credentials for, manage and orchestrate cloud infrastructure, build your own web apps, and more. For now, as we have already learned how to work with text files on the terminal, we’ll go over how to pull down a text file from the web so that the machine we are using has that text file available to us

    Let’s move back into the files/ directory:

    $ cd /home/sammy/files

    We have uploaded a short passage from Jules Verne’s Twenty Thousand Leagues Under the Seas on a cloud server. We’ll pass the URL of that file to the curl command, as demonstrated below.

    $ curl https://assets.digitalocean.com/articles/
    command-line-intro/verne_twenty-thousand-leagues.txt

    Once we press ENTER, we’ll receive the text of the passage as output to our terminal (excerpted below)

    Output

    "You like the sea, Captain?" "Yes; I love it! The sea is everything. It covers seven tenths of the terrestrial globe. ... "Captain Nemo," said I to my host, who had just thrown himself on one of the divans, "this is a library which would do honor to more than one of the continental palaces, and I am absolutely astounded when I consider that it can follow you to the bottom of the seas."

    In order to save the text to a file, we’ll need to run curl with the -O flag, which enables us to output the text to a file, taking the same name of the remote file for our local copy.

    $ curl -O https://assets.digitalocean.com/articles/
    command-line-intro/verne_twenty-thousand-leagues.txt

    You’ll receive feedback from the terminal that your file has downloaded