DEV Community

Alkesh Baghel
Alkesh Baghel

Posted on

๐Ÿง Linux Basics โ€“ Part 5: Viewing and Editing Files from the Terminal

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

By default, it shows the first 10 lines. Want more?

head -n 20 hello.txt
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”š See the End โ€“ tail

As the name suggests, tail shows the end of a file.

Example:

tail hello.txt
Enter fullscreen mode Exit fullscreen mode

Also shows 10 lines by default. Want to see the last 5?

tail -n 5 hello.txt
Enter fullscreen mode Exit fullscreen mode

Bonus: You can even watch a file in real time (great for logs):

tail -f server.log
Enter fullscreen mode Exit fullscreen mode

โœ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Create a file named quote.txt.
  2. Add your favorite quote using nano.
  3. View it using cat.
  4. Check the first line using head -n 1.
  5. 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
Enter fullscreen mode Exit fullscreen mode

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)