How to Display the First Few Lines of a File in Ubuntu

The Data Parsing Vector

When operating an Ubuntu Linux server, administrators frequently need to inspect the contents of massive system logs, configuration files, or database dumps. Attempting to open a 10-Gigabyte log file using a standard text editor like nano or vim will instantly consume all available RAM and crash the server. If you only need to verify the formatting of the data, or read the initial configuration parameters declared at the top of a script, you require a tool that extracts only the beginning of the file without loading the entire payload into memory.

How to Display the First Lines of a File

The standard, highly efficient utility designed specifically for this task is the head command.

1. Open your terminal application or connect to your server via SSH.
2. Navigate to the directory containing your massive text file.
3. To instantly dump the first 10 lines of the file (the default behaviour) to your terminal screen, execute the command followed by the filename:

head access.log

4. Adjusting the Output: If you need to see more or fewer lines, you must use the -n (number) flag followed by the exact number of lines you require. For example, to view the first 50 lines of the file, type:

head -n 50 access.log

5. Multiple Files: You can also feed the command multiple files simultaneously. The terminal will clearly print a header indicating which file it is reading before displaying the first 10 lines of each:

head -n 5 error.log access.log

The head command operates instantaneously, regardless of how massive the target file is, as it immediately terminates its read operation the moment it hits the designated line limit.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.