Using the Linux Command Line

How to access the command line

  • Terminal emulator
  • Virtual console
  • Remote login (SSH)

Terminals and shells

Introduction

When a new computer user is confronted with an article or tutorial on the internet telling them to open a terminal and enter some arcane commands, they’re likely to ask themselves why one would use such a seemingly outdated way to control the machine. And guidance to use the command line is still surprisingly frequent, even on macOS with its long history of having a highly user-friendly graphical interface. To many, using a command line interface is something people did only before the mouse was invented. Why would you not use the GUI, which has been invented specifically to provide a much more visual way of interacting with the computer?

It’s a valid question. The answer is that the command line allows for a level of control over the system that is orders of magnitude higher than with a graphical user interface. Sure, you can write GUI applications that make specific tasks straightforward, but the sheer number of tasks system administrators have to perform in their daily work would require application developers either to build humongous applications whose user interface would melt anyone’s brain, or a huge number of single applications, each with a specific purpose. These single-purpose utilities already exist – for the command line. Let’s imagine for a moment that a few of them allow changing their behaviour via 30 different options and parameters. However, in practice, you will only use a fraction of them regularly. Memorizing these few parameters and entering them on the command line is easy. And you can still look up the more exotic ones should you need them; which you also can do directly from the command line. To allow for the same functionality, a graphical application would have to display a whole bunch of user interface elements such as menus, check boxes, value sliders and text fields. You can get used to a complex GUI, of course. But getting used to typing commands with a few options will always be more efficient. I’m convinced that the keyboard allows for much more expressiveness than a mouse or trackball ever can. It’s the reason why, over a few years, I’ve built a collection of mechanical keyboards through which I rotate on a regular basis.

Another striking argument for the use of a command line is automation.

Finally, let me also raise the issue of accessibility.

Preparation

sudo dnf install -y ksh tcsh zsh tilix screen tmux

Defining the command line

Note:

  • Text mode interface
  • Type commands and see output

CLI terminology

  • Command prompt
  • Command line
  • Command-line interface
  • Command
  • Terminal
  • Console
  • Virtual console
  • Terminal emulator
  • Pseudo terminal
  • Session
  • Shell

Note:

  • Index

Command prompt

Command line

Note:

  • Software that accepts commands

Command-line interface

Note:

  • Text mode interface
  • Type commands and see output

Command

command [option(s)] [argument(s)]

  • Short option form: -o
  • Long option form: --option

Terminal

DECwriter terminal

https://commons.wikimedia.org/w/index.php?curid=115500012

Note:

  • DECwriter on PHP-11/40

VT100 terminal

https://commons.wikimedia.org/w/index.php?curid=29457452

Note:

  • VT100 on PDP-11/70

Console

Dennis Ritchie and Ken Thompson

Note:

  • Dennis Ritchie and Ken Thompson at PDP-11/20
  • Console is privileged terminal

Virtual consoles

Experiment 7-1: Switch between virtual consoles

Note:

  • Linux has multiple virtual consoles

Terminal emulator

Pseudo terminal

Note:

  • Linux device files under /dev/pts/

Session

Note:

  • many meanings

Shell

Note:

  • Software application that provides CLI

    Preparation

sudo dnf install -y ksh tcsh zsh tilix screen tmux

History of the Unix shell

  • Thompson shell
  • Mashey/PWB shell
  • Bourne shell
  • C shell
  • Bourne Again shell
  • zsh, fish

Note:

  • Thompson shell in Unix V1-V6
  • Thompson shell ← Multics shell ← RUNCOM
  • Mashey introduced most scripting features

Secure Shell (SSH)

Note:

  • Not a shell but a protocol & command

Viewing the command history

Fetching commands from the history

Using carets to correct typos: ^port^prot

Using substition:

  • !!:s/port/prot/
  • !rm:s/port/prot/

Paging through history

History expansion

Reusing command arguments

<Esc> .

Searching the command history

Concealing commands

Using the cursor

History expansion

Using carets to correct typos: ^port^prot

Using substition:

  • !!:s/port/prot/
  • !rm:s/port/prot/

Editing modes

Emacs mode

Vi mode

The PWD

Note:

  • Present working directory

Directory path notation styles

Directory hopping

Jump to your home directory

Tab completion

Shortcuts

  • Aliases
  • Variables

Fast travel with CDPATH

Globbing, substitution and expansion

Exploring files

Experiment 7-7: File operations

More commands

Experiment 7-8: less, and more

Summary

Tools and utilities

Preparations

For some of the experiments in this chapter, you’ll need the info command. This command isn’t installed by default in recent Fedora versions. Run the following shell command to make sure the command is available.

dnf install -y info