Viewing File Contents (cat, less, head, tail)
In this lesson, we will learn how to view the contents of files using different Linux commands. Understanding how to view and navigate file contents is an essential skill for any Linux user. The commands we’ll cover are cat
, less
, head
, and tail
. These commands allow you to display file contents in various ways, each suited to different use cases.
The cat
Command: Concatenate and Display File Contents
The cat
(short for "concatenate") command is the most commonly used command to display the contents of a file in the terminal. It is useful for viewing small files or combining the contents of multiple files.
Basic Usage
To display the contents of a file, simply use:
$ cat filename
For example, to view the contents of a file named file1.txt
:
$ cat file1.txt
This will output the entire content of file1.txt
to the terminal.
Concatenate Multiple Files
You can also concatenate (combine) multiple files into one and display them in sequence:
$ cat file1.txt file2.txt
This will display the contents of file1.txt
followed by file2.txt
.
Redirect Output to Another File
You can redirect the output of cat
to create or overwrite a file:
$ cat file1.txt > newfile.txt
This command will copy the contents of file1.txt
into newfile.txt
. If newfile.txt
already exists, it will be overwritten.
The less
Command: View Large Files Page by Page
The less
command is used for viewing larger files that may not fit entirely on the screen. It allows you to scroll through the content interactively, one page at a time.
Basic Usage
To view a file with less
, simply use:
$ less filename
For Example:
$ less file1.txt
This will open file1.txt
in a scrollable interface. You can navigate through the file using the following keys:
- Arrow keys or j/k to move down/up line by line.
- Space to move down one page.
- b to move up one page.
- q to quit and exit
less
.
Searching Within the File
To search for a term within the file, press /
followed by the search term:
/term
Then, press Enter to search. You can use n to jump to the next match and N to go to the previous match.
The head
Command: View the Beginning of a File
The head
command is used to view the first few lines of a file. By default, it shows the first 10 lines of the file.
Basic Usage
To view the first 10 lines of a file, use:
$ head filename
For example:
$ head file1.txt
This will display the first 10 lines of file1.txt
.
Viewing More or Fewer Lines
You can specify the number of lines to display using the -n
option. For instance, to view the first 5 lines:
$ head -n 5 file1.txt > first_five_lines.txt
This command will save the first 5 lines of file1.txt
into a new file called first_five_lines.txt
.
The tail
Command: View the End of a File
The tail
command is used to view the last few lines of a file. This is especially useful for monitoring log files or files that are constantly being updated.
Basic Usage
To view the last 10 lines of a file (default behavior), use:
$ tail filename
For example:
$ tail file1.txt
This will display the last 10 lines of file1.txt.
Viewing More or Fewer Lines
To view a different number of lines at the end of the file, use the -n
option:
$ tail -n 5 file1.txt
This will display the last 5 lines of file1.txt
.
Following a File in Real Time
The -f
option allows you to follow the content of a file in real-time, which is useful for monitoring log files that are constantly updated. For example, to follow logfile.log
:
$ tail -f logfile.log
This will show the last 10 lines of logfile.log
and continue to display new lines as they are added to the file.
Combining Options
You can combine the -n
and -f
options to view the last few lines and follow the file simultaneously:
$ tail -n 20 -f logfile.log
This will display the last 20 lines of logfile.log
and continue to show new lines as they are appended.
Conclusion
In this lesson, we explored four essential commands for viewing file contents in Linux:
cat
for viewing file contents and concatenating files.less
for viewing large files with interactive scrolling.head
for viewing the first few lines of a file.tail
for viewing the last few lines of a file and monitoring file updates in real-time.
These commands are fundamental for managing and inspecting files in Linux, and you'll use them frequently while working on the command line.