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/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 stoptail -f
.
less for viewing with search and navigation
Start less
on a file.
less /etc/ssh/ssh_config
Navigation keys:
Space
orPageDown
move forward one screenb
orPageUp
move backwardg
jump to top,G
jump to bottom- line numbers:
100g
goes 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/null
Pipe into less
to page long output.
journalctl -u ssh 2>/dev/null | less
ps aux | sort -k4 -nr | less
Color friendly paging with -R
preserves ANSI colors.
grep --color=always -n "PermitRootLogin" /etc/ssh/sshd_config 2>/dev/null | less -R
Set the default pager.
printf '\nexport PAGER=less\n' >> ~/.bashrc
source ~/.bashrc
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
# 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.
nano ~/playground/day4/notes.txt
Common keys (shown at the bottom in nano):
Ctrl O
write file,Enter
confirmCtrl X
exitCtrl W
search,Alt W
search nextCtrl K
cut line,Ctrl U
pasteAlt G
go to line and column
Create and edit a config style file.
mkdir -p ~/playground/day4
nano ~/playground/day4/app.conf
Example contents:
[server]
port=8080
log_level=info
Enable line numbers and soft wrap by default with a user config.
mkdir -p ~/.config
printf "set linenumbers\nset softwrap\n" > ~/.config/nanorc
Export EDITOR
and VISUAL
so tools open the right editor.
printf '\nexport EDITOR=nano\nexport VISUAL=nano\n' >> ~/.bashrc
source ~/.bashrc
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.
sudoedit /etc/ssh/sshd_config
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.
vimtutor
Open a file.
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
thenn
next,N
previous - replace current line example:
:%s/old/new/g
for 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 files
Optional 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
EOF
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
# 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.
sudo apt install -y moreutils 2>/dev/null || sudo dnf install -y moreutils 2>/dev/null
Compare 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 -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. Usesudoedit
instead of running the editor as root.E212: Can't open file for writing
in Vim. Write with:w !sudo tee %
only whensudoedit
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.