// Tutorial //

An Introduction to Linux Basics

Updated on August 2, 2021
An Introduction to Linux Basics

Introduction

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.

Prerequisites

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 Terminal

The terms “terminal,” “shell,” and “command line interface” are often used interchangeably, but there are subtle differences between them:

  • A terminal is an input and output environment that presents a text-only window running a shell.
  • A shell is a program that exposes the computer’s operating system to a user or program. In Linux systems, the shell presented in a terminal is a command line interpreter.
  • A command line interface is a user interface (managed by a command line interpreter program) which processes commands to a computer program and outputs the results.

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:

Terminal window example

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 ENTER.

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.

The Filesystem Hierarchy Standard

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”:

  1. pwd

pwd prints the path to your current directory. The output will be similar to this:

Output
/home/sammy

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 home/ directory, 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 ls command:

  1. ls

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 testdir1 and testdir2, you might run the following command:

  1. mkdir testdir1 testdir2

Now when you run the ls command, these directories will appear in the output:

  1. ls
Output
testdir1 testdir2

To navigate into one of these new directories, run the cd command (which stands for “change directory”) and specify the directory’s name:

  1. cd testdir1

This will change your new current working directory to the directory you specified. You can see this with pwd:

  1. pwd
Output
/home/sammy/testdir1

However, because testdir1 and 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:

  1. cd testdir2
Output
bash: 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:

  1. 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:

  1. cd ~/testdir2

Additionally, you can specify .. to change to the directory one level up in your path. To get back to your original directory:

  1. 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.

File Manipulation

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 file.txt:

  1. 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 command:

  1. 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 newfile.txt named file.txt like this:

  1. 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 vim, emacs, nano, and pico.

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 file.txt with nano, run the following command:

  1. 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:

file.txt
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 file.

Now that file.txt has some text within it, you can view it using cat or less.

The 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:

  1. 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:

Output
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.

Using 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.

Use less to view the contents of the file.txt file, like this:

  1. 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.

Press q to quit out of less.

Finally, to delete the file.txt file, pass the name of the file as an argument to rm:

  1. 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:

  1. rm -d directory

You can also remove empty directories with the rmdir command:

  1. 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:

  1. 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.

A Culture of Learning

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:

  1. man command

For instance, man rm displays the purpose of rm, how to use it, what options are available, examples of use, and more useful information.

Conclusion

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.

Learn more about us


About the authors
Default avatar
J Kim

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
3 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This is a good intro, but I need more. 🙂 For example, at the beg, you mention Debian, Ubuntu, Fedora, CentOS, Gentoo, and Arch Linux, and I’ve seen mention of Gnome, Mate, xfce or something. And then there’s Mint and Cinnamon too. I know there are layers? But I don’t understand that hierarchy/structure yet. ?? Thanks!

Excellent Guide! This is by far the best yet summarized reference for anyone who wants to get started quickly, for newcomers and advanced users. Thank you, DigitalOcean!

Wat’s with the weird indentations yo?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel