Skip to content

head MacOS command

The MacOS head command displays the first part of a file in the terminal. It is useful for quickly previewing the content of a file without opening it fully. By default, head displays the first 10 lines of a file, but you can specify a different number of lines to show. This command is handy for analyzing log files, checking configurations, and previewing the structure of a file.

head Syntax:

Terminal window
head [option] [file]

head Options:

OptionDescription
-nSpecify the number of lines to display. Default is 10 lines.
-cSpecify the number of bytes to display.
-qNever print headers giving file names.
-vAlways print headers giving file names.

Parameters:

ParameterDescription
fileThe file to display the beginning content of.

head Command Usage Examples:

Display the First 10 Lines of a File

Terminal window
head file.txt

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

Display a Specific Number of Lines from the Beginning of a File

Terminal window
head -n 5 file.txt

This will display the first 5 lines of the file.txt file.

Display Multiple Files with the First 10 Lines Each

Terminal window
head file1.txt file2.txt file3.txt

Displays the first 10 lines of file1.txt, file2.txt, and file3.txt.

Display Lines from a File without the Final Newline Character

Terminal window
head -c -1 file.txt

Display the contents of file.txt without the final newline character.

Display the First 20 Lines of a Command Output

Terminal window
ls -l | head -n 20

Displays the first 20 lines of the output generated by the “ls -l” command.

How do I use head in MacOS?

To use the head command in bash, execute the following command:

Terminal window
head file.txt

How can I display a specific number of lines with head in MacOS?

To display a specific number of lines (e.g., 5 lines) of a file using head in MacOS, use the “-n” option followed by the number of lines:

Terminal window
head -n 5 file.txt

How can I show the first part of multiple files with head in MacOS?

To display the beginning of multiple files simultaneously using the head command in MacOS, you can specify the file names as arguments:

Terminal window
head file1.txt file2.txt file3.txt

How can I display the contents of a file except for the first few lines with head in MacOS?

To display the contents of a file except for the first few lines, you can use the combination of head and tail commands. For instance, to skip the first 5 lines and display the rest of the file:

Terminal window
tail -n +6 file.txt

How can I show the first part of a file along with line numbers in MacOS using head?

To display the first part of a file with line numbers shown, you can pipe the output of head to the nl command. For example, to view the first 10 lines of a file with line numbers:

Terminal window
head file.txt | nl

How can I display the last part of a file in reverse order using head in MacOS?

To display the last part of a file in reverse order, you can combine the head and tac commands in MacOS. For example, to show the last 5 lines of a file in reverse:

Terminal window
head -n 5 file.txt | tac

How can I display the first part of a file with specific delimiter characters using head in MacOS?

To display the beginning of a file with specific delimiter characters, you can use the “-c” option to specify the number of bytes to print in MacOS. For instance, to show the first 100 bytes of a file:

Terminal window
head -c 100 file.txt

Applications of the head command

  1. Display the first few lines of a file
  2. Extract the header of a CSV or TSV file
  3. View the beginning of a large log file
  4. Check the contents of a script or configuration file without opening it entirely
  5. Print specific lines from the beginning of a text file