head command in Linux with Examples

Posted in

head command in Linux with Examples
sangeeta.gulia

Sangeeta Gulia
Last updated on December 22, 2024

    In Linux, file filtering is a frequent task in which we filter the data of files and execute an operation on it. Filtering the first few lines of a file is one of the procedures. In Linux, the head command can be used to do this.

    We'll go through the different options for the head command in Linux in this article.

    What is the head command in Linux?

    To standard output, the head command outputs the first lines of one or more files or piped data. It displays the first 10 lines of the selected files by default. Data from each file is preceded by its file name if more than one file name is given. The syntax is:

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

    1. -n n

    Instead of the first 10 lines, the first 'n' lines are printed. If num is not supplied in the command, an error will be shown. The syntax is:

    $ head -n 8 demo.txt

    2. -c size

    Displays the first 'size' bytes of the provided file. Head counts a newline as a single character. Thus, if it prints one, it will be counted as a byte. If the size is not supplied in the command, an error will be shown. The syntax is:

    $ head -c 20 demo.txt

    3. -q

    This option is used when more than one file is specified. Data from each file is not preceded by its file name as a result of this operation. The syntax is:

    $ head -q demo1.txt demo2.txt

    4. -v

    When this option is used, data from the chosen file is always preceded by the name of the file. The syntax is:

    $ head -v demo.txt

    Conclusion

    In this article, we went through various options available with the head command in Linux. The head command is used to filter the first few lines of a file. It provides us with options like getting the first specified bytes of a file, getting the first specific lines of a file, etc.

    People are also reading:

    Leave a Comment on this Post

    0 Comments