Linux is a family of free and open-source operating systems based on the Linux kernel. Operating systems based on Linux are known as Linux distributions or distros. Examples include Debian, Ubuntu, Fedora, CentOS, Gentoo, Arch Linux, and many others.
The Linux kernel has been under active development since 1991, and has proven to be extremely versatile and adaptable. You can find computers that run Linux in a wide variety of contexts all over the world, from web servers to cell phones. Today, 90% of all cloud infrastructure and 74% of the world’s smartphones are powered by Linux.
However, newcomers to Linux may find it somewhat difficult to approach, as Linux filesystems have a different structure than those found on Windows or MacOS. Additionally, Linux-based operating systems depend heavily on working with the command line interface, while most personal computers rely on graphical interfaces.
This guide serves as an introduction to important command line concepts and skills and equips newcomers to learn more about Linux.
To follow along with this guide, you will need access to a computer running a Linux-based operating system. This can either be a virtual private server which you’ve connected to with SSH or your local machine. Note that this tutorial was validated using a Linux server running Ubuntu 20.04, but the examples given should work on a computer running any version of any Linux distribution.
If you plan to use a remote server to follow this guide, we encourage you to first complete our Initial Server Setup guide. Doing so will set you up with a secure server environment — including a non-root user with
sudo privileges and a firewall configured with UFW — which you can use to build your Linux skills.
The terms “terminal,” “shell,” and “command line interface” are often used interchangeably, but there are subtle differences between them:
When someone refers to one of these three terms in the context of Linux, they generally mean a terminal environment where you can run commands and see the results printed out to the terminal, such as this:
Becoming a Linux expert requires you to be comfortable with using a terminal. Any administrative task, including file manipulation, package installation, and user management, can be accomplished through the terminal. The terminal is interactive: you specify commands to run and the terminal outputs the results of those commands. To execute any command, you type it into the prompt and press
When accessing a cloud server, you’ll most often be doing so through a terminal shell. Although personal computers that run Linux often come with the kind of graphical desktop environment familiar to most computer users, it is often more efficient or practical to perform certain tasks through commands entered into the terminal.
Nearly all Linux distributions are compliant with a universal standard for filesystem directory structure known as the Filesystem Hierarchy Standard (FHS). The FHS defines a set of directories, each of which serve their own special function.
The forward slash (
/) is used to indicate the root directory in the filesystem hierarchy defined by the FHS.
When a user logs in to the shell, they are brought to their own user directory, stored within
/home/. This is referred to as the user’s home directory. The FHS defines
/home/ as containing the home directories for regular users.
The root user has its own home directory specified by the FHS:
/root/. Note that
/ is referred to as the “root directory”, and that it is different from
root/, which is stored within
Because the FHS is the default filesystem layout on Linux machines, and each directory within it is included to serve a specific purpose, it simplifies the process of organizing files by their function.
Linux filesystems are based on a directory tree. This means that you can create directories (which are functionally identical to folders found in other operating systems) inside other directories, and files can exist in any directory.
To see what directory you are currently active in you can run the
pwd command, which stands for “print working directory”:
pwd prints the path to your current directory. The output will be similar to this:
This example output indicates that the current active directory is
sammy, which is inside the
home/ directory, which lives in the root directory,
/. As mentioned previously, since the
sammy/ directory is stored within the
sammy/ represents the sammy user’s home directory.
To see a list of files and directories that exist in your current working directory, run the
This will return a list of the names of any files or directories held in your current working directory. If you’re following this guide on a new machine, though, this command may not return any output.
You can create one or more new directories within your current working directory with the
mkdir command, which stands for “make directory”. For example, to create two new directories named
testdir2, you might run the following command:
- mkdir testdir1 testdir2
Now when you run the
ls command, these directories will appear in the output:
To navigate into one of these new directories, run the
cd command (which stands for “change directory”) and specify the directory’s name:
- cd testdir1
This will change your new current working directory to the directory you specified. You can see this with
testdir2 are both held in the sammy user’s home directory, they reside in different branches of the directory tree. The
cd command looks for directories within your current working directory, so this means that you cannot
cd directly into the
testdir2 directory you created previously while
testdir1 is your working directory:
- cd testdir2
Outputbash: cd: testdir2: No such file or directory
However, you can navigate into any existing directory regardless of your current working directory if you specify the full path of the directory you want to navigate to:
- cd /home/sammy/testdir2
Note: In Linux, a tilde (
~) is shorthand for the home directory of the user you’re logged in as. Knowing this, you could alternatively write the previous command like this and it would achieve the same result:
- cd ~/testdir2
Additionally, you can specify
.. to change to the directory one level up in your path. To get back to your original directory:
- cd ..
If you’re ever confused about where you are in the navigation tree, remember you can always run the
pwd command to find your current directory. Many modern shells (including Bash, the default for many Linux distributions) also indicate your current directory, as exhibited in the example commands throughout this section.
You cannot use
cd to interact with files;
cd stands for “change directory”, and only allows you to navigate directories. You can, however, create, edit, and view the contents of files.
One way to create a file is with the
touch command. To create a new file called
- touch file.txt
This creates an empty file with the name
file.txt in your current working directory. The contents of this file are empty.
If you decide to rename
file.txt later on, you can do so with the
- mv file.txt newfile.txt
mv stands for “move” and it can move a file or directory from one place to another. By specifying the original file,
file.txt, you can “move” it to a new location in the current working directory, thereby renaming it.
It is also possible to copy a file to a new location with the
cp command. If we want to bring back
file.txt but keep
newfile.txt, you can make a copy of
file.txt like this:
- cp newfile.txt file.txt
As you may have guessed,
cp is short for “copy”. By copying
newfile.txt to a new file called
file.txt, you have replicated the original file in a new file with a different name.
However, files are not of much use if they don’t contain anything. To edit files, a file editor is necessary.
There are many options for file editors, all created by professionals for daily use. Such editors include
nano is a suitable option for beginners: it is relatively user-friendly and doesn’t overload you with cryptic options or commands.
To add text to
nano, run the following command:
- nano file.txt
This will open up a space where you can immediately start typing to edit
file.txt. Add whatever text you like, or you can copy the text in this example:
Say it's only a paper moon Sailing over a cardboard sea, But it wouldn't be make believe If you believed in me. Yes it's only a canvas sky Hanging over a muslin tree, But it wouldn't be make believe If you believed in me. Without your love, It's a honky-tonk parade. Without your love, It's a melody played in a penny arcade. It's a Barnum and Bailey world, Just as phony as it can be, But it wouldn't be make believe If you believed in me.
To save your written text, press
CTRL + X,
Y, and then
ENTER. This returns you to the shell with a newly saved
file.txt has some text within it, you can view it using
cat command prints the contents of a specified file to your system’s output. Try running
cat and pass the
file.txt file you just edited as an argument:
- cat file.txt
This will print out the entire contents of
file.txt to the terminal. If you used the text from the previous example, this command will return output similar to this:
OutputSay it's only a paper moon Sailing over a cardboard sea, But it wouldn't be make believe If you believed in me. Yes it's only a canvas sky Hanging over a muslin tree, But it wouldn't be make believe If you believed in me. Without your love, It's a honky-tonk parade. Without your love, It's a melody played in a penny arcade. It's a Barnum and Bailey world, Just as phony as it can be, But it wouldn't be make believe If you believed in me.
cat to view file contents can be unwieldy and difficult to read if the file is particularly long. As an alternative, you can use the
less command which will allow you to paginate the output.
less to view the contents of the
file.txt file, like this:
- less file.txt
This will also print the contents of
file.txt, but one terminal page at a time beginning at the start of the file. You can use the spacebar to advance a page, or the arrow keys to go up and down one line at a time.
q to quit out of
Finally, to delete the
file.txt file, pass the name of the file as an argument to
- rm file.txt
Note: Without other options, the
rm command (which stands for “remove”) cannot be used to delete directories. However, it does include the
-d flag which allows you to delete empty directories:
- rm -d directory
You can also remove empty directories with the
- rmdir directory
If you want to delete a directory that isn’t empty, you can run
rm with the
-r flag. This will delete the specified directory along with of its contents, including any files and subdirectories:
- rm -r directory
However, because deleting content is a permanent action, you should only run
rm with the
-r option if you’re certain that you want to delete the specified directory.
It takes time, dedication, and a curious mindset to feel comfortable navigating around a Linux system through a terminal window, especially if it’s entirely new to you.
When you have a question about how to accomplish a certain task, there are several avenues of instruction you can turn to. Search engines like Google and DuckDuckGo are invaluable resources, as are question-and-answer sites like Stack Exchange or DigitalOcean’s Community Q&A. Odds are that if you have a question, many others have already asked it and had the question answered.
If your question has to do with a specific Linux command, the manual pages offer detailed and insightful documentation for nearly every command. To see the man page for any command, pass the command’s name as an argument to the
- man command
man rm displays the purpose of
rm, how to use it, what options are available, examples of use, and more useful information.
This guide serves as an introduction to working with a Linux environment. However, fully understanding Linux and all of its components is far beyond the scope of a single tutorial. For instance, this tutorial makes no mention of permissions, a fundamental concept of Linux system administration.
We encourage you to check out all of our introductory Linux content, which can be found on our Linux Basics tag page.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.