Command line tip - peek into the start and end of files

  • July 2, 2007
  • Avatar for peter
    Peter
    Upfold

Sometimes, especially when dealing with log files, you might want to peek at the start and/or end of a file to see just that bit of the file and not the whole thing. Of course, there are a couple of easy commands that allow you to do this.

The commands are head and tail and work with almost any Unix-like operating system.

As the name suggests, head is for peaking at the top of a file. By default, it will show you the first 8 lines of the file:

$ head /path/to/file

You can override the number of lines displayed with the -n switch:

$ head -n 10 /path/to/file

In this example, we'll return the first 10 lines of the file.

Funnily enough, tail is exactly the same, but works for the end of the file. Our two examples again:

$ tail /path/to/file

For a set number of lines (counting back from the last line), use -n again:

$ tail -n 10 /path/to/file

A very simple, but effective and useful command line tip!

Avatar for peter Peter Upfold

Home » Articles »