Command Line Skills
Command Line Interface
● The CLI is a very powerful and fast interface that allows users to complete varous tasks
by issuing commands.
● The CLI provides great control, speed and also the ability to automate tasks.
● The knowledge of the CLI allows a user to be productive with using any Linux distribution.
The Shell
The Shell
● Once a user has entered a command, the terminal then accepts what the user has
typed and passes it to a shell
● A shell is an application that allows users to use the services of an operating
system
● It is named a shell because it is the outermost layer around the operating system
● Bash is the most commonly used shell in Linux
The Shell
The list of the Bash shell popular features:
○ Command line history
○ The ability to recall/reuse the previously typed commands
○ Inline editing
○ The ability to modify a recalled from the history command prior to executing it
○ Scripting
○ Automation tool. Scripts are used to automate the common/repetetive tasks
○ Aliases
○ Aliases are short names that can be used to run the long and complex commands
○ Variables
○ Ability to store values in RAM (memory)
The Shell
● Command line prompt
● The appearance of the command line prompt can be customized
● The default command line prompt contains information about the user and the system
ivanovn@atlas:~$
● The above command line prompt contains the following information:
○ Username (ivanovn)
○ System name (atlas)
○ Current Directory (~, the ~ symbol represents the user’s home
directory)
Commands
Commands
● A command is a program that performs an action on the computer
● To execute a command
● type the name of the command
● And then hit the Enter key to execute the command
● Below is the example of executing the ls command
ivanovn@atlas:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
Commands
● The behaviour of commands can be customized by using options and arguments
○ Options are used to modify the behavior of a command
○ Arguments are used to provide additional data that a command needs to produce the
required result
● The typical format for a command is as follows:
command [options]... [arguments]...
● The square brackets are used to specify that the options and arguments are optional
● The ... is used to specify that more than one option and/or argument can be used
Arguments
Command Arguments
command [options]... [arguments...]
● An argument is a value that must be given to a command so that the command can
produce the result
● For example, the ls command can be given the name of a directory or a file as an
argument:
ivanovn@atlas:~$ ls /home
lost+found oa-adminhome1 oa-adminhome2
● Commands can accept multiple arguments:
ivanovn@atlas:~$ ls /home /etc
Options
Command Options
command [options]... [arguments]...
● Options are used to modify behaviour of a command
● An option is a character with a dash (-) character in front of that character
● Below is an example where the -l option is used to tell the ls command display the long listing
(show additional information)
ivanovn@atlas:~$ ls -l
total 36
drwxr-xr-x 2 ivanovn users 4096 Mar 24 12:31 bin
-rw-r--r-- 1 ivanovn users 220 May 16 14:21 cal.txt
...
Options
● Several options can be applied to a command at the same time. For example, the ls command below
is given two options: -l to show the long list and –r to reverse the order in which the output is shown
ivanovn@atlas:~$ ls -l -r
ivanovn@atlas:~$ ls -lr
● Options are often single letters like –r, -l, -u, -o
● Options can also be words or phrases: --all, --reverse
● Commands can use single letters and complete words for options
○ Single-letter options are preceded by a single dash - character, like the -h option
○ Full-word options are preceded by two dash -- characters
○ -h option is the same as --human-readable option
Command History
● When a command is executed, it is stored in a history file.
● The commands can be retreived from the history file and without the need to retype
the command.
● We can use the Up Arrow and Down Arrow keys retreive the recently executed
commands from the history file.
● To view the entire history list of the typed commands, use the history command:
ivanovn@atlas:~$ history
1 whoami
2 pwd
3 cal 7 2000
...
Command History
● The command that is listed in the history file can be executed by:
● typing an exclamation mark ! character and then the number next to the command: !3
● If the history command is passed a value as an argument, it searches through the history file for
commands that match the passed value
ivanovn@atlas:~$ history ls
3 ls
17 ls /home
80 ls –ar /
● To execute the most recent command use !! and hit Enter
● To execute the most recent version of a specific command, type !command and hit Enter
ivanovn@atlas:~$ !ls
Variables
Variables
● A variable allows to store a value in the RAM (memory).
● Variables must have names and they are stored in memory temporarely.
● There are two types of variables used in the Bash shell:
● Local (shell) variables - used to store values, perform calculations, and etc
● Environment variables – used to control the Linux run time environment
Local Variables
● Local or shell, variables exist only in the current shell
● When the terminal window or shell is closed, all of the local variables are removed
● To create a local variable named var and assigns it a value of Hello:
ivanovn@atlas:~$ var=Hello
● To access and use the value of the variable, use a dollar sign $ character followed by the variable
name.
● The following example uses the echo command to display the content of the previously created $var
variable:
ivanovn@atlas:~$ echo $var
Hello
Environment Variables
● Environment variables
● These variables also called global variables because they are available system-wide and not to a particular
shell.
● There are many environment variables. For example: PATH, HOME, and HISTSIZE variables.
● The command in the example below displays the value of the HISTSIZE variable:
ivanovn@atlas:~$ echo $HISTSIZE
1000
● The env command outputs a list of the environment variables.
● The export command is used to turn a local variable into an environment variable.
ivanovn@atlas:~$ export var
ivanovn@atlas:~$ env | grep var
var=Hello
● Variables can be removed using the unset command: unset var
Path Variable
● The PATH variable is a so called search path. It allows the OS to find the executable files for the
commands that we run in the terminal.
● For example, cal is a command. When this command is executed, the /usr/bin/cal executable
file runs and produces the calendar.
● The which command can be used to show wich executable file would run for a given
command: which cal
● The PATH variable lists all the directories where the OS will search for executable files for given
commands.
ivanovn@atlas:~$ echo $PATH
/home/ivanovn/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/g
ames
● If the executable file for a command is not found in any directory listed in the PATH variable, then
the shell returns a command not found error.
Command Types
Command Types
● The type command can be used to determine information about the type of a
command.
type command
● There are four types of commands:
○ Internal commands – these commands are a part of the shell.
○ External commands – thse commands require the executable files to run.
○ Aliases – short names given to long and complex commands.
○ Functions – complex structures that can be used to create new commands or to
change the behavior of the exising commands.
Internal Commands
● These are the shell built-in commands as they are built into the shell itself.
● The cd (change directory) or the echo command are a part of the Bash shell.
● The type command shows that the cd command is an internal command:
ivanovn@atlas:~$ type cd
cd is a shell builtin
External Commands
● External commands are stored in executable files.
● The shell has to use the directories listed in the PATH environment variable to find those
executable files when the command is used.
● The which command searches for the location and the name of an executable file for a
given command by searching the PATH variable:
ivanovn@atlas:~$ which ls
/bin/ls
ivanovn@atlas:~$ which cal
/usr/bin/cal
External Commands
● External commands can be executed by typing the complete path to the executable file for that
command.
ivanovn@atlas:~$ /bin/ls
Desktop Documents Downloads Music Pictures Public Templates Videos
● For external commands, the type command displays the location of the command:
ivanovn@atlas:~$ type cal
cal is /usr/bin/cal
● There could be multiple executable files for a command.
● To display all executable files for a command, use the -a option with the type command:
ivanovn@atlas:~$ type -a echo
echo is builtin
echo is /usr/bin/echo
echo is /bin/echo
Aliases
● An alias can be used to map longer commands to shorter names.
● For example, the command ls -l cann be aliased to l or ll.
● To see the list of existing aliases use the alias command:
ivanovn@atlas:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
Output Omitted...
● The type command can identify aliases to other commands:
ivanovn@atlas:~$ type ll
ll is aliased to `ls -alF'
● A new alias can be created using the alias command: alias mydate=‘cal 7 2000’
● An alias can be removed using the unalias command: unalias mydate
Functions
● Functions can be used to:
○ Create new commands
○ Override the behariour of existing commands
● Aliases and functions are loaded from the configuration files when the shell starts or when
the user logges in.