The Unix command line, often referred to as the terminal or shell, is a powerful text-based interface for interacting and using Unix-based operating systems. Unix commands will be used throughout various guides in this website, so it is essential to have a basic understanding of how to navigate and perform tasks in the terminal.

Opening the Terminal

The first step is to open the terminal application. On macOS, you can find it in Applications > Utilities. In Linux distributions, it's usually accessible through the system's main menu or by pressing Ctrl+Alt+T.

Terminal window

Command line flow

The command line takes input in the form of text commands and executes them. When you open the terminal, you'll see a prompt that typically displays your username, hostname, and the current directory. It might look something like this:

user@hostname:~$

This varies depending on your system. When you see this prompt, you can start typing commands. As soon as you type a command and press Enter, the system will execute it and display the output. Once the command finishes executing, you'll see the prompt again, ready for your next command.

The working directory

The terminal always operates in a specific directory, known as the working directory. Any commands you run will affect the files and directories in this location. You can see the current working directory by running the pwd command, which stands for "print working directory". For example:

user@hostname:~$ pwd
/home/user

This means you're currently in the /home/user directory. All commands you run will affect this directory unless you specify otherwise. For example, if you run ls to list files, it will show the files in the /home/user directory.

user@hostname:~$ pwd
/home/user
user@hostname:~$ ls
file1.txt file2.txt directory1

This indicates that there are two files (file1.txt and file2.txt) and one directory (directory1) in the /home/user directory. If you were to navigate to a different directory, the output of ls would show the files and directories in that location. Example:

user@hostname:~/directory1$ pwd
/home/user/directory1
user@hostname:~/directory1$ ls
file3.txt file4.txt

The output of ls (List files) now shows the files file3.txt and file4.txt, because you've navigated to the /home/user/directory1 directory.

Basic commands

Now that you understand the basic flow of the command line, let's look at some essential commands to get you started:

Providing arguments and input to commands

Many commands accept arguments to modify their behavior. For example, the ls command can take options like -l for a detailed list or -a to show hidden files. Arguments are usually preceded by a hyphen (-). For example:

The specific arguments a command accepts will vary, so it's essential to refer to the command's manual (man <command>) for detailed information. See Reading manual pages for more information on how to read manual pages.

Some commands also accept input from the user. For example, the echo command can take a string as input and display it. Example:

user@hostname:~$ echo "Hello, World!"
Hello, World!

In this case, the string "Hello, World!" is provided as input to the echo command, which then displays it on the terminal.

File paths and directories

When working with files and directories, you'll often need to specify their paths. There are two types of paths:

Special characters in paths

When working with file paths, you may encounter special characters that have specific meanings:

Tab completion

Tab completion is a useful feature that helps you quickly complete file and directory names. When you start typing a file or directory name and press the Tab key, the terminal will automatically complete the name if it's unique. If there are multiple options, pressing Tab twice will show a list of possible completions.

Reading manual pages and finding information

The man command is used to display the manual pages for various commands. These manual pages provide detailed information about the command, including its usage, options, and examples. To view the manual for a specific command, use the following syntax:

man <command>

The manual pages are organized into sections, each covering a specific topic. The most common sections you'll encounter are:

To navigate through the manual pages, you can use the arrow keys, Page Up and Page Down keys, or the q key to exit the manual.

These days, you can also find a lot of information online about various commands and their usage. You can usually find more information by either:

Advanced commands and concepts

Piping and Redirection

Wildcards

Wildcards are characters that help match filenames or paths based on patterns. Some common wildcards are:

Interrupting commands

You may encounter situations where a command is taking too long to execute, or you want to stop a running command. You can interrupt a command by pressing Ctrl + C. This sends a termination signal to the command, stopping its execution. But be careful! This may cause the command to terminate abruptly, which could lead to unexpected behavior or data loss.

Example of working with the terminal

Let's walk through an example to demonstrate how you might use the terminal to perform common tasks.

Let's say we want to create a directory and a file within that directory:

user@hostname:~$ pwd
/home/user
user@hostname:~$ mkdir example_directory
user@hostname:~$ cd example_directory
user@hostname:~/example_directory$ touch example_file.txt
user@hostname:~/example_directory$ ls
example_file.txt

Now, let's write some content to the file and display it:

user@hostname:~/example_directory$ echo "Hello, World!" > example_file.txt
user@hostname:~/example_directory$ cat example_file.txt
Hello, World!

Cool! Now, let's append some more content to the file.

user@hostname:~/example_directory$ echo "Goodbye, World!" >> example_file.txt
user@hostname:~/example_directory$ cat example_file.txt
Hello, World!
Goodbye, World!

Finally, let's search for lines containing "Hello" in the file using grep:

user@hostname:~/example_directory$ cat example_file.txt | grep "Hello"    
Hello, World!

Here we only see the line containing "Hello" in the file, as filtered by grep. Let's go back up one directory:

user@hostname:~/example_directory$ cd ..
user@hostname:~$ 

And let's remove the directory and its contents:

user@hostname:~$ rm example_directory
rm: example_directory: is a directory

Oops! We can't remove the directory directly with rm. We need to use rm -r (recursive) to remove the directory and its contents:

user@hostname:~$ rm -r example_directory
user@hostname:~$ ls
user@hostname:~$ 

And that's it! We've created a directory, a file, written content to the file, searched for specific lines, and then removed the directory and its contents.

Learning further

To deepen your understanding and skills, here are some learning resources you might find helpful: