Working with Files & Text
Working with Files & Text
The terminal is a text-first environment. Configuration files, log files, scripts, and data pipelines all live as plain text on disk. A DevOps engineer who cannot fluently read, inspect, copy, move, and manipulate text files from the shell is blocked on nearly every operational task. This lesson teaches the canonical toolkit — cat, less, head, tail, cp, mv, rm, touch, and file — with the production mindset that separates professionals from beginners.
Reading File Contents
There are four primary tools for reading files, each suited to a different situation.
cat (concatenate) dumps the entire file to stdout. It is correct for small config files, quick inspections, and piping content into other tools. Avoid it on files larger than a few thousand lines — your terminal scrollback will fill up and you will lose context.
less is a pager: it shows one screenful at a time, lets you scroll up and down, and never loads the whole file into memory. It is the correct tool for any file you do not already know the size of. Key controls: j/k or arrow keys to scroll, Space/b for page down/up, /pattern to search forward, n/N to jump between matches, q to quit, F to follow new content (like tail -f).
less over cat for any log file or config you have not seen before. On a busy production server, cat /var/log/nginx/access.log can scroll thousands of lines past you in a second. less keeps you in control.
head and tail read the beginning or end of a file. Both default to 10 lines; -n N overrides that.
head -n 20 config.yaml— see the first 20 lines (great for verifying a header or schema)tail -n 50 /var/log/app.log— see the last 50 lines (recent events)tail -f /var/log/nginx/error.log— follow mode; new lines stream to your terminal in real time as the file grows — the single most important log-watching technique in productiontail -F(capital F) follows by filename and handles log rotation, making it more robust than lowercase-fin long-running sessions
Inspecting File Type and Metadata
file probes the actual content of a file (magic bytes, encoding, script shebang) and reports what it truly is — regardless of the file extension. This matters constantly in DevOps: you receive a binary with a .log extension, a gzipped file named backup, or a script with no extension at all.
cat a binary file to your terminal. Terminals interpret escape sequences in the byte stream. A crafted binary can send sequences that remap your keyboard, reset your terminal title, or in older terminals trigger command execution. Always run file on an unknown file before reading it.
Copying, Moving, and Renaming Files
cp copies files or directories. The source is preserved.
cp file dest— copy file to destinationcp -r dir/ backup/— copy a directory recursivelycp -p— preserve permissions, ownership, and timestamps (critical when copying config files)cp -a— archive mode: recursive + preserve everything (the right default for backups)
mv moves or renames. There is no copy — the original path disappears. On the same filesystem this is instantaneous (just a metadata update); across filesystems it copies then deletes.
rm deletes permanently — there is no recycle bin on Linux. Common flags: -r to remove directories recursively, -f to suppress "no such file" errors. The combination rm -rf is extremely powerful and frequently the cause of production disasters.
rm -rf trap. A misplaced space or a variable expansion to empty in a script can wipe an entire filesystem. Production-safe habits: always double-check the path with ls first; use rm -ri (interactive) on unfamiliar directories; never run rm -rf /some/path/$VAR unless you know $VAR cannot be empty. Google has lost data this way. So has every major cloud provider at least once.
touch creates an empty file if it does not exist, or updates the access and modification timestamps if it does. In DevOps it is commonly used to create lock files, trigger file-watching tools, or create placeholder files in Git repos.
Text Streams and Combining Tools
The real power of these commands emerges when you combine them via pipes. A production engineer rarely reads a raw log file — they filter, search, and summarize it on the fly. The tools in this lesson are foundational building blocks.
cp -p original original.bak. This single habit has saved countless engineers from having to restore from a full backup at 2 AM. Many teams also keep config under version control (Git) so every change is auditable — that is the professional standard covered in the Git tutorial.
What You Now Know
You can read any file safely (less, head, tail -F), identify what a file actually is (file), copy and back up configs without destroying the original (cp -p), rename and move files atomically (mv), and remove files deliberately and safely (rm). These primitives underpin every shell script, every automation pipeline, and every production incident response you will ever run.