head and tail: Flip a coin

I should have mentioned one or both of these by this point in time. I use both head and tail on an almost-daily basis, and leaving those two out until now was an egregious oversight. 😳

Both head and tail are easy enough to figure out: By default, they show the first and last 10 lines of a file, respectively. Nothing fancy there.


There are a few flags that are worth mentioning, and since both head and tail are owned by coreutils, some of those options are shared between the two.

  • -cN: Shows N characters (technically bytes) instead of lines. This can be a godsend if you just want to pluck a certain number of characters out of /dev/urandom.
  • -n: Sets the number of lines to show, so you’re not limited to just 10. You can use bytes here as well, and specify a specific data quantity with conventional suffixes for multipliers. So for example, head --bytes=1b sample.txt would show the first 512 bytes of sample.txt.
  • If you add a minus sign to the --bytes data quantity in head, or a plus sign to the --bytes data quantity in tail, both tools will omit the last or first block of that size. That might be a little confusing; think of head --bytes=-12 sample.txt as “show me all but the last 12 characters of sample.txt” and tail --bytes=+12 sample.txt as “show me all but the first 12 characters of sample.txt.” It might take a little getting used to, but it works, and can be handy.

tail has another flag that deserves attention, mostly because I get the feeling most people don’t know about it. We’ve looked over a lot of log display utilities in the past, and most of them operate by hovering over a log file and echoing it to the screen when it changes. If you pick the right tool, it might also colorize the text. πŸ™‚

Perhaps you knew this and perhaps you didn’t, but tail has a built-in log display mode, as the --follow flag.


As you can see there, with no more than just the -F option, you’ll get a by-the-second update of what appears in a log file, each line added to your display as it is added to the original file. No muss, no fuss.

Like I said, you get both of these tools as part of coreutils, and I have yet to meet a distro that didn’t include that by default. So they’re probably on your machine, and I hope you make good use of them. πŸ˜‰