NavigationContentFooter
Jump toSuggest an edit

First steps with the Linux command line

Reviewed on 25 November 2024Published on 16 November 2023
  • 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.

Tip

If you are just looking for a quick recap and cheatsheet of Linux commands you are already familiar with, skip to the last section.

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:~$
Tip

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.

  1. Go one directory “upwards” in the file system, i.e. a step closer to the root, by using the command cd followed by ..:

    cd ..
  2. Repeat the pwd command to check your new working directory:

    pwd

    The working directory now displays, for example:

    /home
    Tip

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

  3. 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 the home directory, use the command below replacing username 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.

  1. List the subdirectories and files in your current working directory with ls:

    ls

    The output will depend on the contents of your particular directory, but you may see something like the following:

    Documents Music Templates
    Desktop Pictures config.txt
    Downloads Public Videos
  2. Practice the cd command again by changing the directory into one of the subdirectories listed, checking the contents with pwd, and then using cd .. 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.

  1. Make a new directory with mkdir followed by the name of the directory you want to create:

    mkdir my-directory
  2. Use ls to confirm that the directory is created:

    ls

    You see my-directory that you just created, listed in the contents of the current working directory.

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

  1. Make a new file called my-text-file.txt with the touch command:

    touch my-text-file.txt
  2. Use ls to confirm that the file is created:

    ls

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

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

    Nano 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 use CTRL + K to cut text, etc.

  2. Enter some text into the file, for example:

    Hello world!
    This is my first text file in Linux.
  3. Use CTRL+O to save (“write out”) the file, and hit enter when asked to confirm.

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

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

    The contents of your text file display on the command line:

    Hello world!
    This is my first text file in Linux

    You can also use cat to put the contents of the text file, into a new text file, as we see next.

  2. Copy the contents of my-text-file.txt into a new text file called another-text-file.txt, with the following command:

    cat my-text-file.txt > another-text-file.txt
  3. Display the contents of the new text file:

    cat another-text-file.txt

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

  1. Create a new directory called dir-2 with the following command, as we saw earlier:

    mkdir dir-2
  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
  3. Use cd and list to move into the new directory and check that the text file has been copied:

    cd dir-2
    ls

    You should see the name of the file (my-text-file.txt) when displaying the directory contents with ls.

  4. Use cd to move back to the previous directory, and check that the original text file still exists there too:

    cd ..
    ls

    You should see the name of the file (my-text-file.txt) when displaying the directory contents with ls. The cp simply made a new copy for the new folder.

  5. Make a new text file with the following command. Notice that here instead of using touch to create an empty file, we use nano to directly create and edit the new file:

    nano second-text-file.txt

    Nano opens the empty file.

  6. 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.
  7. Use mv to move second-text-file.txt into the dir-2 directory you previously created:

    mv second-text-file.txt dir-2
  8. List contents of the current directory, to see that second-text-file.txt is no longer there:

    ls
  9. Use the following commands to change your working directory to dir-2 and see that second-text-file.txt has been moved there:

    cd dir-2
    ls
    Tip

    A 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/recipes

    This principle does not just apply to the mv command, but all commands. For example, you can use cd + 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.

  1. Navigate to the directory where my-text-file.txt is located, and delete the file with the following command:

    rm my-text-file.txt
  2. With cd, navigate to the directory where you created dir-2 (do not go into dir-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
  3. Delete the dir-2 directory (which we presume still has other files inside):

    rm -r dir-2
    Tip

    The -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, including ls, 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.

  1. Update the software packages on your system with the following command:

    sudo apt update

    APT will fetch the latest information (about versions, indexes, etc) for packages on your system.

  2. Upgrade the software packages on your system with the following command:

    sudo apt upgrade

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

  3. Install a new software package on your system. In this example, we install the text editor ne:

    sudo apt install ne

    The ne packages are installed.

  4. Open ne by typing its name:

    ne

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

  1. Create a new user with the adduser command. Replace sarah with the username of your choice.

    sudo adduser k8s

    You 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 successfully
    Changing the user information for sarah
    Enter the new value, or press ENTER for the default
    Full Name []: Sarah
    Room Number []: 3
    Work Phone []:
    Home Phone []:
    Other []:
    Is the information correct? [Y/n] y

    You can see that a home directory has been automatically created for the new user.

  2. 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
  3. Log in as the new user with the su (switch user) command:

    su sarah
    Tip

    You 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 directorypwd
Move one directory ‘upwards’cd ..
Move into a directory that’s inside your working directorycd name-of-directory
Move into a directory that’s elsewherecd /path/name-of-directory
Move into the home directorycd ~
List the contents of a directoryls
Make a new directorymkdir name-of-directory
Make a new empty filetouch name-of-file
Open nano to edit a text filenano name-of-file
Display the contents of a filecat name-of-file
Copy the contents of a file into a new filecat my-file.txt > new-file.txt
Create a copy of a file and put it in a specific directorycp my-file.txt target-directory
Move a file into a specific directorymv my-file.txt target-directory
Delete a filerm name-of-file
Delete an empty directoryrm -d name-of-directory
Delete a non-empty directoryrm -r name-of-directory
Run a command as the superusersudo command
Update the software packages on your systemsudo apt update
Upgrade the software packages on your systemsudo apt upgrade
Create a new usersudo adduser name-of-user
Give a user sudo privilegessudo usermod -aG sudo name-of-user
Log in as a different usersu name-of-user
Was this page helpful?
API DocsScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCareers
© 2023-2024 – Scaleway