Chapter 4: Viewing and Editing Files in WSL Ubuntu (cat, nano, vim, less)
In Linux, working with files often means using the terminal. Whether you’re reading logs, editing config files, or writing scripts, you’ll rely on powerful command-line tools.
This chapter covers essential commands for viewing and editing files: cat, less, nano, and vim. Each has its strengths — and once you get comfortable, you’ll never need a GUI for quick edits.
📄 Viewing Files in the Terminal
📌 cat – View Entire File (No Scroll)
Displays the contents of a file directly in the terminal.
cat filename.txt
Good for short files. For longer files, use less.
📌 less – Scroll Through Long Files
Allows you to scroll up and down through long files easily.
less filename.txt
↑/↓orPageUp/PageDown– scrollq– quit/word– search forwardn– next result
📌 head & tail – View Top/Bottom of Files
head filename.txt # First 10 lines
tail filename.txt # Last 10 lines
tail -f logfile.log # Live updates (logs)
📝 Editing Files in the Terminal
📌 nano – Simple Text Editor
Beginner-friendly and easy to use. Ideal for quick config changes.
nano myfile.txt
Basic nano controls:
Ctrl + O– SaveCtrl + X– ExitCtrl + K– Cut lineCtrl + U– Paste line
📌 vim – Powerful Editor (Advanced Users)
vim is a highly powerful terminal editor but has a learning curve.
vim myfile.txt
Vim basics:
i– Insert mode (start editing)Esc– Command mode:w– Save:q– Quit:wq– Save and quitdd– Delete lineu– Undo
If vim is not installed, you can install it using:
sudo apt install vim
📌 Pro Tips
- Use
lessto read long log files like/var/log/syslog - Edit config files like
.bashrcusingnano ~/.bashrc - Combine commands:
cat filename.txt | grep "searchword"
🧪 Practice Task
Open Ubuntu WSL and try the following:
# Create and edit a file
nano mynotes.txt
# Type some text, save and exit (Ctrl+O, Enter, Ctrl+X)
# View with cat and less
cat mynotes.txt
less mynotes.txt
# Add more lines
echo "New line added" >> mynotes.txt
head mynotes.txt
tail mynotes.txt
# Try vim (optional)
vim mynotes.txt
✅ Summary
cat– Quick file outputless– Scrollable viewingnano– Beginner-friendly text editor</li
