Using Just the Beginning or End of a File
Problem
You need to display or use just the beginning or end of a file.
Solution
Use the head or tail
commands. By default, head will output the
first 10 lines and tail will output the last 10
lines of the given file. If more than one file is given, the appropriate
lines from each of them are output. Use the -number switch (e.g., -5) to change the number of lines.
tail also has the -f and -F switches, which follow the end of the file
as it is written to. And it has an interesting + switch that we cover in
Skipping a Header in a File.
Discussion
head and tail, along with cat, grep, sort, cut, and uniq, are some of the most commonly used Unix text processing tools out there. If you aren’t already familiar with them, you’ll soon wonder how you ever got along without them.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access