But what if you need a certain part or a few starting lines?
The head command prints the initial content of a file on the terminal.
What Is the head Command on Linux?
Hannah Stryker / How-To Geek
Linux has multiple commands to display file contents.
The most popular and frequently used arecat,less, andviewcommands.
However, these commands are more useful for displaying large portions of files.
Theheadcommand does the opposite of what thetailcommand does.
It shows the starting content of a file, while thetailcommand prints the ending lines of a file.
By default,headdisplays the first 10 lines.
If you want to print more or less than 10 lines, just use the-noption.
Similarly, the-coption with theheadcommand can restrict the output to a particular byte number.
Theheadcommand cananalyze logsand other text files that are subject to vary over time.
you’re able to use theheadcommand inconjunction with other commandsfor selective, real-time monitoring.
It also accepts some options that modify its behavior and output.
If no file is specified, theheadcommand reads from thestandard input.
Each option has a concise and extended form, to use with basic syntax.
It controls how much data theheadcommand prints to the normal output.
For example, it allows you to decide whether to include the header in the file output or not.
-cor–bytes
Shows the specified number of bytes from the start.
-vor–verbose
Prints each file name along with the file contents.
This is useful when displaying multiple files at once.
-qor–quiet
Suppresses the printing of file names before displaying their contents.
This is useful when displaying only one file or when piping the output to another command.
-zor–zero-terminated
Replace the newline character with NULL at the end of each line.
Before moving to the demonstration of theheadcommand, let’s first look at the content of the sample file.
Use thecatcommand followed by the filename to get all the data of the sample file.
This example file contains 15 lines of text.
Now, let’s print the content of the example.txt file using theheadcommand.
Theheadcommand, without any options, will print the first 10 lines of the file.
This will skip the last N lines of a file.
This is helpful when you want to skip some lines at the end of a file.
Consider you have the same file example.txt, and it contains 15 lines of text.
A negative number can also be defined with the-coption.
This will display all bytes of a file, except the last N bytes.
For this, you have to pipe theheadcommand with other text processing commands likecut,awk, orsed.
Use the pipe operator (|) to pipe bothheadandawkcommands.
In this way, the output of theheadcommand will serve as input to theawkcommand.
However, it can display the file name when used with multiple files.
Use the-voption to get the file name along with its content.
This option prints a header with the filename of the specified file.
Let’s take two files called example.txt and test.txt that contain multiple lines of content.
Now, theheadcommand will display both file names along with their content.
you could use theheadcommand with the-qoption to view the content of multiple files without displaying their names.
Using head With Other Commands
Theheadcommand can also be used with other commands to perform various tasks.
it’s possible for you to use it withtail,more,wc, andgrepcommands.
you could pipe theheadcommand withgrepto give you all the lines that contain the specified pattern.
The above syntax displays all lines in the example.txt file that contain “ch”.
you’ve got the option to also pipe theheadcommand with thewccommand.
Both these commands will output the count of total lines, words, and bytes in the file.
Theheadcommand shows the starting lines of a file, while thetailcommand prints the ending lines of a file.
Consider the example.txt file that contains 15 lines.
After that, it will pipe the output to thetail -n 5command.
Thetailcommand will give us the final output of entities that are between the 5th and 11th lines.
Want to Display Line Endings With head?
Theheadcommand, as its name implies, is primarily concerned with the initial lines of a file.
Conversely, thetailcommand serves the purpose of displaying the concluding lines of a text file.
It can also monitor a file and display each new text entry to that file as they occur.
Just like theheadcommand, you could also usetailto monitor multiple files or count the number of bytes.
It can also check a specific pattern or text inclusion in the text file.
This makes it a great tool to monitor log files.