Graphical interfaces are great—we wouldn’t want to live without them. But if you’re a Mac or Linux user and you want to get the most out of your operating system (and your keystrokes), you owe it to yourself to get acquainted with the Unix command line. Point-and-click is wonderful whenever you need to do something once or twice. But if you need to repeat that task many times, the command line is your savior.
The command line is a window into the full, awesome power of your computer. If you long to break free of the constraints of the GUI or think that programming or administering remote machines is in your future, then learning the Unix command line is definitely for you.
Don't worry if Unix commands seem like magical incantations or the mysterious internals of the system seem beyond your grasp. They aren't that difficult to learn, and this article will give you the 10 essential commands you need to get started. Before long those cryptic strings will be second nature.
The Unix command line shell is roughly equivalent to the command window in Microsoft Windows (cmd or PowerShell). The commands we walk through below will work on any Unix-like system, including Linux, Darwin (the foundation of MacOS), FreeBSD, and even Windows with something like Git Bash or the new Bash shell in Windows 10. The options and output vary slightly, but you should have no trouble making sense of them.
First, you must open a shell, sometimes called a terminal window. Often Unix distributions put this under the Administration or System menus. In MacOS, you’ll find the terminal in Applications > Utilities > Terminal. When you launch it, you'll see something like this:
This screen, from MacOS 10.11, is typical of most shells in a GUI. At the top of the window we see the type of shell, in this case Bash (Bourne Again Shell, which is the default shell in MacOS and most Linux distributions), and the size of the window. Inside the window is the prompt, in this case configured to display the name of the machine (
Mercury), the name of the current working directory (here the tilde,
~, which is shorthand for the user’s home directory), the name of the user, and finally the prompt symbol (the
$). Note that your prompt will change as you move around the file system or if you become a different user on your machine (such as the root or superuser in order to run administrative commands). The prompt displays this information, so you can easily tell where and who you are on any machine.
It’s worth knowing there are two major flavors of the Unix shell: Bourne and C shell. Bourne and company were derived from the original AT&T Unix, while the C shell hails from the University of California at Berkeley and BSD Unix. Generally the Bourne and C shell derivatives are good for interactive work at the terminal. The POSIX standard shell, the Korn shell, is the one you'd want to use for writing your own programs in the shell, called scripts. We use the Bash shell for the examples in this tutorial.
The shell environment
One of the first facts to understand about working on the Unix command line is that the shell operates in its own environment. Understanding how to control the shell environment is an important part of becoming efficient at the command line. Let’s take a look at the environment using the
Don't worry about understanding all of the environment variables now, but know that they're there. You should recognize a few variables already. For example,
SHELL=/bin/bash tells us we're using the Bash shell.
HOME=/Users/nunez specifies the location of the user’s home directory. You can change or create environment variables, and you will often do so. Here is an example of setting an environment variable called
FOO and displaying its value:
As you see, by convention we put variables in upper case. Take special note how we reference environment variables when we use them in commands, with the preceding
$ tells the command interpreter to use the variable’s value. Without the
echo command above would simply print the name of the variable,
Regardless of which shell you use, whenever you type a command in the shell, you will cause a Unix program to execute. The Unix design philosophy is to create programs that do one thing well and to chain (or “pipe”) them together to do useful work. Let’s look at a simple example to count the number of files in the /etc directory (we’ll see how to move to the /etc directory later on):
This command sequence illustrates two important concepts: piping and options. The
ls command (equivalent to the
dir command in Windows) displays the contents of a directory, and
wc (word count) the number of words. Notice the vertical bar between them? That's the pipe character. The pipe takes the output of the first command and directs it as input to the second command. You can chain any number of commands together in Unix by connecting one to another with pipes.
The second thing to notice is the options given to each command. In Unix, options are traditionally prefixed with a single dash character,
-. These command-line options change the behavior of the command. In this example, the
-l option to
ls means to output the directory contents in “long” format, while the
–l option to
wc means to count the “lines” instead of the words. In English this command might be read:
List the number of lines in the current directory and then send them to the word count program to count the number of lines.
Often these command-line options override defaults that are set in the environment. If you want to change the way the command behaves on a permanent basis, you can set the environment variable automatically when you log in. Many commands allow you to combine options in a single string—for example,
ls -la—but others do not. You can learn about all of the options for a command by checking its manual or “man pages” (which we discuss below).
Learning and using command-line options is a big part of being efficient at the Unix command line. Some commands have so many options that the documentation runs to dozens of pages. Don't let that worry you now. You often need only a few options to perform a given task, and many options are used only when writing programs in the shell language.
Next page: More must-know Unix commands