Mastering the Linux Terminal: A Beginner’s Guide to Essential Commands

Benjamín Zambelli
12 min readJul 20, 2023

--

We are going to learn about:

  • Navigation Commands (ls, cd)
  • Help commands (- -help, man)
  • Creating Files (cat, touch)
  • Creating a Directory (mkdir)
  • Copying a File or Directory (cp)
  • Moving a File or Directory (mv)
  • Renaming a File or Directory (mv)
  • Removing Files and Directories (rm)
  • Finding Stuff (find)
  • Executing Commands on Found Items
  • Quick look at wildcards
  • Filtering (grep)
  • Using grep in combination with other commands

Why the Terminal?

While graphical user interfaces (GUIs) are user-friendly and visually appealing, the Terminal offers a different level of control and efficiency. Understanding the Terminal and its commands can significantly enhance your productivity, as it allows you to perform tasks quickly, automate repetitive processes, and access powerful tools not readily available in GUIs.

Where Am I Standing?

When you open your Terminal you will see a symbol `~` (tilde) indicating you are in your current user’s home directory.

You can use `pwd` (print working directory) in order to confirm it:

pwd

So the tilde is a synonym for your user’s home directory.

Navigation Commands

In order to see what is in the current directory we use the ls (list) command:

ls

As you can see, there are the same directories than in the GUI (graphical user interface).

Now let’s change the current directory to the miniconda3directory using the command cd (change directory). Tip: press tab key when writing names so you don’t have to write the entire word.

cd miniconda3

Now as you can see we are in ~/miniconda3.

With the list command we can see what is inside.

ls

Directories are blue and files like LICENSE.txt black.

How it looks in the GUI:

Let’s go back to our home directory, you can use cd ~ from anywhere or in this case you could use cd .. (double dots) to move up one level in the file structure.

cd ..

Getting Help

The usefulness of using a command like ls doesn’t end there. Many times commands will have options or flags.

An option or flag is a part of a command that modifies its behavior or provides additional instructions to the command. Options/flags are usually denoted by one or more characters, preceded by a hyphen (-) or two hyphens (--). They allow users to customize how a command operates without the need to modify the command itself.

There are two main types of options or flags:

1. Short options like ls -a
2. Long options like ls -all

Both commands accomplish the same task.

You can investigate what -a does using the command ls --help

When you append --help to a command, it will generally print a short description of the command’s functionality and list available options.

The --help option is useful for quickly understanding the basic functionality of a command and getting a list of available options without diving into detailed documentation.

ls --help

As you can see there is a short description and the first option is the -a and it says that “do not ignore entries starting with .”

In other words, the -a option stands for --all, and it tells the command to include hidden files and directories in the listing. Hidden files are files whose names start with a dot (.), and they are typically configuration files or files that are not intended to be directly displayed in regular directory listings (like .git).

We can combine options, for example we could use:

ls -la

-l stands for “long format” and the output of the command will provide additional details about each file or directory, such as permissions, owner, group, size, modification date, and name. And -a is the option we just learned about.

Additionally to--help you can use the command man:

man ls

This command is used to display the manual pages. These manual pages usually provide more comprehensive information than using --help.

Modifying Files and Directories

Creating Files

There are many ways to create files in Linux, but for now we’ll just look at two simple methods. The first is cat, which is short for concatenate. The cat command is generally used for displaying the contents of a file, but it can also be used to create small text files. For creating larger text files, it’s better to enter the text in a text editor such as nano, vim, emacs, leafpad, gedit, or kate and then save it as a file.

cat > file.txt

When you press ENTER, Linux will go into interactive mode and wait for you to start entering content for the file.

To exit and return to the prompt press CTRL + ­D

Then, we want to see what’s in our new file:

cat file.txt

You can add or append more content to a file with >>:

cat >> file.txt

And overwrite the content with using again only one >:

cat > text.txt

touch

The second command for file creation is touch. Create an empty file:

touch newfile.txt

The size is zero because it is empty.

Creating a Directory (folder)

The command for creating a directory in Linux is mkdir, a contraction of make directory.

mkdir newdirectory

When you create a directory, it has a small default size due to the metadata overhead associated with its structure.

Copying a File or Directory

To copy files, we use the cp command. This creates a duplicate of the file in the new location and leaves the old one in place.

Let’s copy file.txt to newdirectory:

Using absolute path (location of a file or directory starting from the root directory of the file system /):

cp file.txt /home/ben/newdirectory/

Using relative path (location of a file or directory relative to the current working directory):

cp file.txt ./newdirectory/

When we then navigate to newdirectory, we see that there is an exact copy of file.txt:

To copy a directory you can use the cp command with the -r (recursive) option. The -r option allows cp to copy directories and their contents.

Here’s the basic syntax:

cp -r source_directory destination_directory

Moving a File or Directory

The mv command in Linux is used to move or rename files and directories in the file system.

Syntax:

mv [options] source destination

Let’s move newfile.txt to newdirectory:

mv newfile.txt ./newdirectory

Renaming a File or Directory

To rename a file or directory, you use the mv command and provide the current name of the file/directory as the source and the new name as the destination.

Syntax:

mv old_name.txt new_name.txt
mv newfile.txt filetwo.txt

Removing Files and Directories

The rm command in Linux is used to remove (delete) files or directories from the file system. It is a powerful and potentially dangerous command, as it permanently deletes the specified files and directories, and they cannot be easily recovered. Therefore, it is essential to use the rm command with caution and make sure you have a backup of important data before using it.

Syntax:

rm [options] file1 file2 … filen
rm filetwo.txt

By default, the rm command does not remove directories. To remove directories and their contents, you need to use the — r (recursive) option. Be extremely careful when using the -r option, as it can delete an entire directory tree.

rm -r newdirectory/

If a directory is protected you can force the deletion using the -f flag:

rm -rf directory-name

Remember that once you use the rm command, the files or directories are immediately deleted, and there is no way to undo the operation (unless you have a backup). Therefore, it is crucial to double-check the files and directories you are about to delete and ensure that you are using the correct options to avoid unintended consequences.

As a safety measure, you can also create an alias or function for the -rm command that includes the -i option, which will prompt you for confirmation before deleting each file. This way, you can minimize the risk of accidental deletions (I show you an example of -i later).

Finding Stuff

find

The find command allows you to search for files and directories in a directory hierarchy based on various criteria. It is one of the most versatile and commonly used commands for locating files and performing actions on them.

Syntax:

find [starting-path] [options] [expression]

To use the find command, you typically provide the starting path from which the search should begin. If you omit the path, the search will start from the current directory. You can start searching from the root directory /but if you can specify a little better where your file/directory could be, it is more efficient.

You can specify different types of criteria to search for files and directories. Some common criteria include:

-name: searches for files/directories with a specific name (case-sensitive).
-iname: searches for files/directories with a specific name (case-insensitive).
-type: specifies the type of item to search (f for file, d for directory, l for symbolic link, etc.).

Example:

Suppose I want to find a png file named linux-filesystem-hierarchy.png:

find /home/ben/ -type f -name 'linux-filesystem-hierarchy.png'

I start looking at /home/ben/ (starting path), the type is file (f), and the entire name.

As you can see I have that file in /Downloads/

It’s also important to note that unlike some other search commands, find displays only exact name matches. We can remedy this limitation by using wildcards, which enable us to match multiple characters. Wildcards come in a few different forms: *, ., ?, [], etc.

For example to look for any .png file we could use the * wildcard:

find /home/ben/ -type f -name '*.png'

This returns all the png files from my home directory tree.

Executing Commands on Found Items

You can perform various actions on the found files/directories using -exec option. This allows you to execute shell commands on the found items.

Suppose I want to find a specific png file and delete it.

find . -type f -name 'linux-filesystem-hierarchy.png' -exec rm {} \;

Here looking from the current directory . we are going to delete the specified file.

-exec rm {} \; is an action provided to the find command. It instructs find to execute the rm (remove) command on each file that matches the search criteria. The {} is a placeholder for the file found by the find command, and it will be replaced by the actual file name during the execution (rm linux-…-hierarchy.png). The \; at the end is used to terminate the -exec option.

We could use the wildcard *.png in order to delete every single png file.

BUT now important notes:

  • Be extremely careful when using the rm command, especially with find and -exec, as there is no confirmation prompt, and the files will be permanently deleted.
  • If you want to avoid accidental deletions, consider adding the -i option to the rm command, which will prompt you for confirmation before each deletion:

Quick look at wildcards

Let’s say we’re doing a search on a directory that has the files cat, hat, what, and bat. The ? wildcard is used to represent a single character, so a search for ?at would find hat, cat, and bat but not what, because at in this filename is preceded by two letters. The [] wildcard is used to match the characters that appear inside the square brackets. For example, a search for [c,b]at would match cat and bat but not hat or what. Among the most widely used wildcards is the asterisk *, which matches any character(s) of any length, from zero to an unlimited number of characters. A search for *at, for example, would find cat, hat, what, and bat.

Filtering with grep

The grep command is used for searching and pattern matching within files or streams of text. Its name stands for “Global Regular Expression Print,” which reflects its primary function of searching for text patterns (regular expressions) and printing matching lines.

Syntax:

grep [options] pattern [file…]

The most straightforward use of grep is to search for a specific pattern within one or more files. If no files are specified, grep will read from standard input (usually your keyboard), allowing you to enter text directly.

In this example, grep will search for the string “three” in the file file.txt and display all lines containing that pattern.

grep supports powerful regular expressions (regex) for pattern matching.

Notice the difference between using *and .*

tw*using shell wildcard matches “t” followed by zero or more occurrences of “w” (e.g., “t”, “th”, “twww...w", etc.).
tw.* using regular expression matches “tw” followed by any character (e.g., “two”, “twoooo”, “tw!”, etc.).

Notice the difference between using ? and .

? specifies that the preceding character can occur zero or one time. For example, the regex pattern colou?r will match both “color” and “colour” because the `u` is optional.
. matches any single character. For example, the regex pattern `b.t` will match “bat,” “bit,” “but,” and so on, as long as there is a character between ‘b’ and ‘t’.

Note: to match a literal question mark or dot in the input string, you need to escape it with a backslash \? or \. respectively.

By default, grep is case-sensitive. To perform a case-insensitive search, use the -i option.

grep -i “THREE” file.txt

You can also use the -v option to perform an inverse match, showing lines that do not match the specified pattern.

grep -v “three” file.txt

To search for a pattern in all files within a directory and its subdirectories, use the -r (recursive) option.

grep -r “three” /home/ben/

To see only the count of matching lines, use the -c option.

The grep command is an essential tool for text processing, searching, and pattern matching in Linux. It is often used in combination with other commands to perform complex text processing tasks and is highly versatile for handling various text-based scenarios. Additionally, grep supports numerous other options and can be used creatively in scripting and other automation tasks.

Using grep in combination with other commands

The grep command is often used when output is piped from one command to another. Linux allows us to take the output of one command and send it as input to another command. This is called piping, and we use the | command to do it.

The ps command is used to display information about processes running on the machine. Suppose we want to see all the processes running on my Linux system. In this case, I can use the ps (processes) command followed by aux:

ps aux

This provides me with a listing of all the processes running in this system — but what if I just want to find one process to see if it is running?

I can do this by piping the output from ps to grep and searching for a keyword. For instance, to find out whether the virtualbox service is running, I would enter the following:

ps aux | grep virtualbox

This command tells Linux to display all my services and then send that output to grep, which will look through the output for the keyword virtualbox and then display only the relevant output.

--

--