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?
-
If documentation is "recorded steps for a human to execute",
then command line is "recorded steps for a computer to execute". -
Good balance between:
- Steps that a human can read and write relatively quickly.
- Steps that a computer can understand.
-
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.
-
Different steps of a process can be chained together relatively quickly.
step_1 | step_2 | step_3 echo qqwwww | tr 'qw' 'ha' | rev
-
This is a transferable skill – this can be used:
- in different products
- in different teams
- in different workplaces
Why Not Command Line?
-
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.
-
Easy to miscommunicate with the computer.
-
Errors can be catastrophic.
-
Large scripts tend to be difficult to understand.
-
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:
- 🪟 Windows: Microsoft Terminal
- 🐧 Linux: Terminator
- 🍏 OS X: iTerm 2
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
-
The Art of Command Line is a very good resource to discover what to learn.
-
To discover what a command does, generally type:
man <command> <command> -h # or <command> --help
-
If you install
tealdeer
, you can type:tldr <command>
This will show you example command usage with easy to understand descriptions.
-
explainshell.com maps a command and its args to the
man
page explanations.
Tips
- Spam the Tab key.
- Learn to navigate lines / words – Ctrl A / Ctrl E; Alt B / Alt F.
- Learn to delete by line / word – Ctrl U / Ctrl K; Ctrl W / Alt D.
- Learn to undo edits – Ctrl Shift Underscore.
- 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
- Working directory
- Paths
echo
/printf
- Hotkeys: jump between words, delete words, line
- Tab completion.
File System
ls
mkdir
,cd
,pushd
/popd
touch
,mv
,rm
cp
du
/df
find
/fd
Command Manipulation
- Pipe between commands
- stdin, stdout, stderr
- Redirection
tee
xargs
sudo
- heredocs
shuf
Environment
env
export
unset
sleep
ps
: process listuname
id
group
sar
(sysstat)
Text / Binary content
cat
/head
/tail
grep
/rg
tr
less
top
/btm
md5sum
hexdump
base64
Network
curl
nc
telnet
traceroute
nslookup
ifconfig
Shell Scripting
chmod
set -e
set -u
set -x
set -o pipefail
set -o nullglob
- variables
- subshells: (cd /tmp && echo hi > a_file)
- substring prefix / suffix
- conditionals
- loops
- switch
- functions
Day to day
vim
git
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.
Tool | Alternative |
---|---|
cat | bat |
cd | z (zoxide) |
diff | delta |
find | fd |
grep | rg |
ls | lsd |
sed | sd |
top | btm (bottom) |
powerline | starship |
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