head/tail

>Usage

The head and tail commands allow users to view specific sections of a file. head displays the beginning of a file, while tail shows the end. They are commonly used for quickly previewing files or monitoring log files in real-time.

Multiple options can be used e.g tail -n 20 -f logfile.txt To return the last 20 lines of a log file.

>Synopsis

head [option] file
tail [option] file

>Options

head

The head command displays the first 10 lines of a file by default

Example:

Dots

-zsh

> head destination_folder/example.txt


$ head destination_folder/example.txt
This is the first line of example.txt.
This is the second line of example.txt.
This is the third line of example.txt.
This is the fourth line of example.txt.
This is the fifth line of example.txt.
This is the sixth line of example.txt.
This is the seventh line of example.txt.
This is the eighth line of example.txt.
This is the ninth line of example.txt.
This is the tenth line of example.txt.
$

tail

The tail command displays the last 10 lines of a file by default.

Example:

Dots

-zsh

>tail destination_folder/example.txt


$ tail destination_folder/example.txt
This is the tenth line of example.txt.
This is the ninth line of example.txt.
This is the eighth last of example.txt.
This is the seventh last of example.txt.
This is the sixth last of example.txt.
This is the fifth last of example.txt.
This is the fourth last of example.txt.
This is the third last of example.txt.
This is the second last line of example.txt.
This is the last line of example.txt.
$

-n

Both head and tail allow you to specify the number of lines to display using the -n option.

Example:

Dots

-zsh

> tail -n 5 myfile.txt


This is the fifth last line of example.txt.
This is the fourth last line of example.txt.
This is the third last line of example.txt.
This is the second last line of example.txt.
This is the first last line of example.txt.

+

Using + with head/tail -n allows you to start displaying from a specific line number. This command starts displaying a file's content from this number and onward.

Example:

Dots

-zsh

> tail -n +20 myfile.txt


(There are 25 lines totally in this file)
This is the 20th line.
This is the 21st line.
This is the 22nd line.
This is the 23rd line.
This is the last line.

-f

Using -f with head/tail -f is useful for monitoring log files (Log files in Unix are files where the system, applications, and services record events, errors, and other information.) as they are updated. This command displays the last 10 lines of a file and updates the output in real-time as new lines are added to the file. To stop monitoring the file, simply press Ctrl + C to terminate the command. This will bring you back to the regular terminal prompt.

Example:

Dots

-zsh

> tail -f logfile.txt


2024-11-16 10:00:01 [INFO] Application started
2024-11-16 10:00:15 [INFO] User logged in: user1
2024-11-16 10:02:00 [ERROR] Failed to load configuration
2024-11-16 10:02:10 [INFO] User logged in: user2
2024-11-16 10:05:00 [INFO] Task completed
2024-11-16 10:06:30 [INFO] User logged out: user1
2024-11-16 10:07:00 [INFO] Application shutdown
2024-11-16 10:10:00 [INFO] Application started
2024-11-16 10:11:00 [INFO] User logged in: user3
2024-11-16 10:12:30 [ERROR] Task execution failed

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Sweetmoreton Beryl