Bash essentials#

https://tiswww.case.edu/php/chet/img/bash-logo-web.png

Image source#

Using CLI effectively#

First things first: the terminal can feel awkward to use. What can we do about this?

Each section below is some set of tips for using the interactive bash CLI effectively.

Keyboard shortcuts#

Configuration#

Bare necessities#

The following sections explain the purpose of each command and show a few use cases and useful options.

These are commands you probably already know – if you don’t, you’ll know by the end of lab-0, as you’ll need them all!

Getting around: cd and ls#

NAME
  cd - change the current directory
  ls - list directory contents

SYNOPSIS
  cd [DIR]
  ls [OPTION]... [FILE]...

Viewing files: cat and tac#

NAME
  cat - concatenate files and print on the standard output
  tac - concatenate and print files in reverse

SYNOPSIS
  cat [OPTION]... [FILE]...
  tac [OPTION]... [FILE]...

Creating files: touch and mkdir#

NAME
  touch - Update the modification times of each `FILE` to the current time.
          Creates the files if they do not exist.
  mkdir - Create the given DIRECTORY(ies) if they do not exist

SYNOPSIS
  touch [FILE]...
  mkdir [-p/--parents] [DIRECTORY]...

Moving files: mv and cp#

NAME
  mv - Move `SOURCE` to `DEST`, or multiple `SOURCE`(s) to `DIRECTORY`.
  cp - Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

SYNOPSIS
  mv  [-f/--force] [-i/--interactive] [-g/--progress] [SOURCE]... [DEST]
  cp  [-f/--force] [-i/--interactive] [-g/--progress] [-R/--recursive] [SOURCE]... [DEST]

Managing permissions: chmod and chown#

NAME
  chmod - Change the permissions mode of each FILE to MODE.
  chown - Change file owner and group of each FILE to USER:GROUP

SYNOPSIS
  chmod [-R/--recursive] [MODE] [FILE]
  chown [-R/--recursive] [USER:GROUP] [FILE]

Deleting files: rm#

NAME
  rm - Remove the FILE(s)

SYNOPSIS
  rm [-f/--force] [-i/--interactive] [-r/--recursive] [FILE]...

The five fingers of death#

https://theactionelite.com/wp-content/uploads/2019/04/GetAttachmentThumbnail.jpg

Five Fingers of Death, or King Boxer as it is known on Wikipedia, is a martial-arts movie I have not seen, but I have heard referenced in many songs. It speaks to me that the mastery of a seemlingly small set tools (five fingers) can lead to drastic increases in capability (the ability to inflict death) and I believe this spirit applies directly to working with unix tools. Image source#

The following 5 sets of commands are indispensable GNU Coreutils that are included on all linux systems.

There are many more coreutils that I have not included – I have chosen these 5 sets as I believe that mastering them, above all, will bring you in harmony with your linux system, and therefore closer to truth, happiness, and the meaning of life – or, if not, at least they will help you solve the labs that I give you in this course.

Almost all of these notes are adapted from a resource I found that’s pretty much exactly what I wanted to write myself: [@CLITextProcessing]. It comes with great explanations and exercises and solutions. I may base some quizzes and tests on it!

find files and grep content#

NAME
  find - search for files that match a given expression
  grep - print lines in file(s) that match a given pattern

SYNOPSIS
  find [STARTING-POINT...] [OPTION...] [EXPRESSION]
  grep [OPTION...] PATTERNS [FILE...]

tr characters and cut fields#

NAME
  tr - Translate characters matching STRING1 in stdin/FILE to STRING2,
       writing to stdout
  cut - Prints specified columns from each line of stdin, writes to stdout

SYNOPSIS
  tr [OPTION]... STRING1 STRING2
  cut [-d/--delimiter] [-f/--fields] [FILE]

sort data and uniq duplicates#

NAME
  sort - Display sorted concatenation of all FILE(s).
         With no FILE, or when FILE is -, read stdin
  uniq - Report or omit repeated lines.

SYNOPSIS
  sort [FILE]...
  uniq [-d/--repeated] [FILE]...

know head from tail#

NAME
  head - Print the first 10 lines of each `FILE` to standard output.
         With no `FILE`, or when `FILE` is `-`, read stdin
  tail - Print the last 10 lines of each `FILE` to standard output.
         With no `FILE`, or when `FILE` is `-`, read stdin

SYNOPSIS
  head [-n/--lines] [FILE]...
  tail [-n/--lines] [-f/--follow] [FILE]...

tree and tee#

NAME
  tree - list contents of DIRECTORIES in a tree-like format.
  tee - Copy standard input to each FILE, and also to standard output.

SYNOPSIS
  tree [-L level] [DIRECTORY]...
  tee [FILE]...

Redirection and Pipes#

This section was adapted from [@HowToRedirectionProcess]

When Bash starts, normally, 3 file descriptors are opened, 0, 1 and 2 also known as standard input (stdin), standard output (stdout) and standard error (stderr).

You can use the > operator to “redirect” the output of commands (which normally goes to stdout) to different files or other file descriptors. Some common examples are shown below:

command  >  filename     Redirect command output (stdout) into a file
command  >  /dev/null    Discard stdout of command
command  2> /dev/null    Discard stderr of command

command  >&2             Redirect command output (stdout) to stderr

command  >> filename     Redirect command output and APPEND into a file
command  <  filename     Redirect a file into a command

commandA | commandB       Pipe stdout of commandA to commandB
commandA | tee filename   Pipe stdout of commandA into filename AND stdout

Core utilities#

ssh#

NAME
  ssh - OpenSSH remote login client

SYNOPSIS
  ssh [-l login_name] [-p port] DESTINATION [command [argument...]

ssh is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to provide secure encrypted communications between two untrusted hosts over an insecure network.

ssh connects and logs into the specified destination, which may be specified as either [user@]hostname or a URI of the form ssh://[user@]hostname[:port].

If a command is specified, it will be executed on the remote host instead of a login shell.

rsync#

NAME
  rsync - a fast, versatile, remote (and local) file-copying tool

SYNOPSIS
  Local:
    rsync [OPTION...] SRC... [DEST]
  Access via remote shell:
    Pull:
        rsync [OPTION...] [USER@]HOST:SRC... [DEST]
    Push:
        rsync [OPTION...] SRC... [USER@]HOST:DEST

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon.

It is famous for sending only the differences between the source files and the existing files in the destination, increasing efficiency for repetitive synchronization between source and destination.

Rsync is widely used for backups and mirroring, and as an improved cp command for everyday use.

tar, zip, and unzip#

NAME
  tar - a general archiving utility for creation/extraction/compression and more
  zip - package and compress files into a ZIP archive
  unzip - list, test and extract compressed files from a ZIP archive

SYNOPSIS
  tar --create/--extract [--file ARCHIVE] [OPTIONS] [FILE...]
  zip [OPTIONS] [ARCHIVE] [FILE...]
  unzip [ARCHIVE] [-d OUTPUTDIR]

The tar, zip, and unzip programs provide the ability to create, extract, and otherwise manipulate archives of files, where an archive of files is simply a file that stores a collection of other files.

You can read more:

git#

NAME
  git - the stupid content tracker

SYNOPSIS
  git <command> [<args>]

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

See man 7 gittutorial to get started, then see man 7 giteveryday for a useful minimum set of commands.

More resources#

  • LinuxCommand.org

    • Short guides on learning bash shell and bash scripting.

    • Links to interactive learning games under “Adventures”. Basic Shell Features

    • Complete reference with examples.