If you are just looking for a quick recap and cheatsheet of Linux commands you are already familiar with, skip to the last section.
First steps with the Linux command line
- linux
- ubuntu
- pwd
- mkdir
This tutorial shows you how to get started with the Linux command line (also known as the terminal).
You may be used to using a Graphical User Interface (GUI) for your machine, such as Windows or MacOS. These GUIs make it easy to do everything visually, with clicks of your mouse to open, and close programs and complete thousands of different tasks.
However, when you provision a virtual or remote machine such as a Scaleway Instance, Dedibox, or Elastic Metal server it is more usual to use the text-based command line, rather than a GUI, to operate the machine. While GUIs are user-friendly, the command line is much more powerful, uses fewer of the machine’s resources (leaving more compute power to be used elsewhere), is less laggy, and more efficient.
Even if you are using a GUI on your own local machine, you can also choose to use the command line by opening a terminal application in your GUI, to start carrying actions via the command line instead of the graphical interface.
When you are faced with operating a machine via the command line for the first time, this can be confusing and frustrating. This document aims to show you some of the basic principles of using a command line instead of a graphical interface. We will guide you through a series of hands-on exercises to practice some of the main commands you need to get started.
In this document, we cover commands for a Linux command line. If you are using a Windows command line, we recommend that you refer to the Windows documentation, as the commands differ. In the context of virtual machines and servers, a Linux-based OS is generally the go-to choice. Unlike Windows, Linux OSes do not require paid-for licenses; they are free and open source.
Before you start
To complete the actions presented below, you must have:
- Provisioned a virtual/remote machine running a Linux OS and connected to its command line via SSH or you are using a physical machine running a Linux OS or MacOS, and have opened the terminal.
The command prompt
When you are first connected to the command line (aka the shell), you will see a command prompt which should look something like this:
username@machine-name:~$
If you see /root
, this suggests you may have a fresh Linux installation with no individually created users yet. It would be a good idea to create a “normal” user before continuing.
The exact prompt will vary depending on the setup of your individual machine. But generally, it will display your username on the machine, followed by an @
and the name of the machine itself.
The ~
indicates that you are in your home directory.
The $
signals the end of the command prompt (if you are logged in as root you may see a #
instead of a $
).
Where am I? The working directory - pwd
The working directory is also known as the current directory. It represents the directory you are currently in, in the command line. You can imagine a graphical file system with many different folders, each themselves containing more subfolders and files. Whenever you are in the terminal, you are “in” a particular directory (or folder) somewhere within this file system tree.
To find out where you are (i.e. what your working directory is), use the pwd
command. It stands for print working directory. Type it into your terminal and hit enter:
pwd
You see an output that prints your current working directory to the screen, for example:
/home/username
Moving between directories - cd
You can move between directories with the cd
command. It stands for change directory.
-
Go one directory “upwards” in the file system, i.e. a step closer to the root, by using the command
cd
followed by..
:cd .. -
Repeat the
pwd
command to check your new working directory:pwdThe working directory now displays, for example:
/homeTipIf you were already in the root directory and tried to do
cd ..
you will not change directories, as there is no upward level to move to. -
Go one directory “downwards” in the file system, i.e. into a subdirectory of your current directory, by using the command
cd
followed by the directory name. Assuming you are now in thehome
directory, use the command below replacingusername
with your own username, to move back into the directory where you started:cd username
Listing the contents of a directory - ls
You can use the ls
command to list the contents of your working directory. It stands for list.
-
List the subdirectories and files in your current working directory with
ls
:lsThe output will depend on the contents of your particular directory, but you may see something like the following:
Documents Music TemplatesDesktop Pictures config.txtDownloads Public Videos -
Practice the
cd
command again by changing the directory into one of the subdirectories listed, checking the contents withpwd
, and then usingcd ..
to change back to the upper directory again:cd Documents- You change into the Documents directory.
pwd- You see an output showing the contents of the Documents directory.
cd ..- You change back into the Documents directory.
Make a new directory - mkdir
You can make a new directory with the mkdir
command. It stands for make directory.
-
Make a new directory with
mkdir
followed by the name of the directory you want to create:mkdir my-directory -
Use
ls
to confirm that the directory is created:lsYou see
my-directory
that you just created, listed in the contents of the current working directory. -
Move into the directory you just created:
cd my-directory
Make a new file - touch
You can make a new file, without any content, with the touch
command.
-
Make a new file called
my-text-file.txt
with the touch command:touch my-text-file.txt -
Use
ls
to confirm that the file is created:lsThe filename should now display among the rest of the output.
Edit a text file - nano
There are many different programs you can use to edit text in the command line of Linux. Some of these include Vim, GNU Emacs and ne. However, for this tutorial, we will use nano. It is pre-installed on most Linux distributions, and relatively easy to use for beginners.
-
Make sure you are in the same working directory as the text file you created in the previous step, and then open the text file with nano:
nano my-text-file.txtNano opens the file and you see a screen like the following:
The information at the bottom tells you the keyboard shortcuts for different actions within the file. Depending on your keyboard, a command such as
CTRL + X
will exit the file and take you back to the command line, whereas you can useCTRL + K
to cut text, etc. -
Enter some text into the file, for example:
Hello world!This is my first text file in Linux. -
Use
CTRL+O
to save (“write out”) the file, and hit enter when asked to confirm. -
Use
CTRL+X
to exit the file and go back to the command line.
Display the contents of a file - cat
You can display the contents of a file on the command line, without opening the file itself, using the cat
command. It stands for concatenate. This is because it is frequently used to concatenate the contents of multiple files together. However, here we will just look at its most simple, basic usage.
-
Make sure you are in the same working directory as the text file you created in the previous step, and display its contents with
cat
:cat my-text-file.txtThe contents of your text file display on the command line:
Hello world!This is my first text file in LinuxYou can also use
cat
to put the contents of the text file, into a new text file, as we see next. -
Copy the contents of
my-text-file.txt
into a new text file calledanother-text-file.txt
, with the following command:cat my-text-file.txt > another-text-file.txt -
Display the contents of the new text file:
cat another-text-file.txtThe contents of the new text file display on the command line. Of course, the contents are identical, as it is simply a copy of the first file:
Hello world!This is my first text file in Linux
Moving or copying files and directories - mv and cp
You can move files and directories with the mv
(move) and cp
(copy) commands.
-
Create a new directory called dir-2 with the following command, as we saw earlier:
mkdir dir-2 -
Make sure you are in the same working directory as the file
my-text-file.txt
that you previously created, and then copy into the directory created in step 1 with the following command:cp my-text-file.txt dir-2 -
Use
cd
andlist
to move into the new directory and check that the text file has been copied:cd dir-2lsYou should see the name of the file (
my-text-file.txt
) when displaying the directory contents withls
. -
Use
cd
to move back to the previous directory, and check that the original text file still exists there too:cd ..lsYou should see the name of the file (
my-text-file.txt
) when displaying the directory contents withls
. Thecp
simply made a new copy for the new folder. -
Make a new text file with the following command. Notice that here instead of using
touch
to create an empty file, we usenano
to directly create and edit the new file:nano second-text-file.txtNano opens the empty file.
-
Add the following text to the file, then save and exit as shown before:
This is a new text file.I'm a Linux pro now. -
Use
mv
to movesecond-text-file.txt
into thedir-2
directory you previously created:mv second-text-file.txt dir-2 -
List contents of the current directory, to see that
second-text-file.txt
is no longer there:ls -
Use the following commands to change your working directory to
dir-2
and see thatsecond-text-file.txt
has been moved there:cd dir-2lsTipA note on file paths
In the examples above, we can simply use the names of the files and the directories in our commands, because we are in the current working directory that contains those files and directories.
If our current working directory was something else, we would need to specify the path of the file to move or copy, and the path of the directory to move it to. Remember that you can use
pwd
to display the path of your current working directory at any time, as a helper.Take the example that my working directory was
/home/username/documents/
, but the file I wanted to move was in/home/username/downloads/text-files
, and the target directory to move it to was/home/username/recipes
, I can use the following command:mv /home/username/downloads/text-files/my-text-file.txt /home/username/recipesThis principle does not just apply to the
mv
command, but all commands. For example, you can usecd
+ a path to a directory, to navigate straight from one working directory to another, even if they are not “adjacent”.
Deleting files and directories - rm
You can remove files and directories with the rm
(remove) command.
-
Navigate to the directory where
my-text-file.txt
is located, and delete the file with the following command:rm my-text-file.txt -
With
cd
, navigate to the directory where you createddir-2
(do not go intodir-2
, stay in its parent directory).The command to remove the directory depends on whether or not it is empty.
- If there are no files in the directory use
rm -d dir-2
- If there are files (or other directories) in the directory, use
rm -r dir-2
- If there are no files in the directory use
-
Delete the
dir-2 directory
(which we presume still has other files inside):rm -r dir-2TipThe
-r
part of this command is known as a flag or option. Flags are used to modify the behavior of a command slightly. Nearly all commands, includingls
,cat
,mkdir
etc. have flags available to use.Flags typically take the form of either one dash followed by a single letter (e.g.
-r
) or two dashes followed by a word (e.g.--recursive
). Often both flag forms are available for the same option.To find out about the flags that can be used with each command and what they can do, run
<command> --help
in your terminal, replacing<command>
by the command you want help with, e.g.cat --help
,rm --help
etc.
Running commands as the superuser - sudo
When you first install Linux, the only user that exists is root, which has inherent “superuser” administrative powers. It is not generally advisable to stay permanently logged in as the superuser, for reasons of security. Instead, you should create a “normal” user account and use that when carrying out operations on your machine.
But as a “normal” user, you will not have permission for everything, such as updating and upgrading software. To carry out this kind of operation, you will need to prefix your command with sudo
(superuser do).
You are usually prompted to enter a password when you enter a sudo
command, to ensure that you have the right permissions. After you have entered your password once, you will not be asked to enter it on subsequent sudo
commands for a little while.
Carry on to the next section to see how to update and upgrade your system’s software, using sudo
.
Updating the system and installing new applications - apt
You can use apt
, the command for the Advanced Package Tool, to install, update, delete, and manage software packages on Linux systems like Ubuntu and Linux Mint.
In this context, “package” essentially refers to software. On Linux, software is usually built as a package, distributed across remote repositories, and managed by users like you via package managers on their local machines.
You need to use sudo for these commands.
-
Update the software packages on your system with the following command:
sudo apt updateAPT will fetch the latest information (about versions, indexes, etc) for packages on your system.
-
Upgrade the software packages on your system with the following command:
sudo apt upgradeAPT will use the information it gained in the previous step, to now actually fetch the new versions of the machine’s packages and bring everything up to date.
You will probably see a message like this:
After this operation, 19,5 kB of additional disk space will be used.Do you want to continue? [Y/n]Hit
y
on your keyboard and then Enter, to continue.Your system’s software is updated and upgraded.
-
Install a new software package on your system. In this example, we install the text editor
ne
:sudo apt install neThe
ne
packages are installed. -
Open
ne
by typing its name:neThe text editor opens. You can play around with this text editor, or type
CTRL+Q
to exit.
Creating and logging into new user accounts - adduser, usermod, and su
As previously mentioned, when you first install Linux, the only user that exists is root, which has inherent “superuser” administrative powers. It is not generally advisable to stay permanently logged in as the superuser, for reasons of security. Instead, you should create a “normal” user account and use that when carrying out operations on your machine.
When creating user accounts, you need to either be logged in as root
, or else to prefix these commands with sudo
.
-
Create a new user with the
adduser
command. Replace sarah with the username of your choice.sudo adduser k8sYou will be prompted to add a password for this user. Then you will be prompted to add optional information such as first name, surname, and telephone number. You can hit enter to skip adding each optional piece of information. Hit
y
when prompted to confirm and create the user.Adding user `sarah' ...Adding new group `sarah' (1003) ...Adding new user `sarah' (1003) with group `sarah' ...Creating home directory `/home/sarah' ...Copying files from `/etc/skel' ...New password:Retype new password:passwd: password updated successfullyChanging the user information for sarahEnter the new value, or press ENTER for the defaultFull Name []: SarahRoom Number []: 3Work Phone []:Home Phone []:Other []:Is the information correct? [Y/n] yYou can see that a home directory has been automatically created for the new user.
-
Give the new user sudo privileges, so that they can carry out actions like updating software with the
sudo
prefix. This is done via the usermod command (user modification):sudo usermod -aG sudo sarah -
Log in as the new user with the
su
(switch user) command:su sarahTipYou can use the command
cd ~
to change your working directory to the new user’s home directory.~
is a special shortcut in Linux to signify the home directory.
Linux commands summary and cheatsheet
The table below provides a summary of everything we covered in this tutorial.
Command | |
---|---|
Print your working directory | pwd |
Move one directory ‘upwards’ | cd .. |
Move into a directory that’s inside your working directory | cd name-of-directory |
Move into a directory that’s elsewhere | cd /path/name-of-directory |
Move into the home directory | cd ~ |
List the contents of a directory | ls |
Make a new directory | mkdir name-of-directory |
Make a new empty file | touch name-of-file |
Open nano to edit a text file | nano name-of-file |
Display the contents of a file | cat name-of-file |
Copy the contents of a file into a new file | cat my-file.txt > new-file.txt |
Create a copy of a file and put it in a specific directory | cp my-file.txt target-directory |
Move a file into a specific directory | mv my-file.txt target-directory |
Delete a file | rm name-of-file |
Delete an empty directory | rm -d name-of-directory |
Delete a non-empty directory | rm -r name-of-directory |
Run a command as the superuser | sudo command |
Update the software packages on your system | sudo apt update |
Upgrade the software packages on your system | sudo apt upgrade |
Create a new user | sudo adduser name-of-user |
Give a user sudo privileges | sudo usermod -aG sudo name-of-user |
Log in as a different user | su name-of-user |