Linux · Head, Tail, and Sed Commands

Linux · Lesson

Head, Tail, and Sed Commands

Master the head, tail, and sed commands for text manipulation in Linux. Practical examples for log analysis and file processing.

Understanding head, tail and sed commands

Linux provides powerful text-processing utilities for reading, filtering and modifying file contents directly from the terminal.

The head command displays lines from the beginning of files, while the tail command displays lines from the end.

The sed command is a stream editor used for advanced text manipulation operations such as printing, replacing, deleting, inserting and appending text.

Core command concepts

head command

Display lines from the top of files or command outputs.

tail command

Display or monitor lines from the bottom of files.

sed command

Perform advanced stream editing and text-processing operations.

Head and tail command usage

head command

  • Displays the first 10 lines by default.
  • Use -n to specify the number of lines.
  • Useful for previewing files quickly.

tail command

  • Displays the last 10 lines by default.
  • Use tail -f for real-time monitoring.
  • Commonly used for log file analysis.

Important sed operations

Text processing

  • p → print lines
  • s → substitute or replace text
  • d → delete lines
  • Supports line numbers and ranges

File modification

  • i → insert text before a line
  • a → append text after a line
  • Use -i to modify original files
  • Supports global and case-insensitive replacement

Practical Linux examples

The following examples demonstrate how to use head, tail and sed commands for text processing and file management in Linux.

1. Head command

Display lines from the top of a file. By default, head shows the first 10 lines.

terminal — bash
linux-text-processing
[root@localhost ~]# head -n 5 /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
[root@localhost ~]#
 
[root@localhost ~]# cat /etc/passwd | head -6
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
[root@localhost ~]#

2. Tail command

Display lines from the bottom of a file or command output.

terminal — bash
linux-text-processing
[root@localhost ~]# tail /etc/passwd
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
dom:x:1002:1002::/home/dom:/bin/bash
john:x:1003:1004::/home/john:/bin/bash
[root@localhost ~]#
 
[root@localhost ~]# cat /etc/passwd | tail -n 6
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
dom:x:1002:1002::/home/dom:/bin/bash
john:x:1003:1004::/home/john:/bin/bash
[root@localhost ~]#

3. Tail -f command

Monitor files in real-time and display updates continuously.

terminal — bash
linux-text-processing
[root@localhost ~]# tail -f /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin
chrony:x:991:987::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
systemd-oom:x:985:985:systemd Userspace OOM Killer:/:/usr/sbin/nologin
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
dom:x:1002:1002::/home/dom:/bin/bash
john:x:1003:1004::/home/john:/bin/bash

4. sed print operation

Use sed to print specific lines, ranges or multiple line numbers.

terminal — bash
linux-text-processing
[root@localhost ~]# sed -n '5p' /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]#
 
[root@localhost ~]# sed -n '1,4p' /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
[root@localhost ~]#
 
[root@localhost ~]# sed -n '1p;5p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]#

5. sed find and replace

Replace text patterns using sed substitution operations.

terminal — bash
linux-text-processing
[root@localhost ~]# sed 's/root/super/' /etc/passwd
super:x:0:0:root:/root:/bin/bash
 
[root@localhost ~]# sed 's/root/super/g' /etc/passwd
super:x:0:0:super:/super:/bin/bash
 
[root@localhost ~]# sed 's/RooT/super/gi' /etc/passwd
super:x:0:0:super:/super:/bin/bash

6. sed delete operation

Delete specific lines or line ranges using sed.

terminal — bash
linux-text-processing
[root@localhost ~]# sed '2d' /mnt/passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
 
[root@localhost ~]# sed '2,6d' /mnt/passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost ~]#

7. sed insert operation

Insert text before a specific line using sed.

terminal — bash
linux-text-processing
[root@localhost ~]# sed '3iHello' /mnt/passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
Hello
daemon:x:2:2:daemon:/sbin:/sbin/nologin
 
[root@localhost ~]# sed -i '3iHello' /mnt/passwd
[root@localhost ~]#

8. sed append operation

Append text after a specific line using sed.

terminal — bash
linux-text-processing
[root@localhost ~]# sed '1aSampleText' /mnt/passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
SampleText
 
[root@localhost ~]# sed -i '1aSampleText' /mnt/passwd
[root@localhost ~]# cat /mnt/passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
SampleText

Best practices while using these commands

Safe file handling

  • Use sed without -i first to preview changes.
  • Keep backups before modifying important files.
  • Verify line ranges carefully before deletion.

Efficient text processing

  • Use head and tail for quick file previews.
  • Use tail -f for real-time log monitoring.
  • Combine sed with pipes for advanced automation.

Practice tasks for your Linux lab

  • Display the first 5 lines of /etc/passwd using head.
  • Display the last 6 lines using tail -n 6.
  • Practice real-time monitoring using tail -f.
  • Print specific lines using sed -n.
  • Replace text patterns using sed 's/old/new/'.
  • Practice delete, insert and append operations using sed.

In upcoming lessons, you will continue learning advanced Linux text processing, regular expressions and shell scripting concepts.