Skip to main content
Linux Command Line – Complete Beginner to Advanced Guide
CHAPTER 05 Beginner

Viewing and Editing File Content

Updated: May 16, 2026
25 min read

# CHAPTER 5

Viewing and Editing File Content

1. Introduction

In a Graphical User Interface (GUI), you read a document by double-clicking it to open Microsoft Word. In a Linux server environment without graphics, opening a heavy word processor is impossible. However, the vast majority of Linux configuration files, server logs, and software code are simple, plain-text files. To administer a Linux machine, you must be able to slice, read, and modify text directly within the terminal window. In this chapter, we will master the tools of text manipulation. We will use cat and less to read data, use head and tail to surgically extract log entries, and take our first steps into the powerful terminal text editors: nano and vim.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Quickly print file contents to the terminal using the cat command.
  • Paginate and navigate massive text files using the less command.
  • Extract the top or bottom lines of a log file using head and tail.
  • Monitor live, updating server logs using tail -f.
  • Edit configuration files using the beginner-friendly nano editor.
  • Understand the basic operational modes of the advanced vim editor.

3. Reading Files (cat and less)

1. The cat Command (Concatenate): cat is the simplest way to read a file. It grabs the text inside the file and vomits it directly onto your terminal screen.
bash
1
cat /etc/hostname

*Use case:* Great for reading small, 5-line configuration files. *The Problem:* If you cat a 10,000-line server log, the text will scroll past your eyes at lightning speed, leaving you looking at only the very last page.

2. The less Command (The Pager): To read a large file, you use less. It opens the file in a clean, scrollable interface.

bash
1
less /var/log/syslog

*Navigation in less:*

  • Press Spacebar to scroll down one page.
  • Press Up/Down Arrows to scroll line by line.
  • Type /error to search the document for the word "error".
  • Press q to quit and return to the terminal.

4. Surgical Reading (head and tail)

Often, you don't care about the whole file. You only want the newest or oldest entries.
  • head: Prints the first 10 lines of a file.
bash
123456789101112
  head -n 20 report.txt  # Prints the first 20 lines instead of 10
  ```
- **`tail`:** Prints the last 10 lines of a file.
  ```bash
  tail /var/log/auth.log  # Shows the most recent login attempts
  ```

**The Superpower: `tail -f` (Follow)**
If a web server is currently crashing, you want to watch the errors happen in real-time. By adding the `-f` flag, the terminal "locks on" to the bottom of the file. As the web server writes new lines to the log file, they instantly pop up on your screen live! (Press `Ctrl+C` to exit).

### 5. Editing Text: `nano` (The Beginner's Tool)
To change the text inside a file, you need a terminal text editor. `nano` is the easiest editor to learn because it has a menu at the bottom of the screen.

bash nano config.txt ``

  1. 1. The terminal changes into a text editing window.
  1. 2. You can use your arrow keys to move the cursor and type normally.
  1. 3. At the bottom, you see shortcuts like ^O Write Out and ^X Exit. (The ^ symbol means the Ctrl key).
  1. 4. To save: Press Ctrl + O, hit Enter to confirm the name.
  1. 5. To close: Press Ctrl + X.

6. Editing Text: vim (The Professional's Tool)

vim (Vi Improved) is infamous. It is incredibly powerful, entirely keyboard-driven, and notoriously difficult for beginners because *you cannot just start typing*. vim uses Modes:
  1. 1. Command Mode (Default): When you open a file (vim script.sh), you are in command mode. If you press the d key twice, you instantly delete a line of text.
  1. 2. Insert Mode: To actually type text, you must press the i key. At the bottom left, it will say -- INSERT --. Now you can type normally.
  1. 3. Escaping: To save, you must press the Esc key to return to Command mode.
  1. 4. Saving and Quitting: Type a colon :, followed by wq (Write and Quit), and press Enter.

*Why learn Vim? Because it is installed by default on 100% of Linux servers globally. Nano is not always available.*

7. Diagrams/Visual Suggestions

*Visual Concept: The Text Editor Progression* Create a visual spectrum. On the far left (Simplest, but read-only), place
cat. In the middle-left (Scrollable, read-only), place less. In the middle-right (Simple editing), place nano. On the far right (Complex, high-power editing), place vim. This helps users conceptualize which tool to grab based on the complexity of their task.

8. Best Practices

  • Never edit production configurations directly: Before using nano or vim to edit a critical system file (like /etc/ssh/sshdconfig), always use cp to make a backup copy first (cp sshdconfig sshd_config.backup). If your edits break the server, you can instantly restore the original text.

9. Common Mistakes

  • Getting Trapped in Vim: The most universally shared joke in computer science is not knowing how to exit Vim. Beginners open it, smash keys, the document fills with garbage text, and they end up closing the entire terminal window to escape.
*The universal escape sequence:* Press
Esc three times, type :q! (Quit without saving!), and press Enter.

10. Mini Project: The Live Log Monitor

Simulate a troubleshooting session:
  1. 1. Open a terminal. Type ping google.com > network.log &. (This runs a ping command silently in the background and writes the output to a text file).
  1. 2. Type tail -f network.log.
  1. 3. Watch your terminal screen. You will see new lines appearing every second as the background ping updates the file!
  1. 4. Press Ctrl + C to stop watching.
  1. 5. (Cleanup: Type killall ping to stop the background process).

11. Practice Exercises

  1. 1. What is the fundamental operational difference between reading a file with cat versus reading a file with less?
  1. 2. Detail the exact sequence of keystrokes required to open a file in vim, type the word "Hello", save the file, and exit back to the terminal.

12. MCQs with Answers

Question 1

You are troubleshooting a web application and need to watch the server's error log update continuously in real-time as users interact with the site. Which command accomplishes this?

Question 2

When utilizing the nano text editor in the Linux terminal, which keyboard shortcut combination is used to save ("Write Out") the modifications you have made to a file?

13. Interview Questions

  • Q: A junior engineer complains that when they run cat on a massive 5GB database dump file, the terminal freezes for 10 minutes. Explain why this happens and what command they should use instead to safely view the contents.
  • Q: Explain the paradigm of "Modes" within the vim text editor. Differentiate between Command Mode and Insert Mode.
  • Q: Walk me through the terminal commands required to view only the top 15 lines of a script file named deploy.sh.

14. FAQs

Q: Can I just use VS Code to edit files on my Linux server? A: Yes! Modern editors like VS Code have "Remote-SSH" extensions. You can connect VS Code on your Windows laptop directly to your cloud Linux server, allowing you to edit the remote server files using a beautiful GUI. However, you must still know
nano or vim for emergency situations when remote GUI access is broken.

15. Summary

In Chapter 5, we conquered the manipulation of raw text within the CLI. We utilized
cat for instantaneous viewing and deployed less for paginated reading and searching of massive documents. We wielded head and tail as surgical tools, proving the immense forensic value of tail -f for live log monitoring. Finally, we transitioned from reading to writing, exploring the beginner-friendly menu system of nano and addressing the steep learning curve and modal architecture of the industry-standard vim` editor.

16. Next Chapter Recommendation

You can create a file, and you can edit a file. But what happens when the server says "Permission Denied"? Proceed to Chapter 6: Linux File Permissions.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·