A comprehensive guide to Linux commands for viewing file contents, including cat, more, head, tail, and wc, with examples of how to display full files page through content, and count words and lines.
This document explores essential Linux commands for viewing and analyzing file contents. It covers displaying entire files with cat, navigating through large files with more, viewing specific portions with head and tail, and analyzing file statistics with wc. These commands provide flexible options for efficiently examining file content in various situations, from quick inspections to detailed analysis.
The cat command is one of the most fundamental tools for viewing file contents in Linux. It concatenates and displays file contents to standard output.
The basic syntax of the cat command is:
1cat [options] filename
When used on a text file, cat displays the entire file content in the terminal:
1$ cat numbers.txt
20
31
42
53
6...
797
898
999
The cat command is ideal for viewing small files whose content fits within a single terminal window. For larger files, the output may scroll quickly, making it difficult to read the entire content.
When dealing with larger files, using cat can be problematic because:
For these situations, Linux provides alternative commands that offer more control over how file contents are displayed.
For files that are too large to view comfortably with cat, Linux provides paging utilities that allow reading content one screen at a time.
The more command displays file contents one screen (page) at a time:
1more numbers.txt
When viewing a file with more:
The more command is useful for reading through large files at a comfortable pace. However, it only allows forward navigation through the file.
The more command provides basic navigation capabilities:
| Key | Action |
|---|---|
| Space bar | Display next page |
| Enter | Move down one line |
| b | Go back one page |
| /pattern | Search for a pattern |
| q | Quit and return to shell |
While not mentioned in the transcript, it’s worth noting that the less command is a more powerful alternative to more. It allows both forward and backward navigation and offers more features. The less command has become the standard pager in most Linux distributions:
1less numbers.txt
Sometimes only specific parts of a file are of interest. Linux provides commands to view just the beginning or end of files.
The head command displays the first few lines of a file. By default, it shows the first 10 lines:
1$ head numbers.txt
20
31
42
53
64
75
86
97
108
119
To specify a different number of lines, use the -n option followed by the desired number:
1$ head -n 3 numbers.txt
20
31
42
The head command is particularly useful for:
The tail command displays the last few lines of a file. By default, it shows the last 10 lines:
1$ tail numbers.txt
290
391
492
593
694
795
896
997
1098
1199
Similar to head, the number of lines can be customized with the -n option:
1$ tail -n 3 numbers.txt
297
398
499
The tail command is especially valuable for:
Although not covered in the transcript, it’s worth mentioning that tail has a special option -f (follow) that continuously displays new lines added to a file:
1tail -f logfile.txt
This is extremely useful for monitoring active log files in real-time.
Beyond viewing content, Linux provides tools to analyze files statistically.
The wc (word count) command provides statistics about a file’s content:
1$ wc pets.txt
27 7 28 pets.txt
The output shows:
The character count includes invisible characters such as newlines, which explains why a file with 7 lines and 7 single-character words would have 28 characters rather than 14 (7 words + 7 newlines).
The wc command can be used with specific options to display only certain statistics:
| Option | Description | Example |
|---|---|---|
-l | Count lines only | wc -l pets.txt |
-w | Count words only | wc -w pets.txt |
-c | Count characters only | wc -c pets.txt |
-m | Count characters (UTF-8) | wc -m pets.txt |
These options are useful for specific analytical needs:
1$ wc -l pets.txt
27 pets.txt
3
4$ wc -w pets.txt
57 pets.txt
6
7$ wc -c pets.txt
828 pets.txt
The wc command is particularly valuable for:
Linux provides a versatile set of commands for viewing and analyzing file contents to suit different needs. The cat command offers simple full-file display for smaller files, while more provides paged viewing for larger content. The head and tail commands allow examining specific portions of files, and wc provides statistical information about file content. Mastering these commands enables efficient file examination and analysis in the Linux environment, whether for quick checks of file content or more detailed analysis of text data.
(2) The more command is specifically designed for viewing files that are too large to fit on a single screen. It displays content one page at a time, allowing users to navigate through the file at their own pace with keyboard controls. This is particularly useful for large text files where using cat would cause most of the content to scroll off the screen too quickly to read.
(3) The tail -f command (where -f stands for “follow”) displays the last 10 lines of a file and then continues to show new lines as they are added to the file in real-time. This is particularly useful for monitoring log files like /var/log/syslog, as it allows administrators to see new log entries as they occur without having to repeatedly run the command.
tail /path/to/application.log. The tail command is designed to display the last portion of a file (by default, the last 10 lines), which is ideal for checking recent entries in log files. For continuous monitoring of new errors as they occur, tail -f /path/to/application.log would be even better, as it follows the file and displays new entries in real-time.(4) The wc (word count) command cannot search for specific words or patterns in a file. Its function is limited to counting lines, words, and characters in files. For searching patterns or specific words, Linux provides other commands like grep, which is specifically designed for pattern searching.
The head command with no options displays the first 5 lines of a file by default.
False. The head command with no options displays the first 10 lines of a file by default, not 5. To display a different number of lines, the -n option must be used, as in head -n 5 filename.txt to display the first 5 lines.
| Command | Function |
|---|---|
| A. cat | 1. Displays the last portion of a file |
| B. head | 2. Displays file contents one page at a time |
| C. tail | 3. Displays the entire contents of a file |
| D. more | 4. Displays the first portion of a file |
A-3, B-4, C-1, D-2. The cat command displays the entire contents of a file, head displays the first portion (default 10 lines), tail displays the last portion (default 10 lines), and more displays file contents one page at a time with navigation controls.
(3) The fact that wc counts newline characters in its character count indicates that Linux treats invisible characters (like newlines) as significant parts of text files. This reflects the Unix/Linux philosophy that all characters in a file are meaningful, including those that control formatting and are not directly visible when displayed.
wc -l *.txt | sort -nr to determine which file contains the most lines of text. This command first uses wc -l to count the number of lines in each text file, then pipes the output to sort -nr, which sorts the results numerically in reverse order (largest first). The file with the most lines will appear at the top of the output. This approach is particularly useful for comparing the sizes of multiple files in terms of content rather than byte size.head -n 20 file.txt | tail -n 5), the command first extracts the specified number of lines from the beginning of the file using head, then pipes that output to tail which extracts the specified number of lines from the end of that output. This combination effectively selects lines from the middle of a file. For example, the command given would display lines 16-20 of file.txt (the last 5 lines of the first 20 lines). This technique is useful for viewing specific sections of files without having to open them in a text editor.