Essential-Linux-Commands 001
Essential-Linux-Commands 001
OnlineTutorial notes
Mr S. D Kanengoni
Masters in International Computer
Science
Class: Short Course
Table of contents
ARGUMENTS.........................................................................................4
OPTIONS..............................................................................................5
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.
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
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
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
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
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
sysadmin@localhost:~$ ls -r
Videos Templates Public Pictures Music Downloads Documents Desktop
ls -l -r
ls -rl
ls -lr
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
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
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 /
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:~$
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$
sysadmin@localhost:~/Documents/$ cd School/Art
sysadmin@localhost:~/Documents/School/Art$
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
sysadmin@localhost:~/Documents/School/Art$ cd ..
sysadmin@localhost:~/Documents/School$
The . Character
The ~ Character
sysadmin@localhost:~/Documents/School$ cd ~