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:
head [option] [file]head Options:
| Option | Description |
|---|---|
| -n | Specify the number of lines to display. Default is 10 lines. |
| -c | Specify the number of bytes to display. |
| -q | Never print headers giving file names. |
| -v | Always print headers giving file names. |
Parameters:
| Parameter | Description |
|---|---|
| file | The file to display the beginning content of. |
head Command Usage Examples:
Display the First 10 Lines of a File
head file.txtThis command will display the first 10 lines of the file.txt file.
Display a Specific Number of Lines from the Beginning of a File
head -n 5 file.txtThis will display the first 5 lines of the file.txt file.
Display Multiple Files with the First 10 Lines Each
head file1.txt file2.txt file3.txtDisplays the first 10 lines of file1.txt, file2.txt, and file3.txt.
Display Lines from a File without the Final Newline Character
head -c -1 file.txtDisplay the contents of file.txt without the final newline character.
Display the First 20 Lines of a Command Output
ls -l | head -n 20Displays 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:
head file.txtHow 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:
head -n 5 file.txtHow 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:
head file1.txt file2.txt file3.txtHow 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:
tail -n +6 file.txtHow 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:
head file.txt | nlHow 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:
head -n 5 file.txt | tacHow 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:
head -c 100 file.txtApplications of the head command
- Display the first few lines of a file
- Extract the header of a CSV or TSV file
- View the beginning of a large log file
- Check the contents of a script or configuration file without opening it entirely
- Print specific lines from the beginning of a text file