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

bash
# Print entire file (only if small)
cat /etc/hostname
 
# Line count and size hint
wc -l /etc/passwd

Avoid cat on huge logs—it floods the terminal.

Page Through Large Files

bash
# Scroll: Space, b, q to quit
less /var/log/syslog
 
# Open at end (like tail)
less +G /var/log/syslog

Search inside less: type /pattern then Enter, n for next match.

Follow Logs Live

Developers live in tail -f during deploys:

bash
# Last 50 lines then follow new lines
tail -n 50 -f /var/log/syslog

Spring Boot or Node apps:

bash
# Example app log path
tail -f /opt/myapp/logs/application.log

Stop with Ctrl+C.

Combine with grep:

bash
# Only lines containing ERROR
tail -f application.log | grep --line-buffered ERROR

Quick Search with grep (Preview)

bash
# Find ERROR in a static file
grep ERROR /var/log/syslog | tail
 
# Case insensitive
grep -i error /var/log/syslog | head

Full grep coverage is in Text processing tools.

Edit with nano (Beginner-Friendly)

bash
# Edit file (creates if missing)
nano ~/notes.txt

Common keys (shown at bottom of nano):

KeyAction
Ctrl+OWrite (save)
Ctrl+XExit
Ctrl+KCut line
Ctrl+UPaste
Ctrl+WSearch

Edit system files with sudo:

bash
sudo nano /etc/hosts

Tip

Backup Before Editing Configs

Copy first: sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

vim Essentials (Optional but Valuable)

bash
vim ~/notes.txt

Modes:

  • Normal — move, delete, paste
  • Insert — type text (press i to enter)
  • Command: commands (press Esc first)

Minimal session:

text
i           # insert mode — type content
Esc         # back to normal
:w          # save
:q          # quit
:wq         # save and quit
:q!         # quit without saving

Movement: 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

bash
# First 10 lines of config
head -n 10 /etc/ssh/sshd_config
 
# Last 20 lines
tail -n 20 /etc/ssh/sshd_config

Mini Example: Watch Nginx Access Log

bash
# Install nginx for exercise (Ubuntu)
sudo apt update
sudo apt install -y nginx
 
# Follow access log
sudo tail -f /var/log/nginx/access.log

In 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.

What comes next?

Text processing: grep, sed, awk.