Table of Contents
Introduction
On a Linux system you will constantly need to look at text files. Configuration files, logs, scripts, and documentation are all plain text. Before you learn to edit these files, you need to become comfortable viewing them efficiently from the command line.
This chapter focuses on tools that show file contents without changing them. You will see how to display whole files, scroll through large ones, preview only the beginning or the end, and follow files that keep growing, as log files often do.
Printing a Whole File with `cat`
The simplest way to display the contents of a text file is the cat command. Its basic form is:
cat filename
cat sends the file contents to the terminal. It does not provide scrolling or search, it just prints. If the file is longer than your screen, older lines scroll off the top.
You can use cat with multiple files to show them one after another:
cat file1 file2
This prints file1 first, then directly continues with file2. cat is useful for small files or when you want to quickly pass contents into another command through pipes, which you will see elsewhere.
Viewing the Beginning with `head`
Sometimes you only need to see how a file starts, for example the header of a log file or the structure of a configuration file. The head command prints just the first lines.
By default, head filename shows the first 10 lines of filename.
To choose a different number of lines, use the -n option:
head -n 20 filename
This shows the first 20 lines. You can also use a shorter form such as head -5 filename to show 5 lines, but -n is easier to remember and clearer.
head is especially useful with large files because it avoids loading and drawing the entire file in your terminal.
Viewing the End with `tail`
Log files and other data files often have the most relevant information at the end. The tail command prints the last lines of a file.
By default, tail filename shows the last 10 lines.
As with head, you can change the number of lines:
tail -n 25 filename
shows the last 25 lines. You can quickly inspect recent log entries or the latest part of a report while ignoring the earlier content.
tail reads from the end of the file, so it is very fast even with very large files.
Following a Growing File with `tail -f`
For logs that are constantly updated, like /var/log/syslog or application logs, you often want to watch new lines appear as they are written.
The option -f turns tail into a live viewer:
tail -f /path/to/logfile
The terminal will show the last lines, then wait. As the file grows, each new line is printed instantly. This is very useful when monitoring a service while you start or restart it.
To stop following the file, use Ctrl + C to interrupt tail and return to the shell prompt.
You can combine -f with -n:
tail -n 50 -f /path/to/logfile
This starts by showing the last 50 lines, then continues to follow new ones.
Important rule: Use Ctrl + C to stop tail -f when you are done watching a file. Otherwise the command will keep running and keep your terminal busy.
Paging Through Files with `less`
For most real-world work, files are too large for a simple cat, and just seeing the top or bottom is not enough. You need a way to scroll back and forth and to search. The less command is the standard solution.
Run it as:
less filename
less loads the file into a pager interface. It does not show a shell prompt. Instead, your terminal becomes a viewer where you can navigate with the keyboard. This does not edit the file, it only views it.
At the bottom of the screen you will see : or some brief status information. To exit less and return to the shell, press q.
Navigating in `less`
Inside less, you can move around the file without changing it. Some common keys are:
Use the up and down arrow keys to scroll one line at a time. Use Page Up and Page Down or Space to move one screen at a time. You can also use b to go back a screen. To jump to the very beginning, press g. To jump straight to the end, press G.
If you resize your terminal window, less will adapt and continue to display the correct number of lines on the screen.
less only reads as much of the file as it needs, which makes it responsive even with very large files.
Searching in `less`
less can search for text patterns within the file. While viewing a file, press / to search forward.
For example, press / then type error and press Enter. less will jump to the next occurrence of the word error after your current position. The matched text is highlighted depending on your terminal settings.
After a search, press n to go to the next match, or N to go to the previous match.
If you want to search backward from your current position, use ? instead of /. For instance, ?error followed by Enter searches upward in the file.
You do not modify the file by searching. You simply move your view around inside it.
Viewing from Standard Input with `less`
less can also read data from other commands, which is very helpful when you are working with pipelines. Instead of giving less a file name, you can use:
somecommand | less
This takes the output of somecommand and opens it in the less pager so you can scroll, search, and navigate.
For example, if another command produces long output that would scroll off the screen, you can run:
dmesg | less
to comfortably inspect it.
To quit this view, press q as usual.
Using `more` as a Simple Pager
more is an older pager similar to less. Its basic use is:
more filename
It shows one screen at a time and waits for you to press Space to move to the next screen. You can use Enter to move down one line. To exit, you can press q.
more has fewer features than less, especially for backward scrolling and advanced search. Because of that, many users prefer less, and on many systems the saying is that "less is more".
You will still see more in scripts and in some old documentation, so it is useful to recognize it, but for interactive work less is usually the better choice.
Viewing Non-printable Characters
Sometimes you need to see hidden characters in a file, such as tab characters or carriage returns that affect how the file behaves. You can use cat with the -A option to reveal these:
cat -A filename
In this mode, tab characters show as ^I, line endings are marked, and other non-printable characters are displayed in a visible form. This is useful when debugging formatting issues, especially in scripts or configuration files.
Do not use this mode for normal reading, but remember it when things look correct visually but do not work as expected.
Combining Viewing Tools Efficiently
These viewing tools are often used together, each for a different purpose. cat is helpful for quick checks of small files or for joining files. head and tail give you fast access to the start or the end of large files. tail -f lets you monitor files as they change in real time. less is the main interactive viewer for exploring and searching within files. more provides a simpler pager that you might encounter, and cat -A can reveal special characters.
As you progress, you will often combine these tools with redirection and pipes when working with more complex command lines. For now, focus on becoming comfortable with their basic behavior and navigation so that you can inspect any text file effectively from the terminal.