In this chapter, we’ll cover the core commands for examining and modifying text files directly in the terminal. Viewing and editing files is very useful and common as there are bunch of configuration files that you frequently need to make changes in. When you are using shell you don’t have the luxury of rich text editors like word or notepad++.
1. cat – Display File Contents
The cat
(concatenate) command shows file contents in the terminal.
Basic Usage:
cat filename.txt
Common Options:
-n
– Show line numbers-b
– Number only non-empty lines
Example:
cat -n config.conf
2. less/more – Scroll Through Text
For large files, use these pagers to view content page by page.
less (recommended):
less large_file.log
- Navigation:
Space
– Next pageb
– Previous page/pattern
– Search forwardq
– Quit
more (older alternative):
more large_file.log
- Less features than
less
(ironically) - Automatically exits at end of file
3. head/tail – View File Beginnings/Ends
Quickly check the start or end of files.
head (first 10 lines by default):
head filename.log head -n 20 filename.log # Show first 20 lines
tail (last 10 lines by default):
tail filename.log tail -n 15 filename.log # Show last 15 lines tail -f live.log # Follow growing file (great for logs)
4. Terminal Text Editors
nano (Beginner-Friendly)
Simple, intuitive editor with on-screen help.
nano document.txt
- Basic Controls:
Ctrl+O
– SaveCtrl+X
– ExitCtrl+K
– Cut lineCtrl+U
– Paste
vi/vim (Power User Editor)
The classic modal editor with steep learning curve but powerful features.
vi document.txt
- Basic Workflow:
- Press
i
to enter Insert mode - Edit text
Esc
to return to Command mode:wq
to save and quit
- Press
5. echo – Print or Write Text
Useful for quick output and file creation.
Print to Terminal:
echo "Hello World"
Write to File:
echo "New content" > file.txt # Overwrite echo "Added line" >> file.txt # Append
Summary Cheat Sheet
Command | Description |
---|---|
cat file | Show entire file |
less file | Scroll through file |
head -n 5 file | Show first 5 lines |
tail -f log | Follow live log |
nano file | Simple text editor |
vi file | Advanced text editor |
echo "text" > file | Create/overwrite file |
Leave a Reply