• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

Head and tail Command in Linux with Examples

{{postValue.id}}

Head and Tail Command

The head command is used to print the first N lines from the file on the terminal.
It displays first 10 lines by default.
The tail command is used to print the last N lines from the file on the terminal. It displays last 10 lines by default.
The head and tail command is especially used with log files to read the first and last few lines to know about the error messages.

Syntax for head command

head < option> < File>

The head command options are
-c: It prints the first N bytes of file with the leading -, prints the characters from the N byte in the file

-n: It Print the first N lines with the leading -, print all but the last N lines of each line.

-q: Never print the headers giving file names.

-v: always print headers giving file name.

Syntax for tail command

tail < option> < File>

The tail command options are:

-c: It prints the last N bytes of file with leading +, prints the characters from the N byte in the file.
-n: It prints last N lines with leading + prints lines from the Nth line in the file.
-f: It prints the appended lines on the terminal as the file grows.
-b: To specify the units of blocks.
-l: To specify the units of lines.

Example

[root@linuxhelp ~]# cat /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
&hellip ..................................................  &hellip .... &hellip ...
&hellip ..................................................  &hellip .... &hellip ...
&hellip ..................................................  &hellip .... &hellip ...
squid:x:23:23::/var/spool/squid:/sbin/nologin
c:x:505:505::/home/c:/sbin/nologin
d:x:506:508::/home/d:/sbin/nologin
a2:x:512:516::/home/a2:/bin/bash
a3:x:513:517::/home/a3:/bin/bash
a1:x:514:518::/home/a1:/bin/bash
user2:x:515:525::/home/user2:/bin/bash
user3:x:516:520::/home/user3/Desktop:/bin/bash
user5:x:517:523::/home/user5:/bin/bash
user1:x:518:519::/home/user1:/bin/bash

Display the first 10 lines using head

This head command is used to view the first 10 lines from the file.

Example

[root@linuxhelp ~]# head /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

By, default it prints 10 lines in the above example output.

Display the first seven lines using head

This head command is used to view the first 7 lines from the file.

Example

[root@linuxhelp ~]# head -7 /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

To specify the number of line to print

This head command is used to specify the first N number of line to print by using -n option. The below example will prints the first 4 lines from the file.

Example

[root@linuxhelp ~]# head -n4 /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

To print all, except the last specified line

This head command is used to print all lines from starting of the file, except the last specified lines.

Example

[root@linuxhelp Desktop]# head -n+12 /etc/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

Print the first n bytes using head command

The command is used to print the first N bytes from the file by using -c option.

Example

[root@linuxhelp Desktop]# head -c 20 /etc/passwd 
root:x:0:0:root:/root

Another Example:

[root@linuxhelp Desktop]# head -c 35 /etc/passwd
root:x:0:0:root:/root:/bin/bash

Display last 10 lines using tail

This tail command is used to view the last 10 lines of the passwd file.

Example:

[root@linuxhelp ~]# tail /etc/passwd 
squid:x:23:23::/var/spool/squid:/sbin/nologin
c:x:505:505::/home/c:/sbin/nologin
d:x:506:508::/home/d:/sbin/nologin
a2:x:512:516::/home/a2:/bin/bash
a3:x:513:517::/home/a3:/bin/bash
a1:x:514:518::/home/a1:/bin/bash
user2:x:515:525::/home/user2:/bin/bash
user3:x:516:520::/home/user3/Desktop:/bin/bash
user5:x:517:523::/home/user5:/bin/bash
user1:x:518:519::/home/user1:/bin/bash

By default it display last 10 ten lines of the file

Display last N lines using tail

-n option is used to print the last n lines from the file. The below example will prints the last 3 lines from the file.

Example

[root@linuxhelp ~]# tail -n3 /etc/passwd 
user3:x:516:520::/home/user3/Desktop:/bin/bash
user5:x:517:523::/home/user5:/bin/bash
user1:x:518:519::/home/user1:/bin/bash

Print lines from the Nth line using tail command

This command is used to print the starting line from 40th line to end of the file by using -n option.

Example

[root@linuxhelp ~]# tail -n+40 /etc/passwd 
a3:x:513:517::/home/a3:/bin/bash
a1:x:514:518::/home/a1:/bin/bash
user2:x:515:525::/home/user2:/bin/bash
user3:x:516:520::/home/user3/Desktop:/bin/bash
user5:x:517:523::/home/user5:/bin/bash
user1:x:518:519::/home/user1:/bin/bash

The above example is to print the starting line from 40th to end of the file.

Print the last n bytes using tail

The command is used to print the last N bytes from the file by using -c option.

Example

[root@linuxhelp ~]# tail -c12 /etc/passwd 
1:/bin/bash

Another example

[root@linuxhelp ~]# tail -c 39  /etc/passwd 
user1:x:518:519::/home/user1:/bin/bash

The above examples show the difference of two outputs, it prints the last 12 bytes in first example and another example shows last 39 bytes from the file.

Print characters from the nth byte using tail

This command is used to print the characters from the starting of nth byte by using the “ +” with -c option.

Example

[user1@linuxhelp Desktop]$ cat file1.txt 
test1
test2
test3
linux server
commands
ls mkdir
[user1@linuxhelp Desktop]$ tail -c+9 file1.txt 
st2
test3
linux server
commands
ls mkdir

The above example prints the characters start from 9th byte of the file.

To view the specific ip logs using grep command in tail commands

This grep command is used view the specific IP log with the -f option in tail command.

Example

[root@linuxhelp ~]# tailf /var/log/squid/access.log | grep “ 192.168.7.13”  

1454971376.033      0 192.168.5.32 TCP_DENIED/403 3659 CONNECT       
s3.amazonaws.com:443 - NONE/- text/html

1454971376.033      0 192.168.5.32 TCP_DENIED/403 3659 CONNECT       
s3.amazonaws.com:443 - NONE/- text/html

1454971376.033      0 192.168.5.32 TCP_DENIED/403 3659 CONNECT       
s3.amazonaws.com:443 - NONE/- text/html

1454971376.033      0 192.168.5.32 TCP_DENIED/403 3659 CONNECT       
 s3.amazonaws.com:443 - NONE/- text/html

Tags:
jackson
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How to show the output of certain files which contains the desired number of bytes using the head command in Linux?

A

You can use the option of "-c" with "head" command to show the output of certain files which contains the desired number of bytes using the head command in Linux. For ex: "head -c45 /file/pat

Q

How to get chosen number of lines instead of default 10 while showing the output of the "tail" command?

A

You can use the option of "-n" with "tail" command to get chosen number of lines instead of default 10 while showing the output of the "tail" command. For Ex: "tail -n /path

Q

How to Display Specific line number using "tail" command?

A

Using the following command to Display Specific line number using tail command, syntax: "tail +10 your_file.txt | head -5"

Q

How to get single output for multiple files using the "tail" command?

A

You can use the option of "-q" with "tail" command to get single output for multiple files using the "tail" command. For Ex: " tail -q file1.txt file2.txt"

Q

How to check the version fo head command in Linux?

A

You can use the option of "--version" with "head" (or) "tail" command to check the version of that command in Linux. Syntax: "head --version" (or) "tail --version"

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luk Van De Looverbosch ?
How to create a root ?

Hello,
How to create root@linuxhelp in Linux Mint 20.1 64-bit ?
Thanks in advance for your reply.
Best regards.

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.