Linux Essentials for
Cybersecurity
by William “Bo” Rothwell
and Denise Kinsey
Chapter 2: Working on the Command Line
Objectives
Manage files and directories
Use shell features such as shell variables
Be able to re-execute previous commands
using the shell feature called history
Identify regular expressions and know how to
use them with commands like find, grep, and
sed
Manage file-compression utilities
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 2
File Management
Every storage location is accessible under
the top-level directory (root)
The root directory is symbolized by /
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 3
Filesystem Hierarchy Standard (FHS)
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 4
Filesystem Hierarchy Standard (cont.)
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 5
Command Execution
Type the command and press Enter
An option is a predefined value that changes
the behavior of the command
Options are a single character value that follows a
hyphen, such as –a or –z
Some can be combined, as in –az
Some newer commands accept word options,
such as --long; word options start with two
hyphens
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 6
Command Execution
An argument is a parameter that appears
after the command
Example: To use the cd command to change to
the bin directory: cd /bin
To execute multiple commands on a single
line, separate them with semi-colons and
press Enter only after the last one
Example: pwd ; date ; ls
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 7
Common Commands
pwd
Displays the current directory
cd
Changes to another directory
Accepts a single argument, the desired directory
Example: cd /etc
file
Reports the type of content in a file
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 8
The ls Command
ls (lowercase L, not a capital I)
Lists the files in a directory
Default is the current directory; accepts an
argument of a different directory’s name
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 9
ls Command Output
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 10
File Globbing
File glob (wildcard): Any character provided
on the command line that represents a
portion of the filename
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 11
More Common Commands
less
Displays large chunks of text data, pausing after
displaying the first page of information
Keys on keyboard enable user to scroll through
the document
Spacebar: one page forward
b: one page back
Enter or down arrow: down one line
Up arrow: up one line
q: return to the shell
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 12
More Common Commands
(cont.)
head
Displays the top part of text data
By default the top ten lines are displayed
tail
Displays the bottom part of text data
By default the bottom ten lines are displayed
mkdir
Creates (makes) a directory
Argument of the new directory’s name
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 13
More Common Commands
(cont.)
cp
Copies files or directories
Syntax: cp [options] file|directory destination
where file|directory is what to copy and destination is
where it should be copied
mv
Moves or renames a file
rm
Removes (deletes) files and directories
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 14
More Common Commands
(cont.)
rmdir
Removes (deletes) empty directories
Command will fail if not empty unless the –r option
is used
touch
Creates an empty file if it doesn’t exist already
Updates the modification and access timestamps
if an existing file is specified
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 15
Shell Variables
HOME
ID
LOGNAME
OLDPWD
PATH
PS1
PWD
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 16
echo Command
Used to display information, such as the
value of a variable
Can also print literal text strings
Special character sequences:
\a Ring terminal bell
\n New line
\t Tab
\\ Single backslash
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 17
set Command
Displays all shell variables and values when
no arguments are used
Can also modify the behavior of the shell
Unset command removes a variable
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 18
PS1 Variable
Defines the primary prompt
Uses special character sequences
\u = current user’s name
\W = current directory
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 19
PATH Variable
Contains a comma-separated list of directory
locations
Example:
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/
home/student/.local/bin:/home/student/bin
The path is searched in that order when a
command is called
To execute a command that is not in the path,
you must use a fully qualified path name
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 20
Environment Variables
To pass variables and their values to other
commands, convert an existing local variable
to an environment variable with export
Example: export NAME
If the variable doesn’t already exist, export
creates it directly as an environment variable
The command export –p displays all
environment variables
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 21
The env Command
Displays environment variables in the current
shell
Local variables are not displayed
Can also temporarily set a variable for the
execution of a command
To unset a variable when executing a
command, use --unset=VAR where VAR is
the variable
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 22
Initialization Files
Logging in starts a login shell
When a user starts a new shell after login, it
is a non-login shell
Which initialization files are executed
depends on whether the shell is a login shell
or a non-login shell
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 23
Alias
A shell feature that allows multiple
commands to be executed by issuing a single
command
Use alias command with no arguments to
display all aliases
Use the unalias command to unset an alias
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 24
Command History
Use history command to view command
history
Limit the number of commands displayed
with an argument
Example: history 5
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 25
Methods for Redirection
Standard input: stdin or STDIN
Standard output: stdout or STDOUT
Standard error: stderr or STDERR
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 26
Piping and Subcommands
Piping
Routes the output of a command to another
command
Uses the “pipe” symbol: |
Example: ls –l /etc | grep “Apr 16”
Subcommands
To use one command’s output as an argument in
another command, place the command within the
$( ) characters
Example: echo “Today is $(date)”
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 27
Regular Expressions (REs)
A character or set of characters designed to
match other characters
Examples:
A dot (.) matches a single character of any type
[a-z] matches any single lowercase character
Basic and extended REs
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 28
Basic REs
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 29
Extended REs
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 30
Other Commands
grep
Searches files for lines that contain a specific
pattern
Example: grep “the” /etc/rsyslog.conf
sed
Edits file data in a non-interactive method
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 31
Compression Commands
tar
Merges multiple files into a single file
gzip
Creates a compressed version of the file
Use gunzip to decompress gzipped files
bzip2
Replaces the original file with the compressed file
xz
Compresses files
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 32
Summary
This chapter focused on essential commands
that all Linux users should know
These commands enable you to navigate the
Linux operating system, manipulate files and
directories, and perform advanced end-user
operations
Master these commands before moving on to
more advanced security-related topics
© 2019 by Pearson Education, Inc. Chapter 2 Working on the Command Line 33