Hey there! 👋
Welcome back to our Linux series! So far, you’ve learned how to move around the filesystem, manage files and folders, and probably started feeling like a terminal pro. 🔥
Now it’s time to learn how to view and edit file content directly from the command line.
This is super handy when you’re working on servers, writing scripts, or just don’t want to open a full-blown text editor.
Let’s break it down with the most commonly used commands. 👇
👁️ View File Content – cat
The cat
command is the quickest way to display the contents of a file.
Example:
cat hello.txt
It will print everything inside hello.txt
right there in your terminal.
💡 Tip: Best for small files. It can get messy if the file is too long.
🧵 View One Page at a Time – less
For bigger files, use less
. It lets you scroll and search too!
Example:
less bigfile.txt
Use the arrow keys to scroll, q
to quit.
🧠 Fun fact: You can search inside
less
by typing/word
and hitting Enter.
🔍 See the Beginning – head
Want to quickly peek at the first few lines?
Example:
head hello.txt
By default, it shows the first 10 lines. Want more?
head -n 20 hello.txt
🔚 See the End – tail
As the name suggests, tail
shows the end of a file.
Example:
tail hello.txt
Also shows 10 lines by default. Want to see the last 5?
tail -n 5 hello.txt
Bonus: You can even watch a file in real time (great for logs):
tail -f server.log
✍️ Editing Files – nano
(Beginner-Friendly Editor)
Ready to make some changes? nano
is a simple text editor that works right in the terminal.
Example:
nano notes.txt
You’ll be taken into an interactive editor. Use arrow keys to move around, make your edits, and when you’re done:
- Press
Ctrl + O
to save - Press
Enter
to confirm filename - Press
Ctrl + X
to exit
🧠 Compared to Windows: Think of it like a very minimal Notepad, but in the terminal.
🧪** Mini Challenge**
Try this mini project to get hands-on:
- Create a file named
quote.txt
. - Add your favorite quote using
nano
. - View it using
cat
. - Check the first line using
head -n 1
. - Check the last line using
tail -n 1
.
Commands:
touch quote.txt
nano quote.txt
cat quote.txt
head -n 1 quote.txt
tail -n 1 quote.txt
Easy and useful!
🧭 Quick Reference
Task | Command |
---|---|
View full file | cat filename |
View paged content | less filename |
View top lines | head filename |
View bottom lines | tail filename |
Watch file in real-time | tail -f filename |
Edit a file | nano filename |
🔚 Wrapping Up
And that’s a wrap for Part 5! 🥳
Being able to quickly view and edit files from the terminal is a superpower, especially when working on remote systems or debugging scripts.
Next up in Part 6, we’ll dive into file permissions—what drwxr-xr-x
means, how to use chmod
, chown
, and how to control who can do what with your files.
Stay curious, keep experimenting, and feel free to drop questions if anything feels confusing. You're doing great! 💪