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! ๐ช
Top comments (0)