Head Command in Linux with Examples

Head Command in Linux

The Linux operating system, renowned for its robustness and flexibility, offers a plethora of commands to streamline your tasks. One such command is thehead‘ command, a powerful tool that displays the initial lines of a file. This command is particularly useful when dealing with large files where only the beginning portion is of interest.

Some examples of when you might use ‘head‘:

  • Checking log files for recent entries
  • Reading the latest entries in a large CSV or database file
  • Previewing the headers in a markup file like HTML or XML
  • Grabbing metadata from the first lines of a media file
  • Fetching sample data from a large dataset or corpus

Basic Usage of the ‘head’ Command

The ‘head‘ command, by default, displays the first 10 lines of a file. The syntax is straightforward: simply type ‘head‘ followed by the file name. For instance, if you have a file named ‘example.txt‘, you can view the first 10 lines by typing:

head example.txt

This command will output the first 10 lines of ‘example.txt‘ to the console. It’s a quick and easy way to peek into the contents of a file without opening it entirely.

Command Options

The ‘head‘ command offers several options that enhance its functionality. Let’s delve into these options and understand their usage with examples.

The ‘-n’ or ‘–lines’ Option

The ‘-n‘ option allows you to specify the number of lines to display. For example, to view the first 5 lines of ‘example.txt‘, you would type:

head -n 5 example.txt

Alternatively, you can use ‘--lines‘ instead of ‘-n‘, like so:

head --lines=5 example.txt

The ‘-c’ or ‘–bytes’ Option

The ‘-c' option lets you display a specific number of bytes. For instance, to display the first 50 bytes of ‘example.txt‘, you would use:

head -c 50 example.txt

The ‘--bytes‘ option can be used as an alternative to ‘-c‘:

head --bytes=50 example.txt

The ‘-v’ or ‘–verbose’ Option

The ‘-v‘ option is used to display the file name tag. This is particularly useful when dealing with multiple files. For example:

head -v example.txt example2.txt

This command will display the first 10 lines of both ‘example.txt‘ and ‘example2.txt‘, each preceded by its file name.

The ‘-q’ or ‘–quiet’ Option

The ‘-q‘ option suppresses the display of file name tags when multiple files are involved. For instance:

head -q example.txt example2.txt

This command will display the first 10 lines of both files without showing the file names.

Real-World Examples

The ‘head‘ command is not just for viewing text files. It can be used in a variety of real-world scenarios. For instance, it’s often used to display the first few lines of a log file. This can be helpful when you want to quickly check the most recent entries in a log:

head /var/log/syslog

The ‘head‘ command can also be used in conjunction with other commands like ‘ls‘ or ‘tail‘. For example, to display the 10 most recently modified files in a directory, you could use:

ls -lt | head

Best Practices

Understanding the ‘head‘ command’s behavior is crucial when working with large files or logs. It’s important to remember that the ‘head’ command reads from the start of the file, making it efficient for large files. However, if you need to read from the end of a file, the ‘tail‘ command would be more appropriate. When using the ‘head’ command, be mindful of the file size. If you’re dealing with a very large file and you use the ‘-c‘ option to display a large number of bytes, it could potentially consume a lot of memory and slow down your system.

Common ‘head’ Pitfalls and Troubleshooting

While ‘head’ is generally straightforward, there are some common mistakes and pitfalls to be aware of:

  • Not using quotes around file/path names with spaces. This causes errors – always quote paths to be safe.
  • Assuming ‘head’ modifies the original file. It only prints to stdout, so redirects like head file > file can overwrite and erase files accidentally.
  • Forgetting to escape special characters. Use \ before chars like * or # when passing as arguments.
  • Encountering broken pipes from piping ‘head’ into commands like less without options like -S. Add -S to disable chopping long lines.
  • Getting a “file truncated” warning. This happens if the buffer size is too low for large line outputs. Increase with --buffer.
  • Seeing garbled text with -c. This is due to different byte counts across encodings. Stick to -n for lines when possible.
  • Not checking return codes from ‘head’ in scripts, which can hide errors. Always check $? or add || exit after the command to catch issues.
  • Parsing the output from ‘head’ incorrectly in scripts. Remember it returns plain text, not any structured format.


The ‘head‘ command is a versatile tool in the Linux command-line arsenal. Its ability to quickly display the beginning of a file makes it invaluable in many scenarios, from peeking into text files to monitoring logs. By understanding and utilizing the options available with the ‘head‘ command, you can greatly enhance your productivity in the Linux environment. So, don’t wait. Start exploring the ‘head’ command today and discover how it can simplify your tasks.


r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button