Introduction

💻

This book provides an introduction to the command line – why it is useful, and tips for learning.

The examples in this book are geared for Unix operating systems – Linux and OS X. Windows users may use the Windows Subsystem for Linux to run the same commands.

Why Command Line?

  1. If documentation is "recorded steps for a human to execute",
    then command line is "recorded steps for a computer to execute".

  2. Good balance between:

    • Steps that a human can read and write relatively quickly.
    • Steps that a computer can understand.
  3. The script is software, so once it is written, it can be re-run by re-entering the commands.

    Tends to be faster than guiding a person through a graphical user interface.

  4. Different steps of a process can be chained together relatively quickly.

    step_1 |
    step_2 |
    step_3
    
    echo qqwwww  |
    tr 'qw' 'ha' |
    rev
    
  5. This is a transferable skill – this can be used:

    • in different products
    • in different teams
    • in different workplaces

Why Not Command Line?

  1. Difficult in general.

    With a website, you are given menus / options to select. So a new user may learn by clicking.

    With a command line interface, you have a text box. There's no hint how to begin.

  2. Easy to miscommunicate with the computer.

  3. Errors can be catastrophic.

  4. Large scripts tend to be difficult to understand.

  5. No type safety.

When writing anything with more than 100 lines, consider using a typed, compiled language.

The type safety and compilation will make errors more visible, before you execute the logic.

Terminal vs Shell

Terminal

The terminal is the application that houses the shell.

Examples:

Shell

The shell is the application with the prompt for commands to be entered.

Examples:

  • dash: /bin/sh
  • bash: /bin/bash
  • zsh
  • Powershell

Paths

All applications, shell or not, run in a "working directory".

# Print the current working directory
pwd

# Same, but with the `PWD` environment variable
echo $PWD

The cd command changes the working directory.

# Create a directory at '/tmp/abc/def' and switch to it
mkdir -p /tmp/abc/def
cd /tmp/abc/def

Tip: You can cd - to move to the directory you were last in.

Relative and Absolute Paths

Paths without a top level root are interpreted to be relative to the current working directory.

  • Linux / OS X: Root is / or ~.
  • Windows: Root is usually a drive letter c:\.
cd /tmp
cd abc/def # relative to '/tmp'

Special Paths

# '.' (dot) is a path referring to the current working directory
pwd
cd . && pwd


# '..' (dot dot) is a path referring to the parent of the current directory
cd .. && pwd


# '~' (tilde) is a notation for "current user's home directory"
cd ~ && pwd


# '/tmp' is usually a file system that lives on memory.
# It gets wiped when you restart your computer.
cd /tmp

Learning

  1. The Art of Command Line is a very good resource to discover what to learn.

  2. To discover what a command does, generally type:

    man <command>
    <command> -h  # or
    <command> --help
    
  3. If you install tealdeer, you can type:

    tldr <command>
    

    This will show you example command usage with easy to understand descriptions.

  4. explainshell.com maps a command and its args to the man page explanations.

Tips

  1. Spam the Tab key.
  2. Learn to navigate lines / words – Ctrl A / Ctrl E; Alt B / Alt F.
  3. Learn to delete by line / word – Ctrl U / Ctrl K; Ctrl W / Alt D.
  4. Learn to undo edits – Ctrl Shift Underscore.
  5. Interrupt / cancel the current input or command – Ctrl C.

You are dangerous when you know a command exists, and run it without knowing what it does.

Examples

List files in current directory

ls is the "list" command, which lists files in the working directory.

ls          # show me the files in my current directory
ls /tmp     # show me the files in the '/tmp' directory

echo $HOME  # show me the value of the `HOME` variable
ls $HOME    # show me the files in the $HOME directory

echo 'arg with spaces' 'second arg'
echo "\$HOME is interpolated to: $HOME"
Find a string within source files
find .         \
  -name '*.rs' \
  -type f      \
  -not -path './target/*' |
xargs grep '"Error\b'

# with ripgrep
rg -Fw '"Error'

Learning Checklist

Basics

  1. Working directory
  2. Paths
  3. echo / printf
  4. Hotkeys: jump between words, delete words, line
  5. Tab completion.

File System

  1. ls
  2. mkdir, cd, pushd/popd
  3. touch, mv, rm
  4. cp
  5. du / df
  6. find / fd

Command Manipulation

  1. Pipe between commands
  2. stdin, stdout, stderr
  3. Redirection
  4. tee
  5. xargs
  6. sudo
  7. heredocs
  8. shuf

Environment

  1. env
  2. export
  3. unset
  4. sleep
  5. ps: process list
  6. uname
  7. id
  8. group
  9. sar (sysstat)

Text / Binary content

  1. cat / head / tail
  2. grep / rg
  3. tr
  4. less
  5. top / btm
  6. md5sum
  7. hexdump
  8. base64

Network

  1. curl
  2. nc
  3. telnet
  4. traceroute
  5. nslookup
  6. ifconfig

Shell Scripting

  1. chmod
  2. set -e
  3. set -u
  4. set -x
  5. set -o pipefail
  6. set -o nullglob
  7. variables
  8. subshells: (cd /tmp && echo hi > a_file)
  9. substring prefix / suffix
  10. conditionals
  11. loops
  12. switch
  13. functions

Day to day

  1. vim
  2. git
  3. xclip / pbcopy

Supporting Skills

  • Typing fast: Typing faster means typing commands faster.
  • Hotkeys: Allows faster command construction.
  • Multi-Caret Text editing: Allows parallelizing command creation.

Rust Alternatives

The following show alternatives to standard tools.

These generally have coloured output, and perform faster.

ToolAlternative
catbat
cdz (zoxide)
diffdelta
findfd
greprg
lslsd
sedsd
topbtm (bottom)
powerlinestarship

Nushell

Like Powershell, nushell doesn't just pass strings between commands, but objects.

ls | sort-by type
ls | sort-by type | where size > 10kb

echo [[name value]; ['a' 1] ['b' 2] ['c' 3]] | table

echo [[name value]; ['a' 1] ['b' 2] ['c' 3]] |
  update value {|it| $it.value + 10 } |
  table