The Command Line Interface

What is an interface?

According to the online Merriam Webster dictionary, an interface is "a system that is used for operating a computer: a system that controls the way information is shown to a computer user and the way the user is able to work with the computer."1 As this definition explains, an interface has two parts: 1) a display of information about what the computer is doing and 2) a method for telling the computer what to do. The command line interface is a very basic interface that uses mostly text for both the display and input components of the interface. In a nutshell, the command line allows for text-based communication with a computer. Let's look at the two parts of the command line interface: the display and the input.

CLI Display

The command line can be the default interface for a computer, but most personal computers use a program (like Terminal) within the desktop graphical interface to provide the command line interface. Let's dissect the CLI's language for a moment. To follow along, log in to your virtual machine or server.

Below is an example of the prompt on an Ubuntu Linux server:

ubuntu@chopin:~$

The above prompt follows this format:

[user]@[hostname]:[current_directory]$

The [user] portion ("ubuntu") represents the current user that is logged in to the command line interface. The [hostname] portion represents the computer's name. Following those two pieces is :[current_directory]. The colon is just for separation, and the [current_directory] displays the path of the directory that you are in. If you've just logged in, it's probably just a tilde (~), which represents the home directory. The last piece is $ (note the trailing space, which refers to the extra space following $). This whole piece of text is called the prompt, or PS1. It can be modified to fit your needs, but usually displays some very basic information that shows you the context of what you're doing. Whenever you log in to a server, or whenever you open up Terminal on your own computer, you'll be presented with a prompt like this one. For the rest of this book, we'll represent the prompt as a simple $.

At the end of the prompt, you should see a cursor which, in the world of the command line, is just a box that blinks on for a second and off for a second. The cursor shows where you are able to input additional text. The thing that allows you to input text is called STDIN (standard input).

The last piece of the display portion of the CLI is the output of your commands. As you can see in the image below, when you type a command, it may have textual output:

$ ls /
bin   home            lib64       opt   sbin  usr
boot  initrd.img      lost+found  proc  srv   var
dev   initrd.img.old  media       root  sys   vmlinuz
etc   lib             mnt         run   tmp   vmlinuz.old

When the output has printed to the screen and the command or program exits, the prompt is displayed again below the output. To review, the components of the CLI display are the prompt, the cursor, the input (text you have typed in), and the output of your commands and programs.

CLI Input

The second component of this textual interface is the input. By typing text into the command line interface, you are creating input for the CLI to interpret and act on. Using commands such as echo or ls, you can command the computer to do your bidding. Let's try a command. Try typing echo "Hello World" into the console:

$ echo "Hello World"

The echo command is very simple, but it can also be very useful. All it does is send text to the CLI's output. Another useful command is the pwd command. If you want to know where you are in your computer's file system, you can type pwd and press enter to execute it. You should get something like the following as output from that command:

$ pwd
/home/ubuntu

The commands above are simple, and don't actually make any changes to your command line environment, the computer, or its files -- they only display some output. But other commands such as rm, cp, and source can directly affect the files on your computer or can change your command line environment.

The Anatomy of a Command

Commands come in all shapes and sizes, but they have many features in common. When you type a command into a terminal, it will always take the following format:

[command] [arguments...]

A command can be the path to a file (e.g. /path/to/file), or it can be a command that your terminal is already aware of (e.g. echo). The second portion of the command usually consists of what are called "arguments". Arguments are strings that are passed to the program that you are executing. Stated more simply, these are pieces of information that you are providing to your command.

To better understand how this works, we can compare the command line and its commands to a well-trained army. In this army, each soldier has a very specific duty, and is trained to do it with perfection. Some duties, like cleaning the dishes, don't take any extra information for them to be accomplished successfully. Other duties require, or at least benefit from, extra information. If you are the general and you tell a soldier to go on patrol, he may be able to go out and patrol somewhere, but it may or may not be where you need him most. He'll probably just go to his usual patrolling area and start there.

Command line commands are basically the same. If you type a command by itself, it will sometimes execute just fine, using default information to do its job. Other times, however, you really need to give the command more information so that it can know exactly how to perform its duty. Take, for example, the tar command. If you open the command line prompt, type tar, and press enter, it won't do much. In fact, all it does is ask you for more information: "You want me to patrol? Where should I patrol?"

$ tar
tar: You must specify one of the '-Acdtrux', '--delete' or '--test-label' options
Try 'tar --help' or 'tar --usage' for more information.

The tar command is an archival command that can archive, compress, and extract files, but to make it work, you need to tell it which files to take action on, and what exactly to do with those files. You should also tell the command where to put the extracted or compressed files once it's done. Let's look at the command in action (the directory is fictitious, so you won't be able to execute this command as-is; also, the lines that start with # are comments):

# Command: compress the files-to-archive directory to archive.tgz in this
# directory.
$ tar -c -z -f ./archive.tgz ./files-to-archive/

In the above example, tar is the command, and -c, -z, -f, ./archive.tgz, ./files-to-archive/ are the arguments. Each of the first three arguments is a flag, which is a special type of argument. The c stands for "create", the z stands for "zip", and the f stands for "file". Flags can take different forms, but they usually start with a dash (-) for abbreviated flags and two dashes (--) for full word flags (e.g. --format). Abbreviated flags can usually be combined, so the above could also be written -czf. To find out which flags a command uses and what they mean, you can type man ("man" is short for "manual") followed by the name of the command:

$ man tar
TAR(1)          BSD General Commands Manual          TAR(1)

NAME
     tar — The GNU version of the tar archiving utility

SYNOPSIS
     tar [-] A --catenate --concatenate | c --create | d
         --diff --compare | --delete | r --append | t
         --list | --test-label | u --update | x --extract
         --get [options] [pathname ...]

DESCRIPTION
     Tar stores and extracts files from a tape or disk ar‐
     chive.
...

Type q to exit man.

What is the Command Line used for?

Now that we've discussed what an interface is, and how the command line is an interface all on its own, let's look at what the command line is typically used for.

The most common use of the command line is what's called "system administration" or, basically, managing computers and servers. This includes installing and configuring software, monitoring computer resources, setting up web servers, and automating processes. The following is a list of common tasks for programmers:

  • Restart servers
  • Rename hundreds or thousands of files according to a prescribed pattern
  • Manage system logs
  • Set up scheduled tasks (cron jobs)
  • Debug server issues
  • Monkey patch code on a server
  • Query data
  • Set up databases and servers

... and many more.

What are some common commands?

The following is a list of some of the most common commands used on the command line. The exercises will walk you through how to use each of them.

Command Description
cd Change directory.
ls List files and directories in current directory.
pwd Display the path of the current directory.
touch Create a file.
mkdir Create a directory.
rm Remove a file or directory. Warning: deleting a file or directory with this command is permanent!
cp Copy a file or directory.
mv Move or rename a file or directory.
echo Print text to STDOUT.
cat Display contents of a file.
more Display contents of a file, starting at the top and letting the user scroll down.
less Display contents of a file in an even more interactive way.
head Display the first part of a file.
tail Display the last part of a file.
man Display documentation about a command.

Exercises

  1. The echo command has only one optional flag: -n, which means "Do not print the trailing newline character." Experiment with echo. Run the following commands, guessing before running each what the output will be:

    $ echo "hello world"
    $ echo hello world
    $ echo -n hello world
    $ echo hello world -n
    $ echo "hello world" -n
    $ echo "-n hello world"
    

    Solution

    The following is the expected output of the above commands:

    $ echo "hello world"
    hello world
    $ echo hello world
    hello world
    $ echo -n hello world
    hello world$ echo hello world -n
    hello world -n
    $ echo "hello world" -n
    hello world -n
    $ echo "-n hello world"
    -n hello world
    

    The above commands illustrate the following ideas:

    1. Arguments can be wrapped in quotes, but don't always have to be wrapped.
    2. Flags come before other arguments.
    3. Misplaced arguments and/or flags change the output.

    Also note that on line 6, the prompt stayed on the same line as the output. This isn't a typo. It's the result of the -n flag being used properly.

  2. We'll talk more about navigating the command line in the next chapter, but as a bit of a preview, let's go over the following commands: cd, ls, and pwd.

    The cd command helps you change your current directory. The ls command gives you information about what files and directories are in a certain directory. Finally, the pwd command, as mentioned above, tells you what your current directory is.

    First, let's see what your current directory is:

    $ pwd
    /home/ubuntu
    

    You should get something like /home/ubuntu as output. So, all the pwd command does is output a string of characters that represent the directory that you are currently in.

    Next, you can try changing your directory with the cd command:

    $ cd /
    $ pwd
    /
    

    The cd command, as can be seen above, doesn't have any output. Now when you run pwd, it shows that you are in the /, or root directory. If you try to change your current directory to a location that doesn't exist, cd will output an error:

    cd /asdf
    -bash: cd: /asdf: No such file or directory
    

    Now let's see what files and directories exist in the root directory:

    $ ls
    bin   home            lib64       opt   sbin  usr
    boot  initrd.img      lost+found  proc  srv   var
    dev   initrd.img.old  media       root  sys   vmlinuz
    etc   lib             mnt         run   tmp   vmlinuz.old
    

    Without any arguments, ls just prints out a list of the files and directories in the current directory. It doesn't tell you much about those files and directories, though. If you add a couple of arguments, you can get the ls command to give up more information about each item in the directory:

    $ ls -lah
    total 84K
    drwxr-xr-x  22 root   root   4.0K May 14 17:31 .
    drwxr-xr-x  22 root   root   4.0K May 14 17:31 ..
    drwxr-xr-x   2 root   root   4.0K May 12 23:20 bin
    drwxr-xr-x   3 root   root   4.0K May 12 23:20 boot
    drwxr-xr-x  13 root   root   3.9K May 14 17:31 dev
    drwxr-xr-x  96 root   root   4.0K Jun 22 17:15 etc
    drwxr-xr-x   3 root   root   4.0K May 12 23:19 home
    lrwxrwxrwx   1 root   root     33 May 12 23:20 initrd.img -> boot/initrd.img-3.13.0-52-generic
    lrwxrwxrwx   1 root   root     33 Mar 25 11:51 initrd.img.old -> boot/initrd.img-3.13.0-48-generic
    drwxr-xr-x  21 root   root   4.0K May 13 19:03 lib
    drwxr-xr-x   2 root   root   4.0K Mar 25 11:50 lib64
    drwx------   2 root   root    16K Mar 25 11:53 lost+found
    drwxr-xr-x   2 root   root   4.0K Mar 25 11:50 media
    drwxr-xr-x   2 root   root   4.0K Apr 10  2014 mnt
    drwxr-xr-x   2 root   root   4.0K Mar 25 11:50 opt
    dr-xr-xr-x 127 root   root      0 May 14 17:30 proc
    drwx------   3 root   root   4.0K May 13 19:00 root
    drwxr-xr-x  18 root   root    700 Jul  1 14:12 run
    drwxr-xr-x   2 root   root   4.0K May 12 23:20 sbin
    drwxr-xr-x   4 ubuntu ubuntu 4.0K Jun 22 17:06 srv
    dr-xr-xr-x  13 root   root      0 May 14 17:30 sys
    drwxrwxrwt   4 root   root   4.0K Jul  1 14:40 tmp
    drwxr-xr-x  10 root   root   4.0K Mar 25 11:50 usr
    drwxr-xr-x  12 root   root   4.0K Mar 25 11:52 var
    lrwxrwxrwx   1 root   root     30 May 12 23:20 vmlinuz -> boot/vmlinuz-3.13.0-52-generic
    lrwxrwxrwx   1 root   root     30 Mar 25 11:51 vmlinuz.old -> boot/vmlinuz-3.13.0-48-generic
    

    We'll go into detail of what everything means in later chapters, but for now, you can see that adding the arguments -l, -a, and -h (grouped into one argument above), adds a lot more information and formats the items into a more easy-to-read list.

  3. The command line offers a bunch of commands to work with files and directories. This exercise will show the basic usage of the following: touch, mkdir, mv, cp, and rm.

    First, let's make sure we're in your home directory (recall that ~ stands for your home directory):

    $ cd ~
    $ pwd
    /home/ubuntu
    

    Now, let's create a practice directory to mess around with:

    $ # Make a directory called "practice"
    $ mkdir practice
    $ ls
    practice
    

    You can see that now there's a directory named "practice" in your home directory. Let's change our current directory to the "practice" directory that we just created:

    $ cd practice
    

    Now, in this directory we can create new files, move or rename them, copy them, and remove them. After that, we'll remove the whole "practice" directory to clean up.

    $ # Create an empty file and verify that it got created
    $ touch example.txt
    $ ls
    example.txt
    

    The touch command created the empty file "example.txt" in the current directory. You can move or rename a file with the mv command:

    $ # Rename example.txt to example1.txt
    $ mv example.txt example1.txt
    $ ls
    example1.txt
    $ # Make another directory
    $ mkdir tmp
    $ # Move example1.txt to the new "tmp" directory
    $ mv example1.txt tmp/
    $ ls tmp/
    example1.txt
    $ ls
    tmp
    $ # Move it back and rename it
    $ mv tmp/example1.txt example2.txt
    $ ls
    example2.txt tmp
    

    You can see from the above examples that moving or renaming a file is basically the same thing on the command line, and follows this pattern:

    $ mv [source] [destination]
    

    Now, let's remove the example file and then the whole practice folder:

    $ rm example2.txt
    $ ls
    tmp
    

    To remove a folder and all its contents, you need to specify the -r (recursive) option.

    $ cd ..
    $ ls
    practice
    $ rm -r practice
    $ ls
    

    The practice folder (and all its contents) are now gone.

    Warning: using the rm command is dangerous and permanent. Do not issue this command until you know for certain you are deleting the right file. Using the rm -r command is even more dangerous, as it will delete recursively.
  4. One of the most common tasks when using the command line interface is reading the contents of a file. This exercise will go over some common commands for doing so: cat, more, less, head, and tail.

    To print out all the contents of a file, use cat:

    $ cat file.txt
    All the content of file.txt printed out here.
    

    To print out the first few lines of a file, use head:

    $ head /etc/mime.types
    #
    #  MIME media types and the extensions that represent them.
    #
    #  The format of this file is a media type on the left and zero or more
    #  filename extensions on the right.  Programs using this file will map
    #  files ending with those extensions to the associated type.
    #
    #  This file is part of the "mime-support" package.  Please report a bug using
    #  the "reportbug" command of the "reportbug" package if you would like new
    

    To print out the last few lines of a file, use tail:

    $ tail /etc/mime.types
    video/x-ms-wmx          wmx
    video/x-ms-wvx          wvx
    video/x-msvideo         avi
    video/x-sgi-movie       movie
    video/x-matroska        mpv mkv
    
    x-conference/x-cooltalk       ice
    
    x-epoc/x-sisx-app       sisx
    x-world/x-vrml
    

    To print out the contents of a file, but only fill one screen's worth at a time, use more:

    $ more /etc/mime.types
    # The first page of /etc/mime.types will show up here, and you can use the
    # down arrow to go to the next line, or the space bar to go to the next
    # page.
    

    Use less when you need to navigate backward and forward in a file:

    $ less /etc/mime.types
    

    The less command allows you to go forward one line with the down arrow, backward one line with the up arrow, and backward and forward a page with the page up and page down keys. You can also use the space bar in the same way you can with the more command.

    Note: to exit more or less, type the q key.

  5. You can get more information about what a command does, how it works, and which flags you can use by referencing the manual for that command. To read the manual pages (typically called manpages) for a command, use the man command:

    $ man touch
    TOUCH(1)                    User Commands                   TOUCH(1)
    
    
    
    NAME
           touch - change file timestamps
    
    SYNOPSIS
           touch [OPTION]... FILE...
    
    DESCRIPTION
           Update  the access and modification times of each FILE to the
           current time.
    ...
    

    The manual pages will sometimes have the available flags and the possible orders they can be used in the "SYNOPSIS" section. In the case of the touch command, the flags can be used in any order just after the touch command itself, and the file is the last argument. The possible options are described in the "DESCRIPTION" section.

    You may have noticed that the man command uses less to display the content of the manpages, so you can use the up- and down-arrow keys to navigate the contents of the manual.

    To exit man, you can type the q key.