Apply head Command in Real-World Scenarios
In this step, you will learn how to apply the head
command in real-world scenarios to solve common text processing and editing tasks.
One common use case for the head
command is to quickly view the top of log files. Let's try this with the system log file:
sudo head /var/log/syslog
Example output:
Feb 24 12:34:56 myhost systemd[1]: Starting System Logging Service...
Feb 24 12:34:56 myhost systemd[1]: Started System Logging Service.
Feb 24 12:34:56 myhost rsyslogd[123]: [origin software="rsyslogd" swVersion="8.2001.0" x-pid="123" x-info="https://www.rsyslog.com"] start
Feb 24 12:34:56 myhost rsyslogd[123]: rsyslogd's groupid changed to 108
Feb 24 12:34:56 myhost rsyslogd[123]: rsyslogd's userid changed to 104
Another common use case is to extract the top lines from the output of a command. For example, let's say you want to see the top 3 processes by CPU usage:
top -bn1 | head -n 5
Example output:
top - 12:34:56 up 1 day, 12:34, 0 users, load average: 0.15, 0.05, 0.01
Tasks: 85 total, 1 running, 84 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1969.3 total, 287.1 free, 654.9 used, 1027.3 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1019.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 8572 5748 3900 S 0.0 0.3 0:01.22 systemd
By using the head
command, you can quickly extract the top 5 lines of the top
command output, which includes the system summary and the top processes.
These are just a few examples of how you can apply the head
command in real-world scenarios. As you continue to work with text processing and editing tasks, you'll find many more opportunities to use this powerful command.