• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Linux Head Command

Written by Admin, Updated On December 10, 2022
head, terminal
Linux Head Command

The head command is used to print the few first lines (by default 10 lines) of file or standard output. This tutorial shows you how to use the Linux head command with practical examples.

Head Command Syntax#

Below is the basic syntax for the head command:

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

Here,

  • OPTION – You can pass the head options for different actions.
  • FILE – Pass here file name. If not passed anything head will read the standard input.

Use of Head Command#

When you used head command without any option it will show the first 10 lines by default like below:

head filename

Display a Specific Number of Lines#

To display the specific the number of lines, use the -n (--lines) option followed by the integer number.

head -n  filename

For example, to show the first 25 lines from a file readme.md, you would run:

head -n 25 readme.md

Another way is, you can remove the n and just use the hyphen (-) and the number without space between them.

Below command will give the same output as the previous example:

head -25 readme.md

Show Specific Number of Bytes#

Use the -c (--bytes) option to display a specific number of bytes:

head -c  filename

For instance, to print the first 150 bytes of data of a file readme.md, run:

head -c 150 readme.md

You also can use the multiplier to specify the number of bytes. b multiplies it by 512, kB multiplies it by 1000, K multiplies it by 1024, MB multiplies it by 1000000, M multiplies it by 1048576, and so on.

In following example, it will show the first 3 kilobytes of the file readme.md:

head -c 5k readme.md

Display Multiple Files#

When the multiple files are given with the head command, it will display the first 10 lines of each files.

head filename1 filename2

You can use the same options as a single file. For example to show the first 15 lines of the files readme.md and license.txt:

head -n 15 readme.md license.txt

If multiple files given then each file name is showing as header in output.

Use Head with Other Commands#

The head command can be piped with other commands by redirecting the standard output from other commands.

In the following example, the output of the ls command is piped to head to show only the three most recently modified files or folders.

ls -t | head -n 3
about.php
contact.php
index.php

Conclusion#

This guide shown you how to use the Head command in Linux with different options.

If you have any question or feedback, feel free to leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Rename Files and Directories in Linux
Next Article   How to Use sed to Find and Replace String in Files

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook