Git & Terminal Cheatsheet

Terminal Basics

Use this Git & Terminal reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Orientation

pwd                 # print working directory (the absolute path you are in)
ls                  # list files
ls -la              # long listing, including hidden dotfiles
ls -lah             # same, with human-readable sizes (-h needs -l)
ls -lt              # newest files first
whoami              # current user
echo $SHELL         # your login shell (zsh, bash, ...)
uname -a            # OS and kernel info
clear               # clear the screen (or ctrl+l)

Getting Help

man ls              # full manual page (q to quit, / to search, n for next match)
ls --help           # built-in usage summary (GNU/Linux tools)
git --help          # same idea, works per subcommand: git commit --help
help cd             # help for a shell BUILT-IN (cd, export, alias, ...)
man -k copy         # search man pages by keyword (same as apropos)
type ls             # is it a builtin, alias, function, or binary?
which python3       # path of the binary that will run
tldr tar            # community example-first pages (brew/apt install tldr)

On macOS/BSD, ls --help does not exist, use man ls. And -h is never a help flag on classic Unix tools, for ls/du/df it means human-readable sizes.

Path Shortcuts

SymbolMeaning
~your home directory
.the current directory
..the parent directory (one level up)
-the previous directory (cd - jumps back)
/the filesystem root
*glob: matches any characters (*.log)
$VARenvironment variable expansion ($HOME, $PATH)

Keyboard Shortcuts

KeysAction
ctrl+cinterrupt (stop) the running command
ctrl+dend of input / close the shell
ctrl+lclear the screen
ctrl+rreverse-search command history (press again for older)
ctrl+a / ctrl+ejump to start / end of line
ctrl+wdelete the word before the cursor
ctrl+u / ctrl+kdelete to start / end of line
tabautocomplete paths and commands
/ walk command history
!!re-run the previous command (sudo !!)

Reading and Opening Files

cat notes.txt           # print a whole file
less app.log            # page through a big file (q quits, /text searches, G = end)
head -n 20 app.log      # first 20 lines
tail -n 20 app.log      # last 20 lines
tail -f app.log         # follow a file as it grows (live logs), ctrl+c stops
wc -l app.log           # count lines
open .                  # macOS: open the current folder in Finder
open file.pdf           # macOS: open in the default app
xdg-open .              # Linux desktop equivalent of open

Pipes and Redirection

ps aux | grep node          # pipe: output of one command into another
history | tail -20          # last 20 commands you ran
ls > files.txt              # redirect stdout to a file (overwrite)
ls >> files.txt             # append instead of overwrite
npm run build 2> errors.log # redirect stderr only
npm run build > all.log 2>&1   # stdout AND stderr to one file
sort < names.txt            # read stdin from a file
ls *.txt | xargs wc -l      # feed piped output as ARGUMENTS to a command
make 2>&1 | tee build.log   # show output AND save it to a file
OperatorMeaning
a | bstdout of a becomes stdin of b
> / >>write / append stdout to a file
2>redirect stderr
2>&1merge stderr into stdout
&&run next command only if the previous succeeded
||run next command only if the previous failed