Linux head Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux head command, which is used to display the first few lines of a file. The lab covers understanding the basic usage of the head command, exploring its various options to customize its behavior, and applying it in real-world scenarios. You will learn how to quickly view the contents of a file, especially when dealing with large files, and how to use the head command to extract specific information from text-based data.

The lab provides a comprehensive overview of the head command, including examples of its usage and practical applications. By the end of the lab, you will have a solid understanding of this powerful text processing tool and be able to apply it effectively in your daily tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") subgraph Lab Skills linux/cat -.-> lab-422718{{"`Linux head Command with Practical Examples`"}} linux/head -.-> lab-422718{{"`Linux head Command with Practical Examples`"}} linux/tail -.-> lab-422718{{"`Linux head Command with Practical Examples`"}} linux/top -.-> lab-422718{{"`Linux head Command with Practical Examples`"}} end

Understand the head Command

In this step, you will learn about the head command in Linux, which is used to display the first few lines of a file. The head command is a useful tool for quickly viewing the contents of a file, especially when dealing with large files.

To use the head command, simply type head followed by the filename. For example:

head example.txt

This will display the first 10 lines of the example.txt file.

Example output:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

You can also specify the number of lines to display using the -n option. For example, to display the first 5 lines of the file:

head -n 5 example.txt

Example output:

Line 1
Line 2
Line 3
Line 4
Line 5

The head command is particularly useful when you need to quickly check the contents of a file, or when you want to view the top of a log file to see the most recent entries.

Explore head Command Options

In this step, you will explore the various options available with the head command to customize its behavior.

One useful option is -c, which allows you to display a specific number of bytes instead of lines. For example, to display the first 20 bytes of a file:

head -c 20 example.txt

Example output:

Line 1
Line

Another option is -q, which suppresses the filename header when multiple files are specified. This is useful when you want to concatenate the output of head for multiple files:

head -q -n 3 file1.txt file2.txt file3.txt

Example output:

Line 1 from file1.txt
Line 2 from file1.txt
Line 3 from file1.txt
Line 1 from file2.txt
Line 2 from file2.txt
Line 3 from file2.txt
Line 1 from file3.txt
Line 2 from file3.txt
Line 3 from file3.txt

You can also use the -v option to display the filename header, even when there is only one file:

head -v -n 3 example.txt

Example output:

==> example.txt <==
Line 1
Line 2
Line 3

Exploring these options will help you use the head command more effectively in your text processing and editing tasks.

Apply head Command in Real-World Scenarios

In this step, you will learn how to apply the head command in real-world scenarios to solve common text processing and editing tasks.

One common use case for the head command is to quickly view the top of log files. Let's try this with the system log file:

sudo head /var/log/syslog

Example output:

Feb 24 12:34:56 myhost systemd[1]: Starting System Logging Service...
Feb 24 12:34:56 myhost systemd[1]: Started System Logging Service.
Feb 24 12:34:56 myhost rsyslogd[123]: [origin software="rsyslogd" swVersion="8.2001.0" x-pid="123" x-info="https://www.rsyslog.com"] start
Feb 24 12:34:56 myhost rsyslogd[123]: rsyslogd's groupid changed to 108
Feb 24 12:34:56 myhost rsyslogd[123]: rsyslogd's userid changed to 104

Another common use case is to extract the top lines from the output of a command. For example, let's say you want to see the top 3 processes by CPU usage:

top -bn1 | head -n 5

Example output:

top - 12:34:56 up 1 day, 12:34,  0 users,  load average: 0.15, 0.05, 0.01
Tasks:  85 total,   1 running,  84 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.2 sy,  0.0 ni, 99.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   1969.3 total,    287.1 free,    654.9 used,   1027.3 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   1019.0 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    1 root      20   0    8572   5748   3900 S   0.0   0.3   0:01.22 systemd

By using the head command, you can quickly extract the top 5 lines of the top command output, which includes the system summary and the top processes.

These are just a few examples of how you can apply the head command in real-world scenarios. As you continue to work with text processing and editing tasks, you'll find many more opportunities to use this powerful command.

Summary

In this lab, you learned about the Linux head command, which is used to display the first few lines of a file. You explored the basic usage of the head command, including how to specify the number of lines to display. Additionally, you learned about various options available with the head command, such as displaying a specific number of bytes, suppressing the filename header when multiple files are specified, and displaying the filename header even when there is only one file.

You also explored how to apply the head command in real-world scenarios, where it can be a useful tool for quickly checking the contents of a file or viewing the top of a log file to see the most recent entries.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like