- Technical difficulty: ★☆☆☆☆
- Server OS covered: Any Unix-like system (macOS, Linux, FreeBSD)
- Client operating systems covered: Any Unix-like system (macOS, Linux, FreeBSD)
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
.
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:
pwd
(print working directory): Displays the current directory you're in.ls
(list): Lists files and directories in the current directory. Usels -l
for a detailed list andls -a
to show hidden files.cd
(change directory): Changes the current directory. Usecd ..
to go up one directory,cd ~
to go to your home directory, orcd /path/to/directory
to navigate to a specific directory.cat
(concatenate): Displays the content of a file. Usage:cat filename
.echo
: Displays a line of text/string that is passed as an argument. Usage:echo "Hello, World!"
.man
(manual): Shows the manual of a command. Usage:man <command>
to see the manual for the specified command. Example:man ls
.clear
: Clears the terminal screen.mkdir
(make directory): Creates a new directory. Usage:mkdir <directory_name>
.touch
: Creates a new file. Usage:touch <filename>
.cp
(copy): Copies files or directories. Usage:cp <source> <destination>
.mv
(move): Moves or renames files or directories. Usage:mv source destination
.sudo
(superuser do): Executes a command with superuser privileges. Generally needed for system-wide changes. Usage:sudo <command>
. Example:sudo apt update
.rm
(remove): Deletes files. Usage:rm <filename>
. Use with caution, as deleted files are not recoverable.
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:
ls -l
: Lists files in long format.ls -a
: Lists all files, including hidden files.ls /path/to/directory
: Lists files in a specific directory.
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:
- Absolute path: Specifies the full path from the root directory of your computer. For example,
/home/user/file.txt
. Notice that it starts with a/
. This path is independent of the current working directory. - Relative path: Specifies the path relative to the current working directory. For example,
file.txt
ordirectory1/file.txt
. This path is relative to the current directory.
Special characters in paths
When working with file paths, you may encounter special characters that have specific meanings:
.
(dot): Represents the current directory. For example,./file.txt
refers to a file in the current directory...
(double dot): Represents the parent directory. For example, if your current directory is/home/user/directory1
,../file.txt
refers to/home/user/file.txt
.~
(tilde): Represents the home directory of the current user. For example,~/file.txt
refers to/home/user/file.txt
. The home directory is usually where your user-specific files and data are stored.
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:
- NAME: Provides the name and a brief description of the command.
- SYNOPSIS: Shows the syntax and options of the command. It provides a quick reference for using the command.
- DESCRIPTION: Provides a detailed explanation of the command and its functionality.
- OPTIONS: Lists the available options and flags that can be used with the command.
- EXAMPLES: Demonstrates how to use the command with various options and arguments.
- SEE ALSO: Lists related commands or topics that may be of interest.
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:
- Searching for the command name followed on a search engine.
- Visiting the official documentation or website of the command or tool.
- Asking ChatGPT or other LLMs to explain the command or provide examples.
- Checking online forums or communities like Stack Overflow for specific questions or issues.
Advanced commands and concepts
Piping and Redirection
|
(pipe): Takes the output of one command as the input to another command. For example,cat file.txt | grep "Hello"
reads the content offile.txt
and searches for lines containing "Hello".>
: Redirects the output of a command to a file, overwriting the existing contents. Example:echo "Hello" > greetings.txt
writes "Hello" togreetings.txt
. This will overwrite the file if it already exists.>>
: Redirects the output of a command to a file, appending the output to any existing contents. Example:echo "Hello" >> greetings.txt
. This will append "Hello" to the end of the file.
Wildcards
Wildcards are characters that help match filenames or paths based on patterns. Some common wildcards are:
*
(asterisk): Matches any sequence of characters. For example,ls *.txt
lists all files with a.txt
extension.?
(question mark): Matches any single character. For example,ls file?.txt
matchesfile1.txt
,file2.txt
, etc.[]
(brackets): Matches any character enclosed within the brackets. For example,ls file[123].txt
matchesfile1.txt
,file2.txt
, orfile3.txt
.
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: