Kahibaro
Discord Login Register

Viewing and Editing Text Files

Why Text Files Matter on Linux

Linux systems store an enormous amount of information as plain text:

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:

(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.

cat file.txt
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.

less /etc/passwd

Inside less:

Because it doesn’t load the whole file at once, less is ideal for very large logs or data files.

Useful options

less -N file.txt
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.

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.txt

Change 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.txt

Change 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.

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.

sed 's/old/new/' file.txt

This only displays the changed text; it doesn’t modify the file.

sed -i 's/old/new/' file.txt

-i means “edit in-place”.

Common variations:

sed -i 's/old/new/g' file.txt

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:

sed '3d' file.txt
sed '5,10d' file.txt

As before, add -i to modify in place.


Choosing the Right Tool

Different tools are suited to different tasks:

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

  1. Display the file with line numbers:
nl -ba /etc/ssh/sshd_config | less
  1. Note the line you want to change.
  2. 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

  1. View the end of the log:
sudo tail /var/log/syslog
  1. Follow new messages:
sudo tail -f /var/log/syslog
  1. Filter for a keyword:
sudo grep "failed" /var/log/syslog | less

Checking the first and last lines of a data file

head -n 5 data.csv
tail -n 5 data.csv

This quickly confirms whether the file looks as expected.


Basic Editor Selection Notes

You’ll encounter multiple editors on Linux; each has a different style:

Command-line editors are essential when:

How to actually use nano and vim will be addressed in the dedicated subsections, but you should now understand:

Views: 25

Comments

Please login to add a comment.

Don't have an account? Register now!