Viewing and Editing Text
Introduction
Servers have no Notepad—you view logs and configs in the terminal and edit with nano or vim. This chapter covers reading files efficiently (less, tail -f) and safe editing habits on production machines.
Prerequisites
View Small Files
# Print entire file (only if small)
cat /etc/hostname
# Line count and size hint
wc -l /etc/passwdAvoid cat on huge logs—it floods the terminal.
Page Through Large Files
# Scroll: Space, b, q to quit
less /var/log/syslog
# Open at end (like tail)
less +G /var/log/syslogSearch inside less: type /pattern then Enter, n for next match.
Follow Logs Live
Developers live in tail -f during deploys:
# Last 50 lines then follow new lines
tail -n 50 -f /var/log/syslogSpring Boot or Node apps:
# Example app log path
tail -f /opt/myapp/logs/application.logStop with Ctrl+C.
Combine with grep:
# Only lines containing ERROR
tail -f application.log | grep --line-buffered ERRORQuick Search with grep (Preview)
# Find ERROR in a static file
grep ERROR /var/log/syslog | tail
# Case insensitive
grep -i error /var/log/syslog | headFull grep coverage is in Text processing tools.
Edit with nano (Beginner-Friendly)
# Edit file (creates if missing)
nano ~/notes.txtCommon keys (shown at bottom of nano):
| Key | Action |
|---|---|
Ctrl+O | Write (save) |
Ctrl+X | Exit |
Ctrl+K | Cut line |
Ctrl+U | Paste |
Ctrl+W | Search |
Edit system files with sudo:
sudo nano /etc/hostsTip
Backup Before Editing Configs
Copy first: sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim Essentials (Optional but Valuable)
vim ~/notes.txtModes:
- Normal — move, delete, paste
- Insert — type text (press
ito enter) - Command —
:commands (pressEscfirst)
Minimal session:
i # insert mode — type content
Esc # back to normal
:w # save
:q # quit
:wq # save and quit
:q! # quit without savingMovement: h j k l, gg top, G end, /word search.
vim has a learning curve—nano is enough for emergencies; many developers invest in vim over time.
Local IDE + Remote Edit (Workflow)
For real projects:
- VS Code Remote SSH or IntelliJ deployment over SFTP
- Edit locally; deploy with Git + CI, not manual vim on prod
Use server-side editors for quick config tweaks on VPS, not full application development.
wc and head/tail Recap
# First 10 lines of config
head -n 10 /etc/ssh/sshd_config
# Last 20 lines
tail -n 20 /etc/ssh/sshd_configMini Example: Watch Nginx Access Log
# Install nginx for exercise (Ubuntu)
sudo apt update
sudo apt install -y nginx
# Follow access log
sudo tail -f /var/log/nginx/access.logIn another terminal or browser, visit http://localhost/ on the server, then watch lines appear.
FAQ
cat vs less?
cat dumps all; less paginates. Use less for anything longer than a screen.
nano not installed?
sudo apt install nano on Ubuntu.
Accidentally changed production config?
Restore from .bak or redeploy from Git. Prefer config management in mature teams.
Binary file opened in vim?
Garbage display—use file command to detect binaries; do not edit with text editors.