Viewing & editing files
cat, less, head, tail, wc, nano, vim.
Most server work is reading files — configs, logs, scripts — and a handful of commands cover every case, from "show me this whole small file" to "follow this log as it grows." Knowing which to reach for saves you from opening a graphical editor you do not have on a server.
cat — dump a file
cat prints a file’s entire contents to the screen. It is perfect for short files and terrible for long ones (it scrolls past instantly). Its name is short for "concatenate" because it can also join files — cat a.txt b.txt prints both in sequence. With -n it numbers the lines.
$ cat /etc/hostnameweb-01$ cat -n config.yml1 port: 80802 workers: 43 log_level: info
less — page through a big file
less opens a file in a scrollable pager — the right tool for anything longer than a screen. Inside less: arrow keys or Space to scroll, / to search forward (then n for the next match), G to jump to the end, g to the start, and q to quit. It loads instantly even on huge files because it does not read the whole thing at once. When cat scrolls off the screen, reach for less.
$ less /var/log/syslog# inside less:# Space / b scroll down / up a page# /error search forward for "error" (n = next match)# G jump to end of file# q quit
head, tail — the start or the end
head shows the first lines of a file (10 by default, or -n N for N lines); tail shows the last. tail is the one you will use most, because the end of a log is the recent activity. And tail -f "follows" the file — it stays open and prints new lines as they are appended, which is how you watch a log live while reproducing a problem.
$ head -n 3 access.log10.0.1.5 - GET /health 20010.0.1.9 - GET /api/users 20010.0.1.9 - POST /api/login 401$ tail -n 2 access.log10.0.2.1 - GET /api/orders 50010.0.2.1 - GET /api/orders 500$ tail -f /var/log/nginx/access.log # follow live — new lines appear as they arrive... (stays open, printing each new request; Ctrl-C to stop)
wc — count lines, words, bytes
wc counts. On its own it prints lines, words, and bytes; -l is the one you want most — count lines — which combined with a pipe answers "how many?" questions constantly: how many errors in the log, how many users in the file, how many processes match.
$ wc -l access.log1428 access.log # 1428 lines$ grep 500 access.log | wc -l # how many 500 errors?37
nano and vim — editing in the terminal
To change a file you need an editor. nano is the beginner-friendly one: it shows the key commands at the bottom (^ means Ctrl), so Ctrl-O saves ("write out") and Ctrl-X exits. vim is more powerful and ubiquitous but modal — you start in "normal" mode, press i to insert text, Esc to leave insert mode, and :wq to save and quit (or :q! to quit without saving). Every server has vi/vim, so knowing the four commands to survive it is worth it.
$ nano config.yml# type normally; ^O = save, ^X = exit (^ = Ctrl)$ vim config.yml# i start typing (insert mode)# Esc stop typing (back to normal mode)# :wq save and quit# :q! quit WITHOUT saving (the escape hatch when you are stuck)