2025-01-02 21:52:51 +11:00

8.9 KiB

🎬 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 Linux, 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 catch up.
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 exist or not and create them if they don't exist. Then automatically create configuration files with default settings in them to setup an application
  • We could stitch together commands that make 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 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 executables 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 interchangeably.
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 it's 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", or echo $HOME
  • cat : This command displays file contents. It can display one or multiple files contents and concatenates them
    • For example cat <filename>
  • ls : Allows to list contents of a directory
  • cd : Change working directory
  • pwd : Writes the full path of the current working directory to the screen
  • touch : Creates a file without content
  • nano : Starts a simple Linux text editor
  • mkdir : Create a directory
  • cp : Copies a file or directory
  • mv : Renames a file or directory. Useful for renaming files or moving content
  • rm : Removes a file or directory
  • grep : Find text in a file or in output
  • df : Displays the amount of disk space available on the file system
  • du : Estimates file space usage for a directory
  • ps : Displays information about active processes. ps aux displays all processes
  • top : Command line utility to monitor system load
  • kill : Terminates a process by process ID
  • wget : Downloads files from the web
  • curl : Create and send a Web request to a server

Command inputs, outputs and redirection

We can capture outputs of a command.

This becomes useful for two main reasons.
Firstly, we can use the output of one command as input for another. Technically this allows us to chain commands together.
This is useful in automation and scripting when taking output of one command execution and passing it as input to another

Secondly, capturing output of a command can be useful to store in a file for troubleshooting purposes or any other reason.
For example, a command's output may be quite large to analyze in a terminal window and may be useful to analyze as a file which we can open in an editor such as Visual Studio Code.

Command pipe

To capture output of one command and send it as input to another command, we can use the "pipe" character.
This often is referred to "piping" output to another command in command line

For example, we can use cat to output file contents and then | it to grep to search that content for a given string pattern.
This often helps looking for something in a large file.

cat <filename> | grep <pattern>

Command redirection

To redirect command line output to a file, we can use the command redirection character >

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 filename 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