Commands.page Logo

How to Show First 10 Lines of a Log File in Ubuntu

Managing log files in Ubuntu often requires checking recent entries or specific segments of data to diagnose issues. This guide explains the simplest method to view the beginning of a log file using the command line. You will learn how to use the head command to display exactly the first 10 lines of any text-based log file efficiently.

Use the Head Command

The primary tool for viewing the start of a file in Linux is the head command. By default, this command displays the first 10 lines of a file, but it is best practice to specify the line count explicitly. Open your terminal and type the following syntax:

head -n 10 /path/to/your/logfile

Replace /path/to/your/logfile with the actual location of your log file. The -n flag specifies the number of lines to output.

Handling System Logs

Many system log files in Ubuntu are stored in the /var/log directory and require administrator privileges to access. If you attempt to view a protected file without permissions, you will receive a “Permission denied” error. To resolve this, prepend sudo to your command.

For example, to view the first 10 lines of the main system log, run:

sudo head -n 10 /var/log/syslog

You will be prompted to enter your user password. Once authenticated, the terminal will display the requested lines immediately. This method is efficient for large files because it reads only the necessary data rather than loading the entire file into memory.