YouTube Icon

Code Playground.

Linux Head Command


Linux Head Command

The head order prints the main lines (10 lines as a matter of course) of at least one records or channeled information to standard yield. 

In this instructional exercise, we will disclose how to utilize the Linux head utility through functional models and itemized clarifications of the most widely recognized head choices. 

Head Command Syntax

The sentence structure for the head order is as per the following: 

head [OPTION]... [FILE]...

Alternative - head choices . We will go over the most widely recognized alternatives in the following areas. 

Document - at least zero info record names. In the event that no FILE is determined, or when FILE is - , head will peruse the standard info. 

How to Use the Head Command

In its most straightforward structure when utilized with no alternative, the head order will show the initial 10 lines. 

head filename.txt

How to Display a Specific Number of Lines

Utilize the - n (- - lines) alternative followed by a whole number indicating the quantity of lines to be appeared: 

head -n <NUMBER> filename.txt

You can discard the letter n and utilize only the hyphen (- ) and the number (with no space between them). 

To show the initial 30 lines of a document named filename.txt you would type: 

head -n 30 filename.txt

The accompanying will deliver similar outcome as the above orders: 

head -30 filename.txt

How to Display a Specific Number of Bytes

The - c (- - bytes) alternative permits to print a particular number of bytes: 

head -c <NUMBER> filename.txt

For instance to show the initial 100 bytes of information from the record named filename.txt you would type: 

head -c 100 filename.txt

You can likewise utilize a multiplier addition after the number to determine the quantity of bytes to be appeared. b duplicates it by 512, kB increases it by 1000, K duplicates it by 1024, MB duplicates it by 1000000, M increases it by 1048576, etc. 

The accompanying order will show the initial five kilobytes (2048) of the document filename.txt: 

head -c 5k filename.txt

How to Display Multiple Files

In the event that numerous documents are given as contribution to the head order, it will show the initial ten lines from each gave record. 

head filename1.txt filename2.txt

You can utilize similar alternatives as while showing a solitary document. 

This model shows the initial 20 lines of the records filename1.txt and filename2.txt: 

head -n 20 filename1.txt filename2.txt

At the point when more than one document is utilized, the yield go before each with a header demonstrating the record name. 

How to Use Head with Other Commands

The head order can be utilized in mix with different orders by diverting the standard yield from/to different utilities utilizing pipes. 

The accompanying order will hash the $RANDOM climate variable , show the initial 32 bytes and show 24 characters arbitrary string: 

echo $RANDOM | sha512sum | head -c 24 ; echo


At this point you ought to have a decent comprehension of how to utilize the Linux head order. It is integral to the tail order which prints the last lines of a record to the to the terminal.