When you are troubleshooting a catastrophic database failure or auditing a massive web server, the system log files are often gigabytes in size, containing millions of individual lines of text. If you try to open a 5-gigabyte text file using standard commands like cat or nano, the terminal will instantly crash under the massive memory load. To mathematically force the Linux kernel to bypass the middle of the file and instantly dump only the absolute beginning of the data stream directly to your screen, you must use the head command.
How the head Command Works
The head command is a surgical extraction engine. It does not attempt to read or load the entire file into memory. Instead, it anchors itself to the absolute first byte of the file, mathematically counts exactly 10 lines downward, violently chops the file off at that exact point, and prints the data to the screen. It is incredibly fast and completely immune to file size.
To view the top of a massive log file named apache_access.log, type:
head apache_access.log
The terminal will instantly output exactly the first 10 lines of the file and then immediately stop processing.
Customizing the Extraction Depth
If 10 lines is not enough data to diagnose the server error, you can mathematically override the default extraction limit using the -n (number) flag.
To force the engine to extract exactly the first 50 lines of the file:
head -n 50 apache_access.log
The system will instantly output 50 lines.
Conversely, if you are writing a complex bash script and you only need to extract the absolute first line of a file (e.g., to grab a specific header or a date stamp), you can constrain the engine to a single line:
head -n 1 data.csv
The system outputs line 1 and instantly terminates.
Extracting by Byte Size
If you are analyzing raw binary files or deeply corrupted text streams where standard line breaks (newlines) do not exist, the -n flag is completely useless. You must force the engine to extract data based on raw mathematical byte count.
You achieve this using the -c (bytes) flag.
To extract exactly the first 100 bytes of a compiled binary file:
head -c 100 program.bin
The head engine will mathematically slice the first 100 bytes off the file and dump them directly to your screen, allowing you to instantly identify the file’s magic number or hidden header data without crashing the terminal.