Enhancing Skills

head: Display the first few lines of a file

Command: head

Used to display the first few lines of a file. By default, it shows the first 10 lines.


Sample Command and Output:

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

Description:

  • head file1.txt: Displays the first 10 lines of file1.txt. This is useful for quickly viewing the beginning of a file.

Sample Command and Output:

$ head -n 5 file1.txt
This is the first line of file1.txt.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.

Description:

  • head -n 5 file1.txt: Displays the first 5 lines of file1.txt. The -n option allows specifying the number of lines to display.

Sample Command and Output:

$ head -c 20 file1.txt
This is the first 20 bytes of

Description:

  • head -c 20 file1.txt: Displays the first 20 bytes of file1.txt. The -c option allows specifying the number of bytes to display instead of lines.

Additional Arguments:

  • -q: Suppresses the output of file headers when multiple files are listed.
  • -v: Always outputs the file headers, even when only one file is being displayed.

Sample Command and Output:

$ head -q file1.txt file2.txt
==> file1.txt <==
This is the first line of file1.txt.
...

==> file2.txt <==
This is the first line of file2.txt.
...

Description:

  • head -q file1.txt file2.txt: Displays the first 10 lines of both file1.txt and file2.txt without showing file headers for each file. The -q option suppresses file names in the output.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.