0% found this document useful (0 votes)
6 views

Essential-Linux-Commands 001

This document provides an overview of essential Linux commands and their usage within the Command Line Interface (CLI). It covers topics such as command syntax, arguments, options, and navigation through directories using commands like ls, cd, and pwd. Additionally, it explains the concepts of absolute and relative paths, as well as shortcuts for efficient directory management.

Uploaded by

augustine
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Essential-Linux-Commands 001

This document provides an overview of essential Linux commands and their usage within the Command Line Interface (CLI). It covers topics such as command syntax, arguments, options, and navigation through directories using commands like ls, cd, and pwd. Additionally, it explains the concepts of absolute and relative paths, as well as shortcuts for efficient directory management.

Uploaded by

augustine
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

LINUX FOUNDATION

OnlineTutorial notes

Mr S. D Kanengoni
Masters in International Computer
Science
Class: Short Course
Table of contents

ESSENTIAL LINUX COMMANDS.......................................................3

ARGUMENTS.........................................................................................4

OPTIONS..............................................................................................5

PRINTING WORKING DIRECTORY..............................................................8

CHANGING DIRECTORIES.........................................................................9

Absolute Paths.............................................................................11

Relative Paths..............................................................................12

SHORTCUTS........................................................................................15
Essential Linux Commands
This module deals exclusively with the CLI or Command Line Interface,
rather than a GUI or Graphical User Interface you may be familiar with.
The CLI terminal is a powerful tool that is often the primary method used
to administer small low-power devices, extremely capable cloud
computing servers, and everything in between. A basic understanding of
the terminal is essential to diagnosing and fixing most Linux based
systems. Since Linux has now become so ubiquitous, even those who plan
on working primarily with systems not utilizing the Linux kernel can
benefit from having a basic understanding of the terminal.

What is a command? A command is a software program that when


executed on the CLI (command line interface), performs an action on the
computer. When you type in a command, a process is run by the
operating system that can read input, manipulate data and produce
output. A command runs a process on the operating system, which then
causes the computer to perform a job.

To execute a command, the first step is to type the name of the


command. Click in the terminal on the right. Type ls and hit Enter. The
result should resemble the example below:

sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos

The name of the command is often based on what it does or what the
developer who created the command thinks will best describe the
command's function. For example, the ls command displays a listing of
information about files. Associating the name of the command with
something mnemonic for what it does may help you to remember
commands more easily.
Consider This
Every part of the command is normally case-sensitive, so LS is incorrect and will fail, but ls is
correct and will execute.
sysadmin@localhost:~$ LS
-bash: LS: command not found

Most commands follow a simple pattern of syntax:

command [options…] [arguments…]


In other words, you type a command, followed by
any options and/or arguments before pressing the Enter key.
Typically, options alter the behaviour of the command and arguments are
items or values for the command to act upon.

Although there are some commands in Linux that aren’t entirely


consistent with this syntax, most commands use this syntax or something
similar.

In the example above, the ls command was executed without any options
or arguments. When this is the case, it’s default behaviour is to return a
list of files contained within the current directory.

sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos

Arguments

command [options…] [arguments…]

An argument can be used to specify something for the command to act


upon. The ls command can be given the name of a directory as an
argument, and it will list the contents of that directory. In the next
example, the Documents directory will be used as an argument:

sysadmin@localhost:~$ ls Documents
School alpha-second.txt food.txt linux.txt os.csv
Work alpha-third.txt hello.sh longfile.txt people.csv
adjectives.txt alpha.txt hidden.txt newhome.txt profile.txt
alpha-first.txt animals.txt letters.txt numbers.txt red.txt

The resulting output is a list of files contained with


the Documents directory.

Because Linux is open source, there are some interesting secrets that
have been added by developers. For example, the aptitude command is a
package management tool available on some Linux distributions. This
command will accept moo as an argument:
sysadmin@localhost:~$ aptitude moo
There are no Easter Eggs in this program.

There is more to this trick than meets the eye, keep reading!

Options

command [options…] [arguments…]

Options can be used to alter the behaviour of a command. On the


previous page, the ls command was used to list the contents of a
directory. In the following example, the -l option is provided to
the ls command, which results in a "long display" output, meaning the
output gives more information about each of the files listed:

sysadmin@localhost:~$ ls -l
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Desktop
drwxr-xr-x 4 sysadmin sysadmin 4096 Aug 4 20:58 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Downloads
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Videos

Often the character is chosen to be mnemonic for its purpose, like


choosing the letter l for long or r for reverse. By default, the ls command
prints the results in alphabetical order, so adding the -r option will print
the results in reverse alphabetical order.

sysadmin@localhost:~$ ls -r
Videos Templates Public Pictures Music Downloads Documents Desktop

Multiple options can be used at once, either given as separate options as


in -l -r or combined like -lr. The output of all of these examples would be
the same:

ls -l -r
ls -rl
ls -lr

As explained above, -l gives a long listing format while -r reverses the


listing. The result of using both options is a long listing given in reverse
order:

sysadmin@localhost:~$ ls -l -r
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Aug 4 20:58 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Desktop
sysadmin@localhost:~$ ls -rl
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Aug 4 20:58 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Aug 4 20:58 Desktop

Ultimately, commands can use many combinations of options and


arguments. The possibilities for each command will be unique. Remember
the aptitude easter egg?

sysadmin@localhost:~$ aptitude moo


There are no Easter Eggs in this program.
It is possible to alter the behavior of this command using options. See
what happens when the -v (verbose) option is added:

sysadmin@localhost:~$ aptitude -v moo


There really are no Easter Eggs in this program.

By combining multiple -v options, we can get a variety of responses:

sysadmin@localhost:~$ aptitude -vv moo


Didn't I already tell you that there are no Easter Eggs in this program?
sysadmin@localhost:~$ aptitude -vvv moo
Stop it!

Remember multiple options can be denoted separately or combined:

aptitude -v -v moo
aptitude -vv moo

Keep adding -v options to see how many unique responses you can get!
Printing Working Directory
In order to discover where you are currently located within the filesystem,
the pwd command can be used. The pwd command prints the working
directory, your current location within the filesystem:

pwd [OPTIONS]

Consider This
Don't turn on your printer just yet! In the early days of computing the command line output would
be sent to physical printers. This method was replaced by video displays which could display
information more quickly. We still use the word print even though the output is just being
displayed on your screen.

sysadmin@localhost:~$ pwd
/home/sysadmin

The output of the above command indicates that the user is currently in
their home folder, shown in the filesystem below.

Consider This
Notice our virtual machines employ a prompt that displays the current working directory,
emphasized with the color blue. In the first prompt above, the blue ~ is equivalent
to /home/sysadmin, representing the user's home directory.
sysadmin@localhost:~$

After changing directories (we will learn how to do this in the next section), the new location can
also be confirmed in the new prompt, again shown in blue.
sysadmin@localhost:/etc/calendar$
Changing Directories
Files are used to store data such as text, graphics and programs.
Directories are a type of file used to store other files–they provide a
hierarchical organizational structure. The image below shows an
abbreviated version of the filesystem structure on the virtual machines.

When you start a fresh virtual machine, either by opening the course or
after using the reset button, you are logged in as the sysadmin user in

your home directory, highlighted below:

To navigate the filesystem structure, use the cd (change directory) command to change
directories.

cd [options] [path]

If you look back at the graphic above, you will see the Documents directory
is located within the home directory, where you are currently located. To
move to the Documents directory, use it as argument to the cd command:
sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$

Directories are equivalent to folders on Windows and Mac OS. Like these
more popular operating systems, a Linux directory structure has a top
level. It is not called "My Computer", but rather the root directory and it is
represented by the / character. To move to the root directory, use
the / character as the argument to the cd command.

sysadmin@localhost:~$ cd /

The argument to the cd command is more than just the name of a


directory, it is actually a path. A path is a list of directories separated by
the / character. For example, /home/sysadmin is the path to your home
directory:

If you think of the filesystem as a map, paths are the step-by-step


directions; they can be used to indicate the location of any file within the
filesystem. There are two types of paths: absolute and relative. Absolute
paths start at the root of the filesystem, relative paths start from your
current location.
Absolute Paths
An absolute path allows you to specify the exact location of a directory. It
always starts at the root directory; therefore, it always begins with
the / character. The path to the home directory /home/sysadminis an
absolute path. The path begins at the root / directory, moves into
the home directory, and then into the sysadmin directory. Following this
path on a graphical user interface (GUI) like your home computer would
look something like this:

Use this path as an argument to the cd command to move back into the
home directory for the sysadmin user.

sysadmin@localhost:/$ cd /home/sysadmin
sysadmin@localhost:~$

No output means the command succeeded. Go ahead and confirm this


using the pwd command:

sysadmin@localhost:~$ pwd
/home/sysadmin
Relative Paths
A relative path gives directions to a file relative to your current location in
the filesystem. Relative paths do not start with the / character, they start
with the name of a directory. Take another look at the first cd command
example. The argument is an example of the simplest relative path: the
name of a directory in your current location.

sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$

The image below shows a map of the files contained within


the sysadmin directory. You are currently in the Documents directory and
want to move to the Art directory:
A relative path begins in from with the current directory, however you
don't include it in the path. The first step would be to move into
the School directory, and then move into the Art directory. Use
the / character to separate the directory names and the
result School/Art is a relative path from the Documents directory to
the Art directory:

Use the relative path as an argument to the cd command to move into


the Art directory.

sysadmin@localhost:~/Documents/$ cd School/Art
sysadmin@localhost:~/Documents/School/Art$

Use the pwd command to confirm the change:

sysadmin@localhost:~/Documents/School/Art$ pwd
/home/sysadmin/Documents/School/Art
Consider This
The output of the pwd command is the absolute path to the Art directory.

Consider This
In the example above the cd command followed the School/Art path:
cd School/Art

A path can also be broken down into multiple cd commands. The following
set of commands would achieve the same results:

cd School
cd Art
Shortcuts
The .. Characters

Regardless of which directory you are in, .. always represents one


directory higher relative to the current directory, sometimes referred to as
the parent directory. To move from the Art directory back to
the School directory:

sysadmin@localhost:~/Documents/School/Art$ cd ..
sysadmin@localhost:~/Documents/School$

The . Character

Regardless of which directory you are in, the . character always


represents your current directory. For the cd this shortcut is not very
useful, but it will come in handy for commands covered in subsequent
sections.

The ~ Character

The home directory of the current user is represented by the ~ character.


As stated above, you always begin as the sysadmin user, whose home is
located at /home/sysadmin. To return to your home directory at any time
execute the following command:

sysadmin@localhost:~/Documents/School$ cd ~

You might also like