How to use the command 'head' (with examples)

How to use the command 'head' (with examples)

The ‘head’ command is used to output the first part of files. It is commonly used to display the first few lines of a file. By default, the ‘head’ command outputs the first 10 lines of a file, but this can be customized using the ‘-n’ option.

Use case 1: Output the first few lines of a file

Code:

head -n count path/to/file

Motivation: Sometimes, we may only need to view the beginning of a file rather than the entire contents. This can be useful when dealing with large files or log files.

Explanation:

  • ‘-n’ specifies the number of lines to display. In the example code, you should replace ‘count’ with the desired number of lines.
  • ‘path/to/file’ is the path to the file you want to view. Replace it with the actual file path on your system.

Example output: Suppose we have a file named ’example.txt’ with the following content:

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

If we run the command head -n 3 example.txt, the output will be:

Line 1
Line 2
Line 3

Conclusion:

The ‘head’ command is a handy tool for quickly viewing the first part of a file. Whether you need to check the beginning of a log file or preview the content of a large file, the ‘head’ command provides a convenient way to accomplish these tasks.

Related Posts

How to use the command ipconfig (with examples)

How to use the command ipconfig (with examples)

The ipconfig command is used to view and control IP configuration state on a device.

Read More
How to use the command 'sort' (with examples)

How to use the command 'sort' (with examples)

The sort command is a powerful tool for sorting lines of text files.

Read More
How to use the command sdiag (with examples)

How to use the command sdiag (with examples)

The sdiag command is used to show information about the execution of slurmctld.

Read More