mooc-course.com is learner-supported. When you buy through links on our site, we may earn an affiliate commission.

Display the First Few Lines of a File in Linux | Linux head Command with Examples

The Linux head command is a powerful and versatile utility that allows users to view the beginning of text files or piped data. Whether you’re a beginner just starting with Linux or an intermediate user looking to enhance your command-line skills, this comprehensive guide will help you master the head command and its various applications.

Understanding the Linux Head Command

The head command in Linux is used to display the first part of files or piped data. By default, it prints the first 10 lines of each specified file to the standard output. However, it’s highly customizable, allowing you to specify the number of lines or bytes you want to view.

Basic Syntax of the Head Command

The basic syntax of the head command is as follows:

head [OPTION]... [FILE]...

If no FILE is specified, or when FILE is -, the command reads from the standard input.

Key Options of the Head Command

Let’s explore some of the most commonly used options with the head command:

  1. Specifying the Number of Lines (-n)

    The -n option allows you to specify the number of lines you want to display:

    head -n 5 filename.txt

    This command will display the first 5 lines of the file.

  2. Specifying the Number of Bytes (-c)

    The -c option lets you specify the number of bytes to display:

    head -c 20 filename.txt

    This command will display the first 20 bytes of the file.

  3. Displaying Multiple Files

    You can use the head command with multiple files:

    head file1.txt file2.txt

    This will display the first 10 lines of each file, with a header indicating the file name.

  4. Quiet Mode (-q)

    The -q option suppresses the headers when displaying multiple files:

    head -q file1.txt file2.txt
  5. Verbose Mode (-v)

    The -v option always prints the file headers, even when processing a single file:

    head -v file1.txt

Examples of Using the Head Command in Linux

Let’s look at some practical examples to better understand how to use the Linux head command:

See also  25 Basic Linux Commands For Beginners

Example 1: Viewing the Beginning of a Log File

head /var/log/syslog

This command displays the first 10 lines of the system log file, which can be useful for quick troubleshooting.

Example 2: Combining Head with Other Commands

You can use the head command in combination with other Linux commands using pipes:

ls -l | head -n 5

This command lists the contents of the current directory and displays only the first 5 entries.

Example 3: Displaying a Specific Number of Bytes

head -c 100 large_file.txt

This command displays the first 100 bytes of the file, which can be particularly useful when dealing with binary files or when you need to see the file’s header.

Example 4: Using Head with Tail

You can combine the head and tail commands to display a specific range of lines:

head -n 20 file.txt | tail -n 10

This command displays lines 11-20 of the file.

Example 5: Using Multiplier Suffixes

The head command supports multiplier suffixes:

head -c 1K file.txt   # Displays the first 1 kilobyte
head -c 1M file.txt   # Displays the first 1 megabyte

Advanced Tips and Tricks

  1. Skipping the Last N Lines: Use a negative number with -n to print all but the last N lines:
    head -n -5 file.txt

    This displays all lines except the last 5.

  2. Using Head in Scripts: The head command is particularly useful in shell scripts for processing the beginning of files or output from other commands.
  3. Handling Large Files: When dealing with very large files, using head can save you time and system resources by allowing you to examine just the beginning of the file.
  4. Data Processing: In data processing pipelines, the head can be used to preview or sample data before further processing.

Conclusion

The Linux head command is a handy utility that allows you to quickly view the beginning of files or command output. By mastering its various options and use cases, you can significantly improve your efficiency when working with text files and data streams in Linux.

Remember, practice makes perfect. Try out these commands and experiment with different options to become proficient in using the head command. Happy coding!

See also  Display the User Manual of Any Command  in Linux | Linux man Command With Examples
Leave a Reply

Your email address will not be published. Required fields are marked *

Free Worldwide Courses

Learn online for free

Enroll in Multiple Courses

Learn whatever your want from anywhere, anytime

International Language

Courses offered in multiple languages & Subtitles

Verified Certificate

Claim your verified certificate