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.
Note that interface has a much broader definition these days. For this book, however, we're only interested in the display and input of text and commands via the command line.
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:
The above prompt follows this format:
[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
$. 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.
The third 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
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"
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:
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
source can directly affect the files on your computer or can change your command line environment.
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:
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: You must specify one of the '-Acdtrux', '--delete' or '--test-label' options
Try 'tar --help' or 'tar --usage' for more information.
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
$ tar -c -z -f ./archive.tgz ./files-to-archive/
In the above example,
tar is the command, and
./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)
tar — The GNU version of the tar archiving utility
tar [-] A --catenate --concatenate | c --create | d
--diff --compare | --delete | r --append | t
--list | --test-label | u --update | x --extract
--get [options] [pathname ...]
Tar stores and extracts files from a tape or disk ar‐
q to exit
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:
... and many more.
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.
|List files and directories in current directory.
|Display the path of the current directory.
|Create a file.
|Create a directory.
|Remove a file or directory. Warning: deleting a file or directory with this command is permanent!
|Copy a file or directory.
|Move or rename a file or directory.
|Print text to STDOUT.
|Display contents of a file.
|Display contents of a file, starting at the top and letting the user scroll down.
|Display contents of a file in an even more interactive way.
|Display the first part of a file.
|Display the last part of a file.
|Display documentation about a command.
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"
Zsh has a built-in feature that prints a highlighted
% and a newline character if a command typed in the Terminal does not output its own newline character. If you see the highlighted
%, you can assume that the
echo command worked as intended.
The following is the expected output of the above commands:
$ echo "hello world"
$ echo 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:
-n) usually must come before other arguments. (There are exceptions to this rule.)
Also note that on line 6, in Bash, 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.
In Zsh, line 6 works differently - it prints a highlighted
% character and a newline instead of displaying the prompt on the same line. See above for a brief explanation of this behavior. To see how newline suppression should work, try running two commands at once:
% echo -n Hello; echo -n Goodbye
% echo Hi again!
% on the second line of this output should be highlighted.
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 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:
You should get something like
/home/ubuntu as output. 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 /
cd command, as can be seen above, doesn't have any output. Now when you run
pwd, it shows that you are in the
/ (often called the "root" directory). The
/ directory is the top-level directory on Unix/Linux/Mac operating systems. If you try to change your current directory to a location that doesn't exist,
cd will output an error:
-bash: cd: /asdf: No such file or directory
Now let's see what files and directories exist in the
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
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
-h (grouped into one argument above), adds a lot more information and formats the items into a more easy-to-read list.
The command line offers a bunch of commands to work with files and directories. This exercise will show the basic usage of the following:
First, let's make sure we're in your home directory (recall that
~ stands for your home directory):
$ cd ~
Now, let's create a practice directory to mess around with:
$ # Make a directory called "practice"
$ mkdir 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:
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
touch command created the empty file "example.txt" in the current directory. You can move or rename a file with the
$ # Rename example.txt to example1.txt
$ mv example.txt example1.txt
$ # Make another directory
$ mkdir tmp
$ # Move example1.txt to the new "tmp" directory
$ mv example1.txt tmp
$ ls tmp
$ # Move it back and rename it
$ mv tmp/example1.txt example2.txt
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]
Next, we can use the
cp command to copy files from one location to another, perhaps with a different name:
$ # Copy example2.txt to the "tmp" directory
$ cp example2.txt tmp
$ ls tmp
We now have two separate files named
example2.txt; one in the current directory, and one in the
cp can also be used to copy files with a new name:
$ # Copy example2.txt to a new file
$ cp example2.txt example3.txt
example2.txt example3.txt tmp
cp command follows a simple pattern:
cp [source] [destination]
Now, let's remove the example file and then the whole
$ rm example2.txt
$ rm example3.txt
To remove a folder and all its contents, you need to specify the
-r (recursive) option.
$ cd ..
$ rm -r practice
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; that is, it will delete a directory and all files or subdirectories nested inside it.
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:
To print out all the contents of a file, use
$ echo "Some example text" > file.txt
$ cat file.txt
All the content of file.txt printed out here.
To print out the first few lines of a file, use
$ head /etc/services
# Network services, Internet style
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# The latest IANA port assignments can be gotten from
To print out the last few lines of a file, use
$ tail /etc/services
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
# 48130-48555 Unassigned
com-bardac-dw 48556/udp # com-bardac-dw
com-bardac-dw 48556/tcp # com-bardac-dw
# Nicholas J Howes <firstname.lastname@example.org>
# 48557-49150 Unassigned
# 49151 IANA Reserved
To print out the contents of a file, but only fill one screen's worth at a time, use
$ more /etc/services
# The first page of /etc/services 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
less when you need to navigate backward and forward in a file:
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
less, type the
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 touch
TOUCH(1) User Commands TOUCH(1)
touch - change file timestamps
touch [OPTION]... FILE...
Update the access and modification times of each FILE to the
The "SYNOPSIS" section of some manual pages describes the available flags that can be used. 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.
man, you can type the