Table of Contents
Introduction
Working with text files is a central skill on a Linux system. Configuration files, scripts, logs, and many tools all use plain text. In this chapter you learn how to look at the contents of files, inspect them efficiently, and make simple edits directly from the terminal. Graphical editors belong to the graphical environment, so here the focus is on tools that work in a terminal window or a text-only session.
What “viewing” a text file really means
When you open a graphical editor, it reads a file into memory and shows it on screen. In the terminal, viewing tools usually do something similar, but they concentrate on showing text in a compact way and on handling large files gracefully.
A text file is simply a sequence of characters, usually arranged as lines ending with a newline character. Viewing tools read these lines and write them to your terminal. Although you could use a general command such as cat to dump text directly, specialized tools let you scroll, search, or show only the part you care about.
The commands you use to view text do not change the file. They only read it. Editing is a separate operation that writes modified content back to disk.
Viewing files with `cat` and friends
The simplest way to see a file is with cat, which copies a file’s contents to standard output.
For example:
cat file.txt
This prints every line in file.txt to your terminal. This works well for short files. For long files it can scroll past too quickly.
There are a few variations that build on this idea without becoming full pagers. One is nl, which shows line numbers alongside the content. Another is tac, which displays lines in reverse order, starting from the bottom of the file. These are useful when the order of lines matters but you still want a single pass over the file.
If a file contains very long lines or non-printable characters, cat offers options to show these more clearly. These details are often useful when debugging scripts or configuration, even though cat remains a basic viewing command.
Paging through files with `less`
When a file does not fit on a single screen, it is more practical to use a pager. The standard pager on modern Linux systems is less. Although its interface resembles that of an old command called more, less is more flexible and interactive.
To use it, you run:
less file.txt
less shows the first page of the file and then waits for you. The file is not opened for editing. You move through the text with keys that are similar to many text interfaces. For example, you can move line by line or page by page. You can scroll backward as well as forward, which is important when you are inspecting logs or long configuration files.
Pagers are more than simple viewers, because they let you search within the file while you look at it. less has a search prompt that appears when you type a search character, then your pattern. The tool jumps to the next match and can continue searching further. This makes less one of the best ways to explore unfamiliar files, look for specific settings, or scan logs for error messages.
When you finish, you exit back to the shell and the prompt appears again. The file on disk remains unchanged.
Looking at only the beginning or end
Often you do not need an entire file. You might only want to see how it starts or what has just been added. For this there are two standard commands, head and tail.
The head command shows the first part of a file. Used without options, it prints a default number of lines from the top. Similarly, tail shows the last part of the file. You can ask for a specific number of lines. This is particularly useful when you want to quickly confirm the format of a file or to check the latest entries in a log without opening the whole file.
There is a further feature of tail that is valuable on live systems. It can continue to run while new lines are appended to the file, updating your screen as they appear. This behavior mimics the effect of watching a file grow in real time, which is convenient for monitoring a program that writes logs or for following a long running operation that outputs its status to a file.
Although head and tail are simple tools, they play an important role in scripts and manual work, because they allow you to limit the amount of data you see and focus on the most relevant part of a file.
Combining viewing commands with redirection
Viewing tools do not have to take a file name directly. On Linux, many commands write their output to standard output, and you can feed that output into a viewer. For example, you can pipe the output of a command into less to page through it, or into head to see only the beginning.
This pattern is very common: instead of saving intermediate results to a file, you connect tools directly. In practice this means you can use text viewing techniques not only on files but also on the output of virtually any command.
The separate chapter on input and output redirection covers the details of pipes and redirection, but when learning to view text it is worth noticing that the same tools you use for files also help you manage the output of other programs.
Editing text from the terminal
Viewing is read only. To actually change text you need an editor. On many distributions there are several editors available, and different users prefer different tools. In this course the focus is on two common terminal editors, nano and vim, but only at the introductory level. More complex editing, macros, and configuration belong to later practice.
When you edit a file in a terminal, the editor process takes control of your terminal window. It shows some or all of the file and interprets keys you press as editing commands. When you save, the editor writes the modified content back to disk. If something interrupts this process, such as power loss or killing the editor, the file may remain unchanged or partly written, depending on how the editor handles backups and temporary files.
Because so many critical files on a Linux system are plain text, learning to edit text confidently is essential for administration, scripting, and development. The basic editors introduced in this part of the course are designed to help you take the first steps without needing a graphical environment.
Choosing between viewing and editing tools
New users sometimes open a full editor just to read a file. This is possible, but not ideal. Editing tools are heavier, and they may warn about file permissions or unsaved changes when you only wanted to look. On the other hand, using a pure viewer when you actually need to modify a line leads to switching tools repeatedly.
A simple guideline is to decide your intent in advance. If you only need to read or search, start with a viewer such as less. If you already know you must change something, open a text editor directly. As you become more familiar with your preferred tools, you will learn how quickly you can move from viewing to editing and back.
Important: Commands that only read a file, such as cat, less, head, and tail, never change its contents. Commands that edit a file, such as text editors, write data back to disk and can permanently alter configuration or scripts. Always be sure which type of command you are using before running it on important files.
Integrating text work into your command line habits
As you continue through the command line section of this course you will combine viewing and editing with navigation, searching, and scripting. You will often find yourself listing a directory, opening a file in a viewer to identify a problem, then switching to an editor to fix it. Later, you may automate parts of this workflow with scripts that filter text automatically.
The viewing and editing tools introduced in this chapter form part of a larger toolbox that makes text a flexible and powerful medium in Linux. Once you are comfortable with them, most of the other command line skills build naturally on this foundation.