How to View and Edit Files on Linux
Learn how to read and edit files on Linux using cat, less, nano, and the basics of vim — the tools every Linux user reaches for first.
Viewing vs Editing
Linux offers lightweight tools for simply viewing file contents without the risk of accidental edits, and full-featured terminal text editors when you need to make changes. Here is what each tool is best for.
cat — Print a File to the Terminal
cat reads one or more files and prints them to standard output. It's best for short files:
cat /etc/hostname
cat config.txt error.log # print multiple files in sequence
Useful flags: -n adds line numbers; -A shows invisible characters (helpful for debugging whitespace).
less — Page Through Large Files
less opens a file in a scrollable pager. Unlike cat, it doesn't print the whole file at once:
less /var/log/syslog
Navigation inside less:
- Space / b — page down / up
- Arrow keys — scroll line by line
- /pattern — search forward; n next match
- G — jump to end; g — jump to beginning
- q — quit
head and tail — Read the Start or End
head -n 20 access.log # first 20 lines
tail -n 50 error.log # last 50 lines
tail -f /var/log/syslog # follow live updates (great for logs)
tail -f is invaluable for watching a log file update in real time.
nano — Beginner-Friendly Editor
nano is the easiest terminal editor. Open a file with:
nano config.txt
The bottom of the screen shows shortcuts. The most important ones:
- Ctrl + O — save (Write Out)
- Ctrl + X — exit
- Ctrl + W — search
- Ctrl + K — cut line; Ctrl + U — paste
vim — Powerful Once You Know the Basics
vim has a learning curve but is available on virtually every Linux server. The key concept: vim has modes.
vim myfile.txt
- Normal mode (default) — navigate and run commands
- Insert mode — type text. Enter with
i, exit with Esc - :w — save; :q — quit; :wq — save and quit; :q! — quit without saving
If you accidentally open vim and can't exit, press Esc then type :q! and hit Enter.
Choosing the Right Tool
- Quick look at a small file →
cat - Scrolling through a large file →
less - Watching live logs →
tail -f - Editing on a server →
nano(beginner) orvim(power user)