Table of Contents
Why Text Files Matter on Linux
Linux systems store an enormous amount of information as plain text:
- System configuration (e.g. files in
/etc) - Logs (e.g.
/var/log/syslog,/var/log/messages) - Scripts and code
- Documentation and notes
Being able to view and edit text files efficiently from the command line is one of the most important everyday skills on Linux.
This chapter focuses on:
- Reading text files in different ways
- Making small, quick edits
- Choosing the right tool for the job
(Details about specific editors like nano and vim are covered in their own subsections; here we focus on the general landscape and core usage patterns.)
Basic Commands for Viewing Text Files
These tools are for reading files, not editing them. They’re safe to use on system files because they don’t change anything.
`cat` – Concatenate and display files
cat is the simplest way to print a file’s contents to the terminal.
- Show an entire file:
cat file.txt- Show multiple files one after another:
cat part1.txt part2.txt
cat is best for small files; for large files it’ll scroll past too fast.
You’ll see cat often combined with other tools via pipes (covered more in the I/O redirection chapter), e.g.:
cat file.txt | grep error`less` – View files page by page
less is the standard pager on most systems. It loads only part of the file at a time and lets you scroll.
- Open a file with
less:
less /etc/passwd
Inside less:
- Scroll down:
Space(page),Downarrow, orj - Scroll up:
b(back a page),Uparrow, ork - Go to beginning:
g - Go to end:
G - Search forward:
/patternthenEnter - Search backward:
?patternthenEnter - Next match:
n - Previous match:
N - Quit:
q
Because it doesn’t load the whole file at once, less is ideal for very large logs or data files.
Useful options
- Show line numbers:
less -N file.txt- Colorize output when piped from something that uses color (common with
grep -n --color=auto, etc.) is usually automatic, but on some systems you may need:
grep pattern file.txt | less -R
-R tells less to output “raw” control characters (like color codes).
`more` – Older pager
more is similar to less but more limited. On modern systems, less is preferred.
- Open a file:
more file.txt
Basic keys are similar (Space to go forward, q to quit), but it has fewer navigation features. Use less unless you specifically need more (e.g., on a minimal rescue shell).
`head` and `tail` – Peek at beginnings and ends
`head` – First lines of a file
By default, head shows the first 10 lines.
head file.txtChange how many lines:
head -n 20 file.txt # first 20 lines
head -5 file.txt # shorthand for -n 5`tail` – Last lines of a file
By default, tail shows the last 10 lines.
tail file.txtChange number of lines:
tail -n 50 file.txt`tail -f` – Follow a growing file
tail -f is especially useful for watching log files in real time.
sudo tail -f /var/log/syslog
This keeps running and displays new lines as they’re written. Quit with Ctrl + C.
Combine with grep to watch only interesting lines:
sudo tail -f /var/log/syslog | grep "error"Viewing Binary vs Text Files
Most of the commands above assume the file is text (printable characters). If you run them on binary files (e.g. images, compiled programs), you may see gibberish or control characters.
- To quickly inspect if a file is text or binary:
file somefile
If you accidentally run cat or less on binary data and your terminal looks messed up, type reset and press Enter to recover.
Quick One-Line Edits from the Command Line
Sometimes you want to make a very small change (e.g., fix a typo in a config file) without opening a full editor.
`sed` for simple replacements
sed is a stream editor and can modify text non-interactively. A common beginner pattern is a simple search-and-replace.
- Replace the first occurrence of
oldwithnewon each line and print the result:
sed 's/old/new/' file.txtThis only displays the changed text; it doesn’t modify the file.
- To save changes back to the file (use with care):
sed -i 's/old/new/' file.txt
-i means “edit in-place”.
Common variations:
- Replace all occurrences of
oldon each line:
sed -i 's/old/new/g' file.txt- Show line with a number and confirm before replacing (safer approach is usually to inspect with
sedorgrep, then use backup):
Instead of blindly using -i, many people create a backup:
sed -i.bak 's/old/new/g' file.txt
This creates file.txt.bak with the original content.
`sed` for line-based changes
A few useful examples:
- Delete specific line numbers (e.g., line 3):
sed '3d' file.txt- Delete a range of lines (e.g., lines 5–10):
sed '5,10d' file.txt
As before, add -i to modify in place.
Choosing the Right Tool
Different tools are suited to different tasks:
- Quick look at a small file:
cat file.txt - Browse a large file with scrolling and search:
less file.txt - See top or bottom of a file:
head file.txt,tail file.txt - Watch logs live:
tail -f /var/log/syslog - Search within file output:
grep pattern file.txt | less - Make a tiny scripted change:
sed -i 's/old/new/' file.txt
For anything more complex than tiny changes, a text editor (like nano or vim) is usually a better choice. Those editors and how to use them are covered in the following chapters.
Common Viewing Workflows in Practice
Here are a few realistic examples tying the commands together.
Checking a configuration file safely
- Display the file with line numbers:
nl -ba /etc/ssh/sshd_config | less- Note the line you want to change.
- Make a backup and edit with an editor (explained in later chapters).
nl -ba numbers all lines (including empty ones), which is useful when referring to problems reported as “line X”.
Inspecting a large log file for errors
- View the end of the log:
sudo tail /var/log/syslog- Follow new messages:
sudo tail -f /var/log/syslog- Filter for a keyword:
sudo grep "failed" /var/log/syslog | lessChecking the first and last lines of a data file
head -n 5 data.csv
tail -n 5 data.csvThis quickly confirms whether the file looks as expected.
Basic Editor Selection Notes
You’ll encounter multiple editors on Linux; each has a different style:
nano– simple, beginner‑friendly, command hints shown on screen.vim/vi– powerful, modal editor, steeper learning curve.emacs– very extensible, almost an entire environment.
Command-line editors are essential when:
- Working on servers without a GUI
- Editing system configs in
/etcfrom a terminal - Fixing issues when the desktop environment isn’t available
How to actually use nano and vim will be addressed in the dedicated subsections, but you should now understand:
- When to view vs when to edit
- Which viewing tools fit which situation
- How to safely inspect and make tiny non-interactive changes to files