Text tools, pipes & redirection
Linux commands are small tools that each do one thing well, and the real power comes from chaining them together. Logs, config files and command output are all just text, so a handful of text tools lets you answer almost any question about a server.
Input, output and errors
> moves 1, 2> moves 2, and 2>&1 joins 2 to 1.Every command has three channels: stdin (input, usually your keyboard), stdout (normal output, usually your screen) and stderr (error messages, also the screen, but a separate channel). Redirection reroutes them:
$ ls > files.txt # stdout into a file (overwrites it)
$ echo "done" >> log.txt # append instead of overwrite
$ ./deploy.sh 2> errors.txt # only the errors into a file
$ ./deploy.sh > all.log 2>&1 # errors into the same place as output
$ ./noisy.sh > /dev/null # throw output away
2>&1 reads as "send channel 2 (errors) to wherever channel 1 (output) is going". /dev/null is a special file that swallows everything written to it.
Pipes
The pipe | sends one command's output straight into the next command's input, so you build answers step by step:
$ ps aux | grep nginx # only nginx processes
$ cat access.log | wc -l # how many requests?
$ grep " 500 " access.log | wc -l # how many were server errors?
$ cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head
# ↑ which IP addresses hit us most often
Read that last line left to right: take the first space-separated field (the IP) from each line, sort them so duplicates sit together, count each group with uniq -c, sort by that count biggest first, show the top ten.
grep: finding lines
$ grep "error" app.log # lines containing "error"
$ grep -i "error" app.log # case-insensitive
$ grep -n "timeout" app.log # show line numbers
$ grep -v "DEBUG" app.log # lines that DON'T match
$ grep -r "DB_HOST" /etc/myapp # search every file in a folder
$ grep -c "404" access.log # just count matches
Sorting, counting, cutting
wc -lcounts lines (-wwords,-cbytes).sortsorts lines;-nnumerically,-rreversed,-hunderstands sizes like 2G.uniqcollapses repeated neighbouring lines (so sort first);-ccounts them.cut -d',' -f2takes the 2nd comma-separated column.awk '{print $1, $9}'prints chosen whitespace-separated columns, handy for logs.sed 's/old/new/g' filefinds and replaces;sed -iedits the file in place.diff a bshows which lines differ between two files.xargsturns input lines into arguments:find . -name "*.tmp" | xargs rm.
Environment variables and PATH
An environment variable is a named value that programs can read, which is how apps get settings like database addresses and API keys without hard-coding them. export DB_HOST=10.0.0.5 sets one for the current shell and anything it starts; echo $DB_HOST reads it; env lists them all. Put exports in ~/.bashrc to set them on every login.
PATH is the variable listing the folders the shell searches when you type a command name. "command not found" usually means the program isn't installed or its folder isn't in PATH.
Your shell also remembers what you typed: history lists it, !! repeats the last command (sudo !! reruns it with sudo), and Ctrl+R searches back through it.