Linux – head Command

The head command is used to display the first few lines of a file. By default, it shows the first 10 lines, but you can customize the number of lines to display. This command is useful for quickly viewing the start of a file without opening the entire file.



Tutorials dojo strip

Initial Example

Using head to display the first 10 lines of a file called example.txt:

head example.txt




HEAD Parameter

ParameterDescription
-c, --bytes=[-]NUMDisplay the first NUM bytes of each file; use a leading ‘-‘ to exclude the last NUM bytes
-n, --lines=[-]NUMShow the first NUM lines instead of the default 10; use a leading ‘-‘ to exclude the last NUM lines
-q, --quiet, --silentSuppress the display of file name headers
-v, --verboseAlways show file name headers, even with a single file
-z, --zero-terminatedUse NUL as the line delimiter instead of newline
--helpDisplay help information and exit
--versionShow version information and exit




Examples

1. Display the First 10 Lines of a File

To view the first 10 lines of a file (default behavior):

head filename.txt

2. Display a Specific Number of Lines

To display a specific number of lines from the beginning of a file, use the -n option followed by the number of lines:

head -n 5 filename.txt

3. Display the First Few Bytes of a File

To display the first few bytes of a file, use the -c option followed by the number of bytes:

head -c 20 filename.txt

4. Combine Options

You can combine options to tailor the output to your needs. For example, to display the first 3 lines and the first 20 bytes of a file:

head -n 3 -c 20 filename.txt

5. Display the First 10 Lines of Multiple Files

To display the first 10 lines of multiple files, specify the file names separated by spaces:

head file1.txt file2.txt




Linux Playground

Scroll to Top