🎬 Introduction to Command Line
💡 Preface
This module is part of a course on DevOps.
Checkout the course introduction for more information
This module is part of chapter 2
What is command line ?
Command line is the means of interacting with a computer or operating system by inputting lines of text.
Each line being a command, I.E "Command" and "line"
In order to talk to an operating system, we type commands in a Terminal Program.
The terminal program basically runs the operating system shell, which is the command line intepreter
The terminal basically acts as a text-based interface to a Shell program.
The Shell interprets commands, processes it and produces output.
In simple terms communication flows like this:
User -> Terminal -> Shell -> Kernel
A User types commands into the Terminal.
The terminal gives us access to the shell, which takes the command, processes it by talking to the Kernal through special API's and produces output and we can see that output in the Terminal.
Types of Shells
There are a number of shells out there depending on your operating system.
On Windows we have PowerShell and CMD (Command Prompt), on Linux we have Bourne Shell (sh) and Bourne-Again Shell (bash). Linux also has other shells like Z shell, or zsh.
In this module, I will showcase an example of Windows Shells and Linux Shells.
So you understand the difference between Terminals and Shells
Types of Terminals
On windows as we can see there is a built-in terminal that can run either Powershell or CMD Command Prompt. Windows also launched a newer terminal called Windows Terminal which is available on Github.
On Windows, we get different terminals like GNOME Terminal, Terminology, Terminator and more
In this demo, I will show you an example of Windows Terminal and Terminator on Linux
Why Learn the Command Line
Troubleshooting
Command line is very important in times when there is no access to Apps \ GUI's or User Interfaces.
Most of the time, when accessing production servers, you may only be able to access it via a terminal through protocols like SSH.
Some examples include:
- When a production server runs out of disk space, you need command line to troubleshoot it
- When services are unable to reach network endpoints, you need command line to test network connectivity
Command line could also be required when certain systems are mostly driven by API's.
For example, some Microsoft Azure or Amazon AWS services have more options to configure via their API's and their GUI portals are often playing catchup.
API's are generally built before UIs, meaning UI's may be behind or may never support certain capabilities.
Scripting and Automation
Another reason to learn command line, is that it gives you a foundation to start automating from.
When you want to automate something, you gather and stitch together all the commands you need and then write it in a script which allows you to run the script to perform many commands at once
Some examples may include:
- We could stitch together commands that check if folder structures exists or not and create them if they dont exist. Then automatically create configuration files with default settings in them to setup an application
- We could stitch together commands that makes a copy of important files and folders on our system, compress them as a "zip" file and upload it to remote storage to create an automated backup process.
Command Line Structure
As a beginner, one of the most important things to learn about is the structure of command lines
Generally, all or most commands conform to some standard.
Executable
For example, all command lines start with some form of executable or binary.
A binary of executable is a program or script.
For example in our Introduction to git
, we learned about the git
command. In that case git
is the executable.
So we start off our command with the executable:
git
Commands and Subcommands
An executable may have commands, and commands by have subcommands.
However, some executable may have no commands. This is generally true for command line executables where they generally only perform one thing, like ls
If you run ls --help
, you'll notice the usage:
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
ls
has no commands.
Commands or Subcommands are generally after the executable. For example:
<executable> <command> <optional-subcommand>
GIT on the other hand, has many commands and some commands also have subcommands.
For example:
git checkout
git pull
git commit
git push
#etc
Arguments, Options, Flags or Parameters
When a command needs inputs, the command may require arguments, options, flags or parameters.
Those terms are often used interchangably.
Command line options generally start with dashes.
Generally a single -
character indicates a short abbreviated form of an option and a double --
is a long form version of the same option.
For example, -h
and --help
generally mean the same thing, but one is abbreviated, so shorter to type out and the other is longer, which means its more descriptive but longer to type out.
If we take a look at git
that we learned about:
git commit -m "commit message here"
Above, -m
stands for message.
Collection of important commands
echo
: This command allows us to print text to the screen or print variable contents- For example,
echo "hello"
, orecho $HOME
- For example,
cat
: This command displays file contents. It can display one or multiple files contents and concatenates them- For example
cat <filename>
- For example
ls
: Allos to list contents of a directorycd
: Change working directorypwd
: Writes the full path of the currend working directory to the screentouch
: Creates a file without contentnano
: Starts a simple Linux text editormkdir
: Create a directorycp
: Copies a file or directorymv
: Renames a file or directory. Useful for renaming files or moving contentrm
: Removes a file or directorygrep
: Find text in a file or in outputdf
: Displays the amount of disk space available on the file systemdu
: Estimates file space usage for a directoryps
: Displays information about active processes.ps aux
displays all processestop
: Command line utility to monitor system loadkill
: Terminates a process by process IDwget
: Downloads files from the webcurl
: Create and send a Web request to a server
Command inputs and outputs
We can capture outputs of a command.
For example when running ls
, it outputs to the terminal.
We can capture that output by placing the >
character after the command followed by a file name that we want to pass output to
ls -l / > output.txt
The single >
will override the file and replace all its current content with the input provided.
If we want to preserve the current content and simply append input to it, we use the double >
For example:
echo "add more content" >> output.txt