3
Command Line Fundamentals
3.1 Introduction
In this day and age, it takes a certain level of skill to be alien to technology and as everyone has computers in their pockets, the interactions with them is almost uncountable. However, these interactions are done through what is called a GUI . Devices running on Windows, MacOS, iOS, and Android all use this interface to interact with the user.
i.e., when clicked on an icon, the close, minimize and maximize buttons on the windows etc..
It
must
be
stressed
as
these
visual
components
are
all
for
the
benefit
of
the
user.
While
these
greatly
simplify
tasks
like
photo
editing
or
video
creation,
some
applications
just
completely
omit
the
use
of
GUI
and
instead
use
a
simpler
version
of
it
called
CLI
.
This
is
especially
true
for
servers
One way we interact with these programs that don’t have a GUI is through the CLI . This is a text-based interface where the commands to execute are typed and all actions are shown as text on a terminal screen, whether it is updating a software or moving files around. The environment we use is called a shell, or command-line interpreter, and there are many shells out there.
A list of Shells that can be encountered in industry and academia can be seen in Table 3.1 .
UNIX | Windows |
Bourne
shell (sh )
|
COMMAND.COM, default in Windows 9x and provided for DOS compatibility in 32-bit versions of NT-based Windows via NTVDM. |
Almquist
shell (ash )
|
|
Debian
Almquist
shell (dash )
|
|
Bash
(Unix
shell) (bash )
|
|
Korn
shell (ksh )
|
cmd.exe ,
the
default
command-line
interpreter
of
the
Windows
NT-family
|
Z
Shell (zsh )
|
|
C
shell (csh )
|
Recovery Console |
TENEX
C
shell (tcsh )
|
Windows PowerShell, based on .NET Framework |
Ch
shell (ch )
|
PowerShell, based on .NET Core |
Emacs
shell (eshell )
|
Hamilton C shell, a clone of the Unix C shell |
Friendly
interactive
shell (fish )
|
|
Powershell (pwsh )
|
4NT, a clone of CMD.EXE. |
rc
shell (rc )
|
Take Command, a newer incarnation of 4NT |
Stand-alone
shell (sash )
|
|
Scheme
Shell (scsh )
|
The
command-line
interpreter
was
one
of
the
earliest
ways
of
interacting
with
the
general-purpose
computer,
starting
in
1971
with
the
Thompson
shell
for
UNIX
Bash,
or
the
Bourne-again
shell
is
one
of
the
most
widely-used
shells
and
odds
are,
it’s
the
one
to
be
encountered
in
industry
or
in
academic
work.
Bash
is
the
shell
that
comes
enabled
by
default
with
most
of
the
popular
Linux
distributions.
It’s
also
available
on
macOS
zsh
shell
instead
but
they
are
designed
to
be
compatible.
The
author
of
this
work
also
uses zsh
as
his
main
driver.
and
in
Windows
with
the
Windows
subsystem
for
Linux.
In this document, Bash will be used. However, the reader is encouraged to explore some of the other shells out there once a working foundation in Bash is achieved.
3.2 The Structure of Commands
There are a few concepts and principles which needs to be understood to be a productive member of the CLI family. Before jumping into using commands though, have a look at how command line statements are structured with the following:
This is the general form. The pattern is command
, options
, and then arguments
. Here’s a
couple of common commands you’ll see with options and arguments that are used with them.
The details of what the aforementioned commands do will be the focus in the future. I just want to show you the structure of what we’ll be working with before we get into what these actually do. Depending on the current action, you might just have a command or a command and one or more options or just a command with one or more arguments.
But there will always be a command.
Command is the
The typed line is called the command line and UNIX uses a special program, called the shell or the command line interpreter, mentioned in the previous section, to interpret what you have typed into what you want to do.
The components of the command line are:
- 1.
- the command,
- 2.
- any options required by the command,
- 3.
-
the
command’s
arguments.
4 4 This is optional as some commands just don’t have any options.
3.2.1 Some Rules Regarding the Syntax
Since the introduction of UNIX System V, Release 3 (released 1983), any new commands must obey a particular syntax governed by the following rules:
-
Command names must be between 2 and 9 characters in length,
-
Command names must be comprised of lowercase characters and digits,
-
Option names must be one character in length,
-
All options are preceded by a hyphen (
-
), -
Options without arguments may be grouped after the hyphen,
-
The first option argument, following an option, must be preceded by white space,
-
i.e.,
-o sfile
is valid but-osfile
is illegal .
-
-
Option arguments are not optional ,
-
If an option takes more than one argument then they must be separated by commas with no spaces, or if spaces are used the string must be included in double quotes (
"
).,-
i.e., both are acceptable:
-f past,now,next
and-f "past now next"
.
-
-
All options must precede other arguments on the command line,
-
A double hyphen
--
may be used to indicate the end of the option list, -
The order of the options are order independent,
-
The order of arguments may be important,
-
A single hyphen
-
is used to mean standard input.
Options must come after the command and before arguments. Options should not appear after the main argument(s). However, some options can have their own arguments! Historically, UNIX commands have been fairly standard in the way that they use options but there are variations.
Bear in mind that commands established before System V, Release 3, do not conform to all of the above rules.
3.3 Helpful Keyboard Shortcuts for the Terminal
Before we moving on to more specific commands and get into CLI programming, there’s a few other helpful things to know about working at the Command Line. The first is Tab completion , a wonderful feature of the Bash shell, and is also included in many others. This feature let’s you skip typing out a whole file name or folder name when you’re working at the Command Line.
When you’re working in the command line it looks at all the information it has so far and makes a guess about what you mean.
For example, I can type ls -l De
and press
Tab
, and it completes the line with Desktop. Now type ls -l Do
and nothing would
happen when I press
Tab
. That’s because
Tab
doesn’t have one clear suggestion to return. As it can be either Documents
of Downloads. However, pressing
Tab
again should give you a suggestion of which items can be completed to.
For reference, in the following page, there is a table for most useful keyboard shortcut for Linux Bash.
Shortcut | Action | |
Navigation | Ctrl + A | Go to the beginning of the line. |
Ctrl + E | Go to the end of the line. | |
Alt + F | Move the cursor forward one word. | |
Alt + B | Move the cursor back one word. | |
Ctrl + F | Move the cursor forward one character. | |
Ctrl + B | Move the cursor back one character. | |
Ctrl + X | Toggle between the current cursor position and the beginning of the line. | |
Editing | Ctrl + A | Undo! (That’s an underscore, so you’ll need to use Shift as well.). |
Ctrl + X | Edit the current command in your $EDITOR. | |
Alt + D | Delete the word after the cursor. | |
Alt | Delete the word before the cursor. | |
Ctrl + D | Delete the character beneath the cursor. | |
Ctrl + H | Delete the character before the cursor (like backspace). | |
Ctrl + K | Cut the line after the cursor to the clipboard. | |
Ctrl + U | Cut the line before the cursor to the clipboard. | |
Ctrl + D | Cut the word after the cursor to the clipboard. | |
Ctrl + W | Cut the word before the cursor to the clipboard. | |
Ctrl + Y | Paste the last item to be cut. | |
Processes | Ctrl + L | Clear the entire screen (like the clear command). |
Ctrl + Z | Place the currently running process into a suspended background process. | |
Ctrl + C | Kill the currently running process by sending the SIGINT signal. | |
Ctrl + D | Exit the current shell. | |
Return | Exit a stalled SSH session. | |
History | Ctrl + R | Bring up the history search.. |
Ctrl + G | Exit the history search. | |
Ctrl + P | See the previous command in the history. | |
Ctrl + N | See the next command in the history. |
3.4 When you need help with Commands
If you ever see an experienced Linux user typing away at the command line in blazing speeds it can seem like memorising the ins and outs of commands and options is the only way to be productive and understand what’s going on. But everybody starts somewhere, and even experienced command-line users don’t memorize everything.
In the world of programming, it’s not practical to try to memorise all of the syntax and options of
command-line tools. Of course, it’s important to remember the basics, but while you’re getting started,
you only need to remember a few commands. The first one is man
, which stands for the
manual pages
.
A man
page
Think
of
the
man
pages
as
a
technical
reference
book
for
your
Linux
distribution
man
,
followed
by
a
command
you
want
to
learn.
Open
up
the
Terminal
by
Ctrl
+
Alt
+
T
.
Earlier,
you
saw
the
command ls
,
so
let’s
look
that
up.
Type man ls
and
press
Return
.
Some distributions or application specific installation of Linux remove the man
pages to
save up on space. In these system one must first do unminimize
to install man
pages.
Here,
you
can
see
some
information
about
the ls
command
head - 10
,
we
will
have
a
look
at
it
later.
.
You
can
see
that
it’s
for
listing
directory
contents
and
in
the
synopsis
section
you
get
a
quick
overview
of
how
to
use
the
command.
In
this
case
it
is ls [OPTION]... [FILE]...
.
We
write ls
followed
by
any
of
the
options
we
need,
and
the
file
or
folder
path
we
want
to
use.
The
terms
in
square
brackets
[OPTION]
and [FILE]
are
optional.
This
basically
means
you
don’t
have
to
use
these
for
the
command
to
work.
You
can
just
use
the ls
command
by
itself
to
see
the
default
output
of
listing
the
directory.
Here,
below
the
description
header,
there
is
a
bit
more
detailed
information
about
the
command,
including
its
default
behaviour
and
usage
notes,
and
below,
is
a
listing
of
the
options
that
the
command
takes.
Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print C-style escapes for nongraphic characters --block-size=SIZE with -l, scale sizes by SIZE when printing them;
There
are
a
lot
of
ways
to
use
the man
pages
efficiently
and
is
a
powerful
tool
when
you
need
to
find
what
can
a
command
do.
There
are
other
ways
to
learn
about
a
command.
Most
of
commands
also
have
an
option
called help
,
which
provides
a
brief
amount
of
information
about
them.
However,
they
usually
refer
you
to
the
manual
pages
for
more
detailed
documentation.
Therefore, help
will
give
you
a
brief
information
compared
to
the man
command.
You
can
see
if
a
command
you’re
using
has
this
feature
available
by
typing --help
after
the
command.
Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print C-style escapes for nongraphic characters --block-size=SIZE with -l, scale sizes by SIZE when printing them;
Here
you
can
scroll
up
and
down
to
have
a
look
at
some
of
the
information.
There
is
another
command
that’s
useful
when
you’re
working
in
Bash,
and
that’s
just help
by
itself.
Information
:
The help
Command
Displays information about shell built-in commands.
GNU bash, version 5.2.21(1)-release (aarch64-unknown-linux-gnu) These shell commands are defined internally. Type `help' to see this list. Type `help name' to find out more about the function `name'. Use `info bash' to find out more about the shell in general. Use `man -k' or `info' to find out more about commands not in this list. A star ( *) next to a name means that the command is disabled. job_spec [&] history [-c] [-d offset] [n] or hist> (( expression )) if COMMANDS; then COMMANDS; [ elif C>
As
we
get
into
working
with
the
Bash
shell,
the help
tool
can
act
as
a
handy
reminder
for
the
syntax
of
some
Bash
specific
commands.
But what if you don’t know the name of a command you are looking for?
In
that
case,
you
can
use
another
program
called apropos
which
searches
a
list
of
commands
and
their
descriptions
for
text
you
provide
as
an
argument.
Information
:
The apropos
Command
helps users find any command using its man
pages.
So if you wanted to find out what can list things, I could type apropos list and see a number of results that match that word.
port-contents(1) - List the files installed by a given port port-dependents(1), port-rdependents(1) - List ports that depend on a given (installed) port port-deps(1), port-rdeps(1) - Display a dependency listing for the given port(s) port-distfiles(1) - Print a list of distribution files for a port port-echo(1) - Print the list of ports the argument expands to port-installed(1) - List installed versions of a given port, or all installed ports port-list(1) - List available ports port-outdated(1) - List outdated ports port-variants(1) - Print a list of variants with descriptions provided by a port AllPlanes(3), BlackPixel(3), WhitePixel(3), ConnectionNumber(3), DefaultColormap(3), DefaultDepth(3), XListDepths(3), DefaultGC(3), DefaultRootWindow(3), DefaultScreenOfDisplay(3), DefaultScreen(3), DefaultVisual(3), DisplayCells(3), DisplayPlanes(3), DisplayString(3), XMaxRequestSize(3), XExtendedMaxRequestSize(3), LastKnownRequestProcessed(3), NextRequest(3), ProtocolVersion(3), ProtocolRevision(3), QLength(3), RootWindow(3), ScreenCount(3), ScreenOfDisplay(3), ServerVendor(3), VendorRelease(3) - Display macros and functions
Here’s the command that can list directory contents we were looking for.
Searching
for
commands
this
way
can
be
time-consuming,
but
if
you
know
what
you
need
to
do
but
not
the
command
to
do
it, apropos
is
very
helpful
and
powerful.
3.5 Additional Information
3.5.1 Use Tab completion on the Shell
If
you
do
not
know
the
exact
name
of
a
command,
then
you
can
make
use
of
tab
completion.
To
use
this
action,
launch
the
terminal
by
pressing
Ctrl
+
Alt
+
T
or
just
click
on
the
terminal
icon
in
the
task
bar.
Just
type
the
command
name
that
you
know
in
the
terminal
and
then
press
Tab
twice.
For
example,
if
we
can’t
remember man
,
we
can
write ma
and
can
choose
one
of
the
option
the
Bash
shell
presents
us.
3.5.2 The info command
Some
commands
do
not
have
their
manuals
written
or
they
are
either
info
.
To
use
this
command,
launch
the
terminal
by
pressing
Ctrl
+
Alt
+
T
or
just
click
on
the
terminal
icon
in
the
task
bar.
Just
type info
in
the
terminal
and
with
a
space,
type
the
name
of
the
command
whose
manual
does
not
exist
and
press
Return
.
File: coreutils.info, Node: ls invocation, Next: dir invocation, Up: Directory listing 10.1 ls: List directory contents ================================== The ls program lists information about files (of any type, including directories). Options and file arguments can be intermixed arbitrarily, as usual. Later options override earlier options that are incompatible. For non-option command-line arguments that are directories, by
The info
command
reads
documentation
in
the
info
format
Information : Man v. Info
Man
pages
are
the
UNIX
traditional
way
of
distributing
documentation
about
programs.
The
term
“man
page”
itself
is
short
for
“manual
page”,
as
they
correspond
to
the
pages
of
the
printed
manual;
the
man
pages
“sections”
3.5.3 The whatis command
This command is used with another command just to show a one liner usage of the latter command from its manual. It’s a quick way of knowing the usage of a command without going through the whole manual.
whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some sort of description within it. So, this command search for the manual pages names and show the manual page description of the specified filename or argument.
To
use
this
command,
launch
the
terminal
by
pressing
Ctrl
+
Alt
+
T
or
just
click
on
the
terminal
icon
in
the
task
bar.
Just
type whatis
in
the
terminal
and
after
a
space,
type
the
name
of
the
command
whose
one
liner
description
you
want
(for
example ls
)
and
then
press
Return
.
dcmcjpls(1) - Encode DICOM file to JPEG-LS transfer syntax dcmdjpls(1) - Decode JPEG-LS compressed DICOM file gdircolors(1), dircolors(1) - color setup for ls gls(1), ls(1) - list directory contents gdircolors(1), dircolors(1) - color setup for ls git-ls-files(1) - Show information about files in the index and the working tree git-ls-remote(1) - List references in a remote repository git-ls-tree(1) - List the contents of a tree object git-mktree(1) - Build a tree-object from ls-tree formatted text gls(1), ls(1) - list directory contents