Basic Linux CLI commands course for beginners based on Debian Part 1
Basic Command Syntax
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 behavior 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 behavior 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!
Linux is Open Source. Linux is developed by a community, you view and contribute to the source code.
Options
command [options…] [arguments…]
Options can be used to alter the behavior 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.
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:
Filesystem system hierarchy showing sysadmin directory at the top,
including highlighted Documents directory underneath as well as directories underneath the Documents directory,
including the School directory. The highlighted Art directory is located underneath the School 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:
Recommended by LinkedIn
sysadmin@localhost:~/Documents/School$ cd ~
Section header image
Listing Files
The ls command is used to list the contents of a directory. You've already seen it used a few times before in examples,
but this page will help ensure you are comfortable with its use.
ls [OPTIONS] [FILE]
By default, when the ls command is used with no options or arguments, it will list the files in the current directory:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
To learn the details about a file, such as the type of file, the permissions, ownerships or the timestamp,
perform a long listing using the -l option to the ls command. Below, a listing of the /var/log directory is used as an example,
since it provides a variety of output:
sysadmin@localhost:~$ ls -l /var/log/
total 832
-rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt
-rw-r----- 1 syslog adm 380 Jul 28 03:45 auth.log
-rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log
-rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp
-rw-r----- 1 syslog adm 324 Jul 28 03:45 cron.log
-rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg
-rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log
-rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog
drwxr-xr-x 2 root root 4096 Jun 30 06:53 fsck
-rw-r----- 1 syslog adm 106 Jul 28 03:45 kern.log
-rw-rw-r-- 1 root utmp 292584 Jul 28 03:45 lastlog
-rw-r----- 1 syslog adm 18703 Jul 28 03:46 syslog
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
-rw-rw-r-- 1 root utmp 384 Jul 28 03:45 wtmp
Each line corresponds to a file contained within the directory. The information can be broken down into fields separated by spaces. The fields are as follows:
File Type
-rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions.
The file types are:
Symbol File Type Description
d directory A file used to store other files.
- regular file Includes readable files, images files, binary files, and compressed files.
l symbolic link Points to another file.
s socket Allows for communication between processes.
p pipe Allows for communication between processes.
b block file Used to communicate with hardware.
c character file Used to communicate with hardware.
The first file alternatives.log is a regular file -, while the second file apache2 is a directory d.
Permissions
drwxr-xr-x 1 root root 0 Apr 11 21:58 upstart
Permissions indicate how certain users can access a file. Keep reading to learn more about permissions.
Hard Link Count
-rw-r----- 1 syslog adm 23621 Aug 23 15:17 auth.log
This number indicates how many hard links point to this file. Hard links are beyond the scope of this module,
but are covered in the NDG Linux Essentials course.
User Owner
-rw-r----- 1 syslog adm 416 Aug 22 15:43 kern.log
User syslog owns this file. Every time a file is created, the ownership is automatically assigned to the user who created it.
Group Owner
-rw-rw-r-- 1 root utmp 292584 Aug 20 18:44 lastlog
Indicates which group owns this file
File Size
-rw-r----- 1 syslog adm 1087150 Aug 23 15:17 syslog.1
Directories and larger files may be shown in kilobytes since displaying their size in bytes would present a very large number.
Therefore, in the case of a directory, it might actually be a multiple of the block size used for the file system.
Block size is the size of a series of data stored in the filesystem.
Timestamp
drwxr-xr-x 1 root root 32 Jul 17 03:36 fsck
This indicates the time that the file's contents were last modified.
Filename
-rw-r--r-- 1 root root 47816 Jul 17 03:36 bootstrap.log
The final field contains the name of the file or directory.
Consider This
In the case of symbolic links, a file that points to another file, the link name will be displayed along with an arrow and the pathname of the original file.
lrwxrwxrwx. 1 root root 22 Nov 6 2012 /etc/grub.conf -> ../boot/grub/grub.conf
Symbolic links are beyond the scope of this module, but are covered in the NDG Linux Essentials course.
Sorting
By default the output of the ls command is sorted alphabetically by filename. It can sort by other methods as well.
Follow Along
The options in examples below will be combined with the -l option so the relevant details of the files are displayed.
Notice fields corresponding to the search option.
The -t option will sort the files by timestamp:
sysadmin@localhost:~$ ls -lt /var/log
total 840
-rw-r----- 1 syslog adm 27014 Jul 28 00:10 syslog
-rw-r----- 1 syslog adm 380 Jul 27 23:10 auth.log
-rw-rw-r-- 1 root utmp 292584 Jul 27 23:10 lastlog
-rw-rw-r-- 1 root utmp 384 Jul 27 23:10 wtmp
-rw-r----- 1 syslog adm 324 Jul 27 23:10 cron.log
-rw-r----- 1 syslog adm 106 Jul 27 23:10 kern.log
drwxr-xr-x 2 root root 4096 Jun 30 06:56 fsck
-rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log
-rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg
-rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog
-rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt
-rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log
-rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
The -S option will sort the files by file size:
sysadmin@localhost:~$ ls -l -S /var/log
total 840
-rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log
-rw-rw-r-- 1 root utmp 292584 Jul 27 23:10 lastlog
-rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg
-rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log
-rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog
-rw-r----- 1 syslog adm 27014 Jul 28 00:10 syslog
-rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt
drwxr-xr-x 2 root root 4096 Jun 30 06:56 fsck
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
-rw-rw-r-- 1 root utmp 384 Jul 27 23:10 wtmp
-rw-r----- 1 syslog adm 380 Jul 27 23:10 auth.log
-rw-r----- 1 syslog adm 324 Jul 27 23:10 cron.log
-rw-r----- 1 syslog adm 106 Jul 27 23:10 kern.log
-rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp
The -r option will reverse the order of any type of sort. Notice the difference when it is added to the previous example:
sysadmin@localhost:~$ ls -lSr /var/log
total 840
-rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp
-rw-r----- 1 syslog adm 106 Jul 27 23:10 kern.log
-rw-r----- 1 syslog adm 324 Jul 27 23:10 cron.log
-rw-r----- 1 syslog adm 380 Jul 27 23:10 auth.log
-rw-rw-r-- 1 root utmp 384 Jul 27 23:10 wtmp
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
drwxr-xr-x 2 root root 4096 Jun 30 06:56 fsck
drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
-rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log
-rw-r----- 1 syslog adm 27014 Jul 28 00:10 syslog
-rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog
-rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log
-rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg
-rw-rw-r-- 1 root utmp 292584 Jul 27 23:10 lastlog
-rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log
The numbers in file size field switch from descending to ascending.
Used alone the -r option with list the files in reverse alphabetical order:
sysadmin@localhost:~$ ls -r /var/log
wtmp lastlog faillog cron.log auth.log alternatives.log
upstart kern.log dpkg.log btmp apt
syslog fsck dmesg bootstrap.log apache2
Administrative Access
There are many Linux commands which deal with sensitive information like passwords, system hardware, or otherwise operate under other exceptional circumstances.
Preventing regular users from executing these commands helps to protect the system. Logging in as the root user provides administrative access,
allowing for the execution of some of the privileged commands.
The su Command
su OPTIONS USERNAME
The su command allows you to temporarily act as a different user. It does this by creating a new shell.
The shell is simply a text input console that lets you type in commands. By default, if a user account is not specified,
the su command will open a new shell as the root user, which provides administrative privileges.
Follow Along
Utilizing the login shell option is recommended, as the login shell fully configures the new shell with the settings of the new user.
This option can be specified one of three ways:
su -
su -l
su --login
After executing the su command, a password is required. On our virtual machines, the password for both the root and sysadmin accounts is netlab123.
If you ever forget, it is displayed every time a new virtual machine is started. As a security measure, the password will not be visible as it is typed.
sysadmin@localhost:~$ su -
Password:
root@localhost:~#
Note the command prompt has changed to reflect that you are now logged in as the root user. To logout and return to the sysadmin account, use the exit command.
Note the prompt changes back:
root@localhost:~# exit
exit
sysadmin@localhost:~$
To avoid executing any sensitive commands, we’ve configured the Steam Locomotive command, the sl command, to require administrative access.
If the command is executed as sysadmin, it fails:
sysadmin@localhost:~$ sl
sl: Permission denied
Use the su command to switch to the root account and execute the sl command with administrative access:
sysadmin@localhost:~$ su -
Password:
root@localhost:~# sl
(@@) ( ) (@) ( ) @@ () @ O @
( )
(@@@@)
( )
(@@@)
==== ________ ___________
_D _| |_______/ \__I_I_____===__|_________|
|(_)--- | H\________/ | | =|___ ___| _________________
/ | | H | | | | ||_| |_|| _|
| | | H |__--------------------| [___] | =|
| ________|___H__/__|_____/[][]~\_______| | -|
|/ | |-----------I_____I [][] [] D |=======|____|__________________
__/ =| o |=-~~\ /~~\ /~~\ /~~\ ____Y___________|__|____________________
|/-=|___|= || || || |_____/~\___/ |_D__D__D_| |_D__
\_/ \_O=====O=====O=====O/ \_/ \_/ \_/ \_/
Use the exit command again to return to the sysadmin account.
root@localhost:~# exit
exit
sysadmin@localhost:~$