Linux Professional Institute Learning Logo.
Skip to main content
  • Home
    • All Resources
    • LPI Learning Materials
    • Become a Contributor
    • Publishing Partners
    • Become a Publishing Partner
    • About
    • FAQ
    • Contributors
    • Roadmap
    • Contact
  • LPI.org
2.1 Lesson 1
Topic 1: The Linux Community and a Career in Open Source
1.1 Linux Evolution and Popular Operating Systems
  • 1.1 Lesson 1
1.2 Major Open Source Applications
  • 1.2 Lesson 1
1.3 Open Source Software and Licensing
  • 1.3 Lesson 1
1.4 ICT Skills and Working in Linux
  • 1.4 Lesson 1
Topic 2: Finding Your Way on a Linux System
2.1 Command Line Basics
  • 2.1 Lesson 1
  • 2.1 Lesson 2
2.2 Using the Command Line to Get Help
  • 2.2 Lesson 1
2.3 Using Directories and Listing Files
  • 2.3 Lesson 1
  • 2.3 Lesson 2
2.4 Creating, Moving and Deleting Files
  • 2.4 Lesson 1
Topic 3: The Power of the Command Line
3.1 Archiving Files on the Command Line
  • 3.1 Lesson 1
3.2 Searching and Extracting Data from Files
  • 3.2 Lesson 1
  • 3.2 Lesson 2
3.3 Turning Commands into a Script
  • 3.3 Lesson 1
  • 3.3 Lesson 2
Topic 4: The Linux Operating System
4.1 Choosing an Operating System
  • 4.1 Lesson 1
4.2 Understanding Computer Hardware
  • 4.2 Lesson 1
4.3 Where Data is Stored
  • 4.3 Lesson 1
  • 4.3 Lesson 2
4.4 Your Computer on the Network
  • 4.4 Lesson 1
Topic 5: Security and File Permissions
5.1 Basic Security and Identifying User Types
  • 5.1 Lesson 1
5.2 Creating Users and Groups
  • 5.2 Lesson 1
5.3 Managing File Permissions and Ownership
  • 5.3 Lesson 1
5.4 Special Directories and Files
  • 5.4 Lesson 1
  1. Topic 2: Finding Your Way on a Linux System
  2. 2.1 Command Line Basics
  3. 2.1 Lesson 1

2.1 Lesson 1

Certificate:

Linux Essentials

Version:

1.6

Topic:

2 Finding Your Way on a Linux System

Objective:

2.1 Command Line Basics

Lesson:

1 of 2

Introduction

Modern Linux distributions have a wide range of graphical user interfaces but an administrator will always need to know how to work with the command line, or shell as it is called. The shell is a program that enables text based communication between the operating system and the user. It is usually a text mode program that reads the user’s input and interprets it as commands to the system.

There are several different shells on Linux, these are just a few:

  • Bourne-again shell (Bash)

  • C shell (csh or tcsh, the enhanced csh)

  • Korn shell (ksh)

  • Z shell (zsh)

On Linux the most common one is the Bash shell. This is also the one that will be used in examples or exercises here.

When using an interactive shell, the user inputs commands at a so-called prompt. For each Linux distribution, the default prompt may look a little different, but it usually follows this structure:

username@hostname current_directory shell_type

On Ubuntu or Debian GNU/Linux, the prompt for a regular user will likely look like this:

carol@mycomputer:~$

The superuser’s prompt will look like this:

root@mycomputer:~#

On CentOS or Red Hat Linux, the prompt for a regular user will instead look like this:

[dave@mycomputer ~]$

And the superuser’s prompt will look like this:

[root@mycomputer ~]#

Let’s explain each component of the structure:

username

Name of the user that runs the shell

hostname

Name of the host on which the shell runs. There is also a command hostname, with which you can show or set the system’s host name.

current_directory

The directory that the shell is currently in. A ~ means that the shell is in the current user’s home directory.

shell_type

$ indicates the shell is run by a regular user.

# indicates the shell is run by the superuser root.

As we do not need any special privileges, we will use an unprivileged prompt in the following examples. For brevity, we will just use the $ as prompt.

Command Line Structure

Most commands at the command line follow the same basic structure:

command  [option(s)/parameter(s)...]  [argument(s)...]

Take the following command as an example:

$ ls -l /home

Let’s explain the purpose of each component:

Command

Program that the user will run – ls in the above example.

Option(s)/Parameter(s)

A “switch” that modifies the behavior of the command in some way, such as -l in the above example. Options can be accessed in a short and in a long form. For example, -l is identical to --format=long.

Multiple options can be combined as well and for the short form, the letters can usually be typed together. For example, the following commands all do the same:

$ ls -al
$ ls -a -l
$ ls --all --format=long
Argument(s)

Additional data that is required by the program, like a filename or path, such as /home in the above example.

The only mandatory part of this structure is the command itself. In general, all other elements are optional, but a program may require certain options, parameters or arguments to be specified.

Note

Most commands display a short overview of available commands when they are run with the --help parameter. We will learn additional ways to learn more about Linux commands soon.

Command Behavior Types

The shell supports two types of commands:

Internal

These commands are part of the shell itself and are not separate programs. There are around 30 such commands. Their main purpose is executing tasks inside the shell (e.g. cd, set, export).

External

These commands reside in individual files. These files are usually binary programs or scripts. When a command which is not a shell builtin is run, the shell uses the PATH variable to search for an executable file with same name as the command. In addition to programs which are installed with the distribution’s package manager, users can create their own external commands as well.

The command type shows what type a specific command is:

$ type echo
echo is a shell builtin
$ type man
man is /usr/bin/man

Quoting

As a Linux user, you will have to create or manipulate files or variables in various ways. This is easy when working with short filenames and single values, but it becomes more complicated when, for example, spaces, special characters and variables are involved. Shells provide a feature called quoting which encapsulates such data using various kinds of quotes (" ", ' '). In Bash, there are three types of quotes:

  • Double quotes

  • Single quotes

  • Escape characters

For example, the following commands do not act in the same way due to quoting:

$ TWOWORDS="two words"
$ touch $TWOWORDS
$ ls -l
-rw-r--r-- 1 carol carol     0 Mar 10 14:56 two
-rw-r--r-- 1 carol carol     0 Mar 10 14:56 words
$ touch "$TWOWORDS"
$ ls -l
-rw-r--r-- 1 carol carol     0 Mar 10 14:56  two
-rw-r--r-- 1 carol carol     0 Mar 10 14:58 'two words'
-rw-r--r-- 1 carol carol     0 Mar 10 14:56  words
$ touch '$TWOWORDS'
$ ls -l
-rw-r--r-- 1 carol carol     0 Mar 10 15:00 '$TWOWORDS'
-rw-r--r-- 1 carol carol     0 Mar 10 14:56  two
-rw-r--r-- 1 carol carol     0 Mar 10 14:58 'two words'
-rw-r--r-- 1 carol carol     0 Mar 10 14:56  words
Note

The line with TWOWORDS= is a Bash variable that we have created ourselves. We will introduce variables later. This is just meant to show you how quoting affects the output of variables.

Double Quotes

Double quotes tell the shell to take the text in between the quote marks ("...") as regular characters. All special characters lose their meaning, except the $ (dollar sign), \ (backslash) and ` (backquote). This means that variables, command substitution and arithmetic functions can still be used.

For example, the substitution of the $USER variable is not affected by the double quotes:

$ echo I am $USER
I am tom
$ echo "I am $USER"
I am tom

A space character, on the other hand, loses its meaning as an argument separator:

$ touch new file
$ ls -l
-rw-rw-r-- 1 tom students 0 Oct 8 15:18 file
-rw-rw-r-- 1 tom students 0 Oct 8 15:18 new
$ touch "new file"
$ ls -l
-rw-rw-r-- 1 tom students 0 Oct 8 15:19 new file

As you can see, in the first example, the touch command creates two individual files, the command interprets the two strings as individual arguments. In the second example, the command interprets both strings as one argument, therefore it only creates one file. It is, however, best practice to avoid the space character in filenames. Instead, an underscore (_) or a dot (.) could be used.

Single Quotes

Single quotes don’t have the exceptions of the double quotes. They revoke any special meaning from each character. Let’s take one of the first examples from above:

$ echo I am $USER
I am tom

When applying the single quotes you see a different result:

$ echo 'I am $USER'
I am $USER

The command now displays the exact string without substituting the variable.

Escape Characters

We can use escape characters to remove special meanings of characters from Bash. Going back to the $USER environment variable:

$ echo $USER
carol

We see that by default, the contents of the variable are displayed in the terminal. However, if we were to precede the dollar sign with a backslash character (\) then the special meaning of the dollar sign will be negated. This in turn will not let Bash expand the variable’s value to the username of the person running the command, but will instead interpret the variable name literally:

$ echo \$USER
$USER

If you recall, we can get similar results to this using the single quote, which prints the literal contents of whatever is between the single quotes. However the escape character works differently by instructing Bash to ignore whatever special meaning the character it precedes may possess.

Guided Exercises

  1. Split the lines below into the components of command, option(s)/parameter(s) and argument(s):

    • Example: cat -n /etc/passwd

      Command:

      cat

      Option:

      -n

      Argument:

      /etc/passwd

    • ls -l /etc

      Command:

      Option:

      Argument:

    • ls -l -a

      Command:

      Option:

      Argument:

    • cd /home/user

      Command:

      Option:

      Argument:

  2. Find what type the following commands are:

    Example:

    pwd

    Shell builtin

    mv

    External command

    cd

    cat

    exit

  3. Resolve the following commands that use quotes:

    Example:

    echo "$HOME is my home directory"

    echo /home/user is my home directory

    touch "$USER"

    touch 'touch'

Explorational Exercises

  1. With one command and using brace expansion in Bash (review the man page for Bash), create 5 files numbered 1 to 5 with the prefix game (game1, game2, …​).

  2. Delete all 5 files that you just created with just one command, using a different special character (review Pathname Expansion in the Bash man pages).

  3. Is there any other way to make two commands interact with each other? What are those?

​

Summary

In this lab you learned:

  • Concepts of the Linux shell

  • What is the Bash shell

  • The structure of the command line

  • An introduction to quoting

Commands used in the exercises:

bash

The most popular shell on Linux computers.

echo

Output text on the terminal.

ls

List the contents of a directory.

type

Show how a specific command is executed.

touch

Create an empty file or update an existing file’s modification date.

hostname

Show or change a system’s hostname.

Answers to Guided Exercises

  1. Split the lines below into the components of command, option(s)/parameter(s) and argument(s):

    • ls -l /etc

      Command:

      ls

      Option:

      -l

      Argument:

      /etc

    • ls -l -a

      Command:

      ls

      Option:

      -l -a

      Argument:

    • cd /home/user

      Command:

      cd

      Option:

      Argument:

      /home/user

  2. Find what type the following commands are:

    cd

    Shell builtin

    cat

    External command

    exit

    Shell builtin

  3. Resolve the following commands that use quotes:

    touch "$USER"

    tom

    touch 'touch'

    Creates a file named touch

Answers to Explorational Exercises

  1. With one command and using brace expansion in Bash (review the man page for Bash), create 5 files numbered 1 to 5 with the prefix game (game1, game2, …​).

    Ranges can be used to express the numbers from 1 to 5 within one command:

    $ touch game{1..5}
    $ ls
    game1  game2  game3  game4  game5
  2. Delete all 5 files that you just created with just one command, using a different special character (review Pathname Expansion in the Bash man pages).

    Since all files start with game and end in a single character (a number from 1 to 5 in this case), ? can be used as a special character for the last character in the filename:

    $ rm game?
  3. Is there any other way to make two commands interact with each other? What are those?

    Yes, one command could, for example, write data to a file which is then processed by another command. Linux can also collect the output of one command and use it as input for another command. This is called piping and we will learn more about it in a future lesson.

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Next Lesson

2.1 Command Line Basics (2.1 Lesson 2)

Read next lesson

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

LPI is a non-profit organization.

Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. With more than 200,000 certification holders, it's the world’s first and largest vendor-neutral Linux and open source certification body. LPI has certified professionals in over 180 countries, delivers exams in multiple languages, and has hundreds of training partners.

Our purpose is to enable economic and creative opportunities for everybody by making open source knowledge and skills certification universally accessible.

  • LinkedIn
  • flogo-RGB-HEX-Blk-58 Facebook
  • Twitter
  • Contact Us
  • Privacy and Cookie Policy

Spot a mistake or want to help improve this page? Please let us know.

© Copyright 1999-2020 The Linux Professional Institute Inc. All rights reserved.