After this lesson, you will be able to: Use the essential file and text commands (ls, cp, mv, rm, mkdir, find, grep, cat, less, tail, head, wc, sort, uniq) plus pipes and redirection.
These are the commands you'll type thousands of times. Pipes + redirection are what turn them from a list into a programming language.
Each command has a man page (`man cp`) with every flag.
cp src.txt dest.txt # copycp -r src/ dest/ # copy directory recursivelymv old new # move or renamerm file.txt # remove (no undo)rm -rf dir/ # remove directory recursively (DANGER)mkdir new-dir # create directorymkdir -p a/b/c # create with parentstouch file.txt # create empty file or update mtime
find searches FILES BY METADATA; grep searches FILE CONTENTS.
# find files by namefind . -name '*.log'find /var/log -name '*.gz' -mtime -7 # modified within last 7 daysfind / -size +100M 2>/dev/null # files over 100MB (silence permission errors)# grep file contentsgrep -r 'TODO' src/ # recursivegrep -n 'error' app.log # show line numbersgrep -v 'DEBUG' app.log # invert (lines NOT containing)grep -i 'warning' app.log # case-insensitivegrep -A 5 -B 2 'panic' app.log # 2 lines before, 5 after
Pick the right tool for the file size and access pattern.
cat file.txt # dump whole file (small files only)less file.txt # pager, scroll, search with /, quit with qhead -n 20 file.txt # first 20 linestail -n 20 file.txt # last 20 linestail -f app.log # FOLLOW: print new lines as they arrive (logs!)tail -n 100 -f app.log # show last 100, then follow
This is where the shell becomes a real programming language.
# > redirects stdout to a file (overwrites)ls > files.txt# >> appendsls >> files.txt# < reads from a file as stdinwc -l < files.txt# 2> redirects stderrcommand 2> errors.log# &> redirects both stdout and stderrcommand &> output.log# /dev/null discardscommand > /dev/null 2>&1# | pipes stdout of one command into stdin of anothercat app.log | grep ERROR | wc -l# count unique IPs in nginx access logcat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head# kill all node processesps aux | grep node | awk '{print $2}' | xargs kill
`rm -rf /` (or `rm -rf $UNSET_VAR/`), irreversible disaster. Modern rm refuses `/`, but `rm -rf $VAR/` where $VAR is empty deletes everything from `/`. Always quote and check. `cat huge.log` instead of `tail -f`. Eats memory; freezes the terminal. Forgetting `> /dev/null 2>&1` to fully silence. Using `grep -r` on `node_modules`. Add `--exclude-dir=node_modules` or use `rg` (ripgrep) which respects .gitignore by default. `mv` between filesystems is actually copy + delete, not atomic. Matters for backups.
Pick the cleanest description.
Sign in and purchase access to unlock this lesson.