head and tail
The head and tail commands are essential Unix utilities for viewing parts of files. They allow you to quickly preview large files by displaying only the beginning or the end, respectively. Whether you need to inspect log files, sample data sets, or monitor real-time output, these commands are both simple and powerful. This comprehensive guide will walk you through the syntax, options, practical examples, and advanced techniques for using both head and tail.
Table of Contents
Introduction
When working with files that contain many lines, it is often impractical to load the entire file into a terminal or editor. The head and tail commands help by displaying only a portion of the file:
head: Shows the first part of a file.tail: Shows the last part of a file.
Both tools are frequently used in shell scripting, log file analysis, and data processing tasks.
The head Command
Basic Syntax and How head Works
The basic syntax for head is:
By default, head prints the first 10 lines of each specified file. If no file is provided, it reads from standard input.
Common Options and Parameters for head
-n NUMor--lines=NUM:
Display the first NUM lines.head -n 5 file.txt-c NUMor--bytes=NUM:
Display the first NUM bytes of the file.head -c 100 file.txtMultiple Files:
When given more than one file,headprints a header with the file name before its output.head file1.txt file2.txt
Practical Examples for head
Display the first 10 lines (default):
head logfile.txtDisplay the first 5 lines:
head -n 5 data.csvDisplay the first 100 bytes:
head -c 100 config.iniPreview multiple files:
head -n 3 /etc/passwd /etc/group
Advanced Usage Tips for head
Piping and Redirection:
You can useheadin pipelines to limit output. For example, when combined withgrep:grep "ERROR" server.log | head -n 20This prints only the first 20 matching lines.
Combining with Other Utilities:
When paired with utilities likewc(word count),headcan help summarize file content.head -n 50 data.txt | wc -lThis confirms that there are 50 lines in the preview.
The tail Command
Basic Syntax and How tail Works
The basic syntax for tail is:
By default, tail prints the last 10 lines of each specified file. If no file is provided, it reads from standard input.
Common Options and Parameters for tail
-n NUMor--lines=NUM:
Display the last NUM lines.tail -n 15 logfile.txt-c NUMor--bytes=NUM:
Display the last NUM bytes.tail -c 50 logfile.txt-for--follow:
Continuously monitor the file for new content (commonly used for logs).tail -f server.log-F:
Similar to-fbut follows the file by name, which is useful if the file is rotated.tail -F server.log
Practical Examples for tail
Display the last 10 lines (default):
tail access.logDisplay the last 15 lines:
tail -n 15 error.logDisplay the last 50 bytes:
tail -c 50 report.txtMonitor a log file in real-time:
tail -f /var/log/syslogFollow a file that might be rotated:
tail -F /var/log/application.log
Advanced Usage Tips for tail
Combined with
head:
To extract a specific block of lines from the end of a file, you can combinetailwithhead. For example, to view lines 101–110 from the end:tail -n 110 file.txt | head -n 10Using with Pipes:
Just likehead,tailworks seamlessly in pipelines. For instance, to see the last few lines of a filtered output:grep "WARN" system.log | tail -n 5Tracking File Changes:
Usetail -fin conjunction with other monitoring tools (likemultitailor custom scripts) to keep an eye on log file updates in real time.
Combining head and tail
Sometimes you might want to extract a specific range of lines from a file. By combining head and tail, you can achieve this easily. For example, to display lines 20 through 30 from a file:
This works as follows:
head -n 30 file.txtextracts the first 30 lines.tail -n 11then displays the last 11 lines of that output, which correspond to lines 20–30 of the original file.
Conclusion and Further Reading
The head and tail commands are powerful tools for quickly previewing and monitoring files. Whether you need a snapshot of the beginning of a file or you want to continuously watch the end of a log file, these utilities are indispensable for everyday command-line tasks. Their simplicity, combined with the ability to integrate them into more complex pipelines, makes them key components of any Unix/Linux user’s toolkit.
Further Reading and Resources
Manual Pages:
Access detailed information by typing:man head man tailOnline Documentation:
Tutorials and Examples:
Explore community forums, blogs, and Q&A sites like Stack Overflow for additional examples and creative uses ofheadandtail.
Experiment with these commands on your own data to see how they can help streamline your workflow. Happy previewing!