EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Head Command in Linux

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Head Command in Linux

Head Command in Linux

Introduction to Head Command in Linux

As by name it is clear that what head command does, typically it prints the first 10 lines(default in Unix-Linux) of any files or output of any files in n standard output format. Some times the head command does not provide the file name instead “-” is provided. So, in this case, it read the standard input of the user.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

HEAD [options] [file]

Options available for Head Command in Linux

1. -n, –lines=[-]num: Displays the first num lines instead of the first 10; with the leading ‘-‘, displays all but the last num lines of each file.

2. -v, –verbose: Always display the header name when file is identified.

3. –version: For version.

4. -c, –bytes=[-]num: Displays the first num bytes of each file; with a leading ‘-‘, displays all but the last num bytes of each file.

5. -q, –quiet, –silent: It restricts the printing of header name when file identified.

6. –help: For help.

Note: In the above “num” have suffix as below multipliers:
  • b      512
  • M    1024*1024
  • K     1024
  • GB  1000*1000*1000
  • MB  1000*1000
  • kB   1000
  • G   1024*1024*1024

Examples of Head Command in Linux

Given below are the examples of Head Command in Linux:

Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,494 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Example #1

Command:

head example.txt

Output:

head command in linux 1

Explanation:

  • Let’s just have a look at what’s in the example.txt file using cat you can see that it’s the numbers 1 to 20 all on separate lines so if I use the head command instead of cats and then type in the name of the file and hit enter you can see that it prints out the first 10 lines of the file which is 1 to 10 now.

Example #2

Command:

head -n 3 example.txt

Output:

head command in linux 2

Explanation:

  • Head can actually do a little bit more than that with some options so if we type in heads and then dash n which stands for the number of lines we can actually tell it how many lines we want to print out so let’s just say 3 and we have to give it a file to actually do this operation on so same file again example.txt and hit enter and you can see that it’s printed out the first 3 lines now we can basically print out as many or as few lines as we want so.

Example #3

Command:

head -n -15 example.txt 

Output:

head command in linux 3

Explanation:

  • Let’s just go for 15 there we go 1 to 15 now there is another thing that we can do with the same command just by editing it slightly and that is to put a minus sign in front of the number now what this does is it tells head that we actually want to print everything up to the last 15 lines.
  • So, in this case, it prints how lines one to five and it won’t bother printing out the rest of the Alliance so if we hit enter you can see it’s printed outlines one to five now.

Example #4

Command:

head -n -2 example.txt

Output:

head command in linux 4

Explanation:

  • If I do that again but I just tell it to not print out the last two lines we should get an output that is 1 to 18 so if I hit enter now you can see that that’s what we’ve got so by putting a minus sign in front of the number for this number option here we’re actually telling heads to print out everything apart from the last two lines or whatever number you have here.
  • We can use for head which is a byte option which is -C and it works in exactly the same way as the -n option but rather than it printing outlines it prints out bytes now if I just cat out example.txt again

Example #5

Command:

head -c 2 example.txt

Output:

head command in linux 5

Explanation:

  • So a byte is eight bits and the newline character which is actually at the end here which is hidden from our view but tells the computer to basically go to a new line and then print the next character is a byte as well so if I use the head command with the -C bytes option and then I tell it to print out the first two bytes of this file and the file name is example.txt again it’s actually just going to print out this one
  • Here so if I hit enter you can see that’s what it’s done and it’s put the command prompt on to a new line now if I tell it to print out three you’ll see this printed out the numbers one the numbers two but the command prompt is on the same line as the two that’s because it hasn’t actually printed or recognized this newline character which is actually after the 2

Example #6

Command:

head -c 4 example.txt

Output:

example.txt

Explanation:

  • So now if I change this to four and hit enter you can see that it’s printed out one and two and the command prompt on a new line so printing out bites is different from printing up lines basically.

Example #7

Command:

head -c -4 example.txt

Output:

-4 example.txt

Explanation:

  • The minus sign with the white option so if I type in head -C which is the bite option and then minus four it’s gonna print out everything apart from the last four bytes of the file so example.txt and hit enter and there we go you can see that’s actually printed out everything up to the last four bytes of the file now.

Example #8

Command:

head -v example.txt

Output:

-v example.txt

Explanation:

  • – V for verbose and then the name of the file so example dot text so you can see it’s print out the first 10 lines and the name of the document.

Recommended Articles

This is a guide to Head Command in Linux. Here we discuss the introduction, options available for Head Command in Linux and respective examples. You may also have a look at the following articles to learn more –

  1. du Command in Linux
  2. Bash Shell in Linux
  3. mv command in Linux
  4. Linux Route Command

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Kali Linux Training Learn More