Day 4 - Viewing and Editing Files in the Terminal

2025-09-246 min read

linuxterminallessnanovimeditorslogs

Day4 cheatsheet

Workflows become faster when files can be viewed and edited without leaving the terminal. This lesson covers less, head, tail, basic search, real time log viewing, safe edits with nano, and an introduction to Vim for future growth.

Today at a glance

Tools used: cat, less, head, tail, grep, nano, vim, sudoedit. Environment variables: PAGER, EDITOR, VISUAL.

Prerequisites

  • Day 1 through Day 3 completed
  • Terminal with Bash
  • A user account with sudo

Fast file previews

Cat, Less, Head and Tail cheatsheet

bash
# print a whole file
cat /etc/os-release

# first and last lines
head -n 20 /var/log/syslog 2>/dev/null || head -n 20 /var/log/messages 2>/dev/null
head -n 3 ~/.bashrc

tail -n 20 ~/.bashrc

# follow a growing file in real time
sudo tail -f /var/log/auth.log 2>/dev/null || sudo tail -f /var/log/secure 2>/dev/null

Notes:

  • head shows the first lines, tail shows the last lines.
  • tail -f keeps the file open and prints new lines as they are written.
  • Press Ctrl C to stop tail -f.

less for viewing with search and navigation

Start less on a file.

bash
less /etc/ssh/ssh_config

Navigation keys:

  • Space or PageDown move forward one screen
  • b or PageUp move backward
  • g jump to top, G jump to bottom
  • line numbers: 100g goes to line 100
  • search forward with /pattern, next with n, previous with N
  • search backward with ?pattern
  • quit with q

Open compressed logs without manual decompression when lesspipe is enabled.

bash
less /var/log/syslog.1.gz 2>/dev/null || less /var/log/messages-*.gz 2>/dev/null

Pipe into less to page long output.

bash
journalctl -u ssh 2>/dev/null | less
ps aux | sort -k4 -nr | less

Color friendly paging with -R preserves ANSI colors.

bash
grep --color=always -n "PermitRootLogin" /etc/ssh/sshd_config 2>/dev/null | less -R

Set the default pager.

bash
printf '\nexport PAGER=less\n' >> ~/.bashrc
source ~/.bashrc
Binary files

Do not open arbitrary binary files in less without -f. If the output looks like random symbols, exit with q. Use file first to check the type.

Grep basics with paging

bash
# show matching lines with line numbers
grep -n "^Port" /etc/ssh/sshd_config 2>/dev/null | less -R

# recursive search under a directory
grep -R "TODO" ~/projects | less -R

# case insensitive and whole word
grep -niw "error" /var/log/syslog 2>/dev/null | less -R

ripgrep (rg) is a faster alternative available in many repos, but standard grep is widely installed.

Editing safely with nano

Nano is a simple editor that is friendly for new users.

bash
nano ~/playground/day4/notes.txt

Common keys (shown at the bottom in nano):

  • Ctrl O write file, Enter confirm
  • Ctrl X exit
  • Ctrl W search, Alt W search next
  • Ctrl K cut line, Ctrl U paste
  • Alt G go to line and column

Create and edit a config style file.

bash
mkdir -p ~/playground/day4
nano ~/playground/day4/app.conf

Example contents:

text
[server]
port=8080
log_level=info

Enable line numbers and soft wrap by default with a user config.

bash
mkdir -p ~/.config
printf "set linenumbers\nset softwrap\n" > ~/.config/nanorc
Set the preferred editor

Export EDITOR and VISUAL so tools open the right editor.

bash
printf '\nexport EDITOR=nano\nexport VISUAL=nano\n' >> ~/.bashrc
source ~/.bashrc
Edit system files the safe way

Use sudoedit instead of sudo nano or sudo vim. sudoedit copies the file to a temporary location, opens it with the user's editor, then writes back with root permissions.

bash
sudoedit /etc/ssh/sshd_config
Avoid editing binary files

Do not open files under /bin or /usr/bin in a text editor. Use file to check type when unsure.

Vim introduction for long term growth

Vim is powerful and widely available. A minimal starter is enough for this series.

Start the built in tutorial.

bash
vimtutor

Open a file.

bash
vim ~/playground/day4/demo.txt

Modes:

  • Normal mode for navigation and commands
  • Insert mode for typing text
  • Visual mode for selections

Basic commands:

  • i enter Insert mode, Esc return to Normal mode
  • :w write file, :q quit, :wq write and quit, :q! quit without saving
  • movement: h left, j down, k up, l right, w next word, b previous word, 0 start of line, $ end of line
  • search: /text then n next, N previous
  • replace current line example: :%s/old/new/g for whole file
  • undo u, redo Ctrl R

Open multiple files and switch between them.

bash
vim a.txt b.txt
:bn     " next buffer
:bp     " previous buffer
:args   " list files

Optional minimal config to make Vim friendlier.

bash
cat > ~/.vimrc <<'EOF'
set number
set ignorecase smartcase
set incsearch
set hlsearch
set expandtab shiftwidth=2 tabstop=2
syntax on
EOF
Modeline safety

Some files carry Vim modelines that can set options. Modern Vim is safe by default. To be strict, add set modelines=0 to ~/.vimrc.

Real time log viewing patterns

bash
# follow one file
sudo tail -f /var/log/auth.log 2>/dev/null | ts 2>/dev/null || sudo tail -f /var/log/secure 2>/dev/null

# follow by unit with systemd
journalctl -u ssh -f 2>/dev/null

# follow kernel messages
journalctl -k -f 2>/dev/null

ts (from moreutils) prepends timestamps if installed.

bash
sudo apt install -y moreutils 2>/dev/null || sudo dnf install -y moreutils 2>/dev/null

Compare changes after edits

bash
# copy a file, make a safe change, then compare
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak 2>/dev/null || true
sudoedit /etc/ssh/sshd_config
sudo diff -u /etc/ssh/sshd_config.bak /etc/ssh/sshd_config | less -R

Exit codes and verification:

  • diff returns 0 when files are identical, 1 when they differ, and above 1 on error.
  • After editing service configs, test syntax if a tool supports it, then restart the service with systemctl in a later lesson.

Troubleshooting

  • Permission denied when editing system files. Use sudoedit instead of running the editor as root.
  • E212: Can't open file for writing in Vim. Write with :w !sudo tee % only when sudoedit is not an option.
  • less shows garbled output. Add -R when piping colored output.
  • Terminal does not support the clipboard. Use the editor's own copy and paste or a terminal that supports clipboard integration.
  • tail -f shows nothing. Confirm that the log path matches the distribution. Debian and Ubuntu often use /var/log/syslog and /var/log/auth.log. RHEL and Fedora use /var/log/messages and /var/log/secure.

Next steps

Day 5 covers users, groups, and sudo. It explains account data in /etc/passwd and /etc/group, how to create users, and how to grant limited administrative rights with sudo while keeping the system safe.