Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»head Command in Linux with Examples

    head Command in Linux with Examples

    By RahulFebruary 4, 20235 Mins Read

    The head command is a command-line utility in most Linux distributions that is used to print data from the beginning of one or more files. It is also used to output piped data to CLI. We can use different options to print any number of lines or bytes of data to the standard output but by default the head command prints the first 10 lines of the file.

    Advertisement

    In this comprehensive guide, we will learn to use the head command in Linux. Moreover, we will also get familiar with the options for the head command that is available on Linux. The head command is available in all major Linux distributions; but for demonstration purposes, in this guide, we will only use Ubuntu 20.04 LTS.

    Let’s begin with understanding the syntax of the head command first:

    Syntax:

    The syntax of the head command is like any other command that is used to work with files. It takes two different parameters; OPTION and FILE_NAME.

    head [OPTION] [FILE_NAME] 
    

    The options are used to manipulate the output of the head command. They can be used to specify the amount of data that needs to be printed to the standard output or to print data with or without headers. Here’s a list of options available for the head command in Linux:

    OptionMeaning
    -c, --byteIt is mandatory to follow this option by a number which specifies the bytes that are to be printed to the standard output.
    -n, --linesIt is mandatory to follow this option by a number as well. The -n option specifies the amount of lines that are to be printed to the standard output.
    :-q, --quiet, --silentIf this option is used then the head command will not print headers specifying the file names.
    -v, --verboseThis option will always output the header/filename.
    -z, --zero-terminatedChanges the line delimiter from newline to NULL.
    --help To output the help menu.
    --versionTo output the version information about the head command.

    In this how-to guide, we will discuss the head command’s options in great detail; but first, we will learn to use the head command without any options.

    How to show first 10 lines from a file

    If we execute the head command without providing any option, it will show the first ten lines only of a text file. Let’s say we have a text file named cars.txt which contains the names of some cars and we need to print the names of the first ten cars which are written on the first ten lines. Then we will use the head command in the following way:

    head cars.txt 
    

    How to print a specific number of bytes from the beginning of a file

    The options -c or --byte can be used to specify the number of bytes that are to be printed to the standard output. Now we will use the cars.txt file again and print the first fifteen bytes of the file:

    head -c 15 cars.txt 
    

    Similarly:

    head --bytes 15 cars.txt 
    

    How to print a specific number of lines from the beginning of a file

    The -n, --lines options can be used to print a specific number of lines from the beginning of a file:

    head -n 5 cars.txt 
    

    Similarly:

    head --lines 5 cars.txt 
    

    How to display the header/file name

    The -v or --verbose option can be used to print the name of the file to the standard output:

    head -v cars.txt 
    

    How to display data from multiple files

    The head command can take multiple file names as parameters. It will print out the first ten lines of both files (proceeded by the file name) by default:

    head cars.txt names.txt 
    

    We can also use options along with multiple file names:

    head -n 2 cars.txt names.txt 
    

    How to display data from multiple files without headers

    The -q, --quiet and the --silent option can be used to display data from multiple files without headers:

    head -n 2 -q cars.txt names.txt 
    

    How to combine head command with other commands

    The head command can be combined with other commands to filter the data that is being printed to the standard output:

    ls | head -n 5 
    

    Using [-]K with -c and -n options

    If the -c and -n options are followed by [-]K, then the head command prints all the bytes/lines of the file except the last K bytes/lines:

    head -n -15 names.txt 
    

    The names.txt file contains twenty-six names in total. If we use the below-given command then the head command will print the first fifteen names (lines).

    head -n 15 names.txt command  
    

    But if we add a - before the number then the head command will print all the lines except the last fifteen:

    head -n -15 names.txt 
    

    Conclusion

    The head is a command present in all major Linux distributions which are used to print out data from the start of a file. It is the opposite of the tail command which is used to output data from the end of a file.

    The head command can be used with different options to specify the number of lines or bytes that should be printed to the standard output. Options are also available to allow or restrict the head command to not show the header or name of the file.

    In this how-to guide, we learned to use the header command along with its options.

    command head
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Create a Directory If It Does Not Exist in Linux

    How to Save Command Output to a File in Linux

    Mastering the Strace Command in Linux: A Complete Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Configure Postfix to Use Gmail SMTP on Ubuntu & Debian
    • PHP Arrays: A Beginner’s Guide
    • Deploying Flask Application on Ubuntu (Apache+WSGI)
    • OpenSSL: Working with SSL Certificates, Private Keys and CSRs
    • How to Create and Read List in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.