Topic 1.1
Creating, Viewing, and Editing Files
In one line
A handful of commands cover the vast majority of real file work: creating an empty file, viewing a file's contents safely, and making a small edit without opening a full text editor.
Think of it like this
Real-life example: touch is like putting a fresh, empty folder on a shelf with a label — nothing inside yet, just a placeholder that now exists. touch myfile.txt creates an empty file (or, if the file already exists, just updates its 'last modified' timestamp without changing its contents at all).
Key ideas
- 01
cat <file>dumps a file's ENTIRE contents to the terminal at once — great for short files, genuinely unusable for anything long (it'll scroll past faster than you can read).less <file>opens a file for SCROLLABLE viewing (arrow keys or Page Up/Down,/to search,qto quit) without loading the whole thing into memory at once — the correct tool for anything more than a screenful. - 02
head <file>shows just the FIRST 10 lines by default (head -n 20for a specific count);tail <file>shows the LAST 10 lines — genuinely essential for log files, where the most recent (and usually most relevant) entries are at the bottom.tail -f <file>FOLLOWS a file live, printing new lines as they're written — the standard way to watch a log in real time. - 03
nanois a simple, beginner-friendly terminal text editor — on-screen shortcuts are shown at the bottom of the screen (Ctrl+O to save, Ctrl+X to exit) so you're never guessing.vimis far more powerful but has a genuinely steep learning curve (it has separate 'modes' for typing versus issuing commands) — worth knowing it exists, not worth mastering on day one. - 04
echo "text" > file.txtwrites text into a file, OVERWRITING anything already there.echo "more text" >> file.txtAPPENDS instead of overwriting — a single-character difference (>vs>>) with a genuinely destructive consequence if confused, so it's worth internalizing early.
Code & diagrams
Run these against a throwaway test file — nothing here is destructive if you use a scratch file.
# Create an empty file
touch notes.txt
# Write into it (overwrites any existing content)
echo "first line" > notes.txt
# Append without overwriting
echo "second line" >> notes.txt
# View the whole thing at once (fine for short files)
cat notes.txt
# View the last 5 lines of a longer file
tail -n 5 /var/log/syslog 2>/dev/null || echo "(no syslog on this machine, that's fine)"
# Follow a file live — Ctrl+C to stop
# tail -f /var/log/syslog
# Open in a beginner-friendly editor
nano notes.txt
# Ctrl+O to save, Ctrl+X to exitExplain it without notes
Why is cat a poor choice for viewing a 10,000-line log file, and what should you use instead?
You meant to type echo "new line" >> config.txt but accidentally typed a single > instead of >>. What just happened to the file?
Practice
Create a file, write three lines into it using echo and >> (append), then view it with cat, then view just the last two lines with tail -n 2.
Deliberately overwrite a test file with a single > after appending several lines, and confirm for yourself exactly what got lost — better to see this happen on a throwaway file now than on something that matters later.
Trade-offs
- ↔
nanois friendlier to learn but genuinely less powerful for serious editing at scale;vimhas a steep learning curve but rewards that investment heavily for anyone who edits text on a server often — most engineers are well served starting withnanoand picking upvimlater only if they find themselves wanting it.
Done when you can
I can create, view, and append to a file without opening a GUI editor.
I know the difference between > (overwrite) and >> (append), from direct experience, not just theory.
I can use tail -f to watch a file update live.