
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.
Tools used: cat, less, head, tail, grep, nano, vim, sudoedit. Environment variables: PAGER, EDITOR, VISUAL.
Prerequisites
Fast file previews

# 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/nullNotes:
headshows the first lines,tailshows the last lines.tail -fkeeps the file open and prints new lines as they are written.- Press
Ctrl Cto stoptail -f.
less for viewing with search and navigation
Start less on a file.
less /etc/ssh/ssh_configNavigation keys:
SpaceorPageDownmove forward one screenborPageUpmove backwardgjump to top,Gjump to bottom- line numbers:
100ggoes to line 100 - search forward with
/pattern, next withn, previous withN - search backward with
?pattern - quit with
q
Open compressed logs without manual decompression when lesspipe is enabled.
less /var/log/syslog.1.gz 2>/dev/null || less /var/log/messages-*.gz 2>/dev/nullPipe into less to page long output.
journalctl -u ssh 2>/dev/null | less
ps aux | sort -k4 -nr | lessColor friendly paging with -R preserves ANSI colors.
grep --color=always -n "PermitRootLogin" /etc/ssh/sshd_config 2>/dev/null | less -RSet the default pager.
printf '\nexport PAGER=less\n' >> ~/.bashrc
source ~/.bashrcDo 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
# 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 -Rripgrep (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.
nano ~/playground/day4/notes.txtCommon keys (shown at the bottom in nano):
Ctrl Owrite file,EnterconfirmCtrl XexitCtrl Wsearch,Alt Wsearch nextCtrl Kcut line,Ctrl UpasteAlt Ggo to line and column
Create and edit a config style file.
mkdir -p ~/playground/day4
nano ~/playground/day4/app.confExample contents:
[server]
port=8080
log_level=infoEnable line numbers and soft wrap by default with a user config.
mkdir -p ~/.config
printf "set linenumbers\nset softwrap\n" > ~/.config/nanorcExport EDITOR and VISUAL so tools open the right editor.
printf '\nexport EDITOR=nano\nexport VISUAL=nano\n' >> ~/.bashrc
source ~/.bashrcUse 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.
sudoedit /etc/ssh/sshd_configDo 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.
vimtutorOpen a file.
vim ~/playground/day4/demo.txtModes:
- Normal mode for navigation and commands
- Insert mode for typing text
- Visual mode for selections
Basic commands:
ienter Insert mode,Escreturn to Normal mode:wwrite file,:qquit,:wqwrite and quit,:q!quit without saving- movement:
hleft,jdown,kup,lright,wnext word,bprevious word,0start of line,$end of line - search:
/textthennnext,Nprevious - replace current line example:
:%s/old/new/gfor whole file - undo
u, redoCtrl R
Open multiple files and switch between them.
vim a.txt b.txt
:bn " next buffer
:bp " previous buffer
:args " list filesOptional minimal config to make Vim friendlier.
cat > ~/.vimrc <<'EOF'
set number
set ignorecase smartcase
set incsearch
set hlsearch
set expandtab shiftwidth=2 tabstop=2
syntax on
EOFSome 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
# 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/nullts (from moreutils) prepends timestamps if installed.
sudo apt install -y moreutils 2>/dev/null || sudo dnf install -y moreutils 2>/dev/nullCompare changes after edits
# 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 -RExit codes and verification:
diffreturns 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
systemctlin a later lesson.
Troubleshooting
Permission deniedwhen editing system files. Usesudoeditinstead of running the editor as root.E212: Can't open file for writingin Vim. Write with:w !sudo tee %only whensudoeditis not an option.lessshows garbled output. Add-Rwhen 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 -fshows nothing. Confirm that the log path matches the distribution. Debian and Ubuntu often use/var/log/syslogand/var/log/auth.log. RHEL and Fedora use/var/log/messagesand/var/log/secure.
Up next: Day 5 - Users, Groups, and Sudo
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.