Look inside a file

INPUT · Slides

Just the top and the bottom

01 / 08

You do not always want all of it

Last lesson you saw the weak spot: cat puts out everything.

But think about it — when you look at a file, you rarely want to read all of it.

  • Want to know what shape the data is in? The first few lines will do
  • Want to know what just happened? The last few lines will do

head and tail exist for those two.

02 / 08

head — ten lines from the top

head puts out the first 10 lines of a file, and nothing else. The name says it.

However many lines are in there, it stops at 10. So you can point it at any file, however big, without worrying.

Meet a file you do not know? head it first. That is the manners of the terminal.

~ $ head access.log01 one started up02 two read the config(and so on, down to line 10)

03 / 08

tail — ten lines from the bottom

tail does the opposite, putting out the last 10 lines.

This is the one you reach for with a log, because new records get added at the bottom, so what just happened is at the end.

~ $ tail access.log(from line 6 onwards)15 fifteen ended

04 / 08

-n changes how many lines

When 10 is too many or too few, say how many with -n. head -n 3 gives you the first three lines only.

Make it -n 1 and you get a single line. That is the shape people use to see just the heading row of a table.

~ $ head -n 3 access.log01 one started up02 two read the config03 three connected

05 / 08

tail -n +N for "from line N to the end"

The -n on tail has a second way of being written. Put a + in front of the number and it means "from that line to the end".

tail -n +2 means "from line 2 to the end" — in other words, throw away line 1. Handy when a table has a heading row and you only want the body.

-n 2 (the last two lines) and -n +2 (from line 2 to the end) are completely different, so watch for that +.

~ $ tail -n +14 access.log14 fourteen woke up15 fifteen ended

06 / 08

Slicing out the middle

Both head and tail only cut from an end. So what about the middle?

You stack them. Take the first 5 lines with head -n 5, then take the tail -n 2 of that, and you are left with lines 4 and 5.

The symbol that stacks them is | (a pipe). It is another star of the chapter on joining commands, so for now it is enough to know this is possible.

~ $ head -n 5 access.log | tail -n 204 four responded05 five wrote

07 / 08

With two or more files you get headings

Hand head or tail two or more files and it labels them so you can tell which is which.

cat joined them with nothing in between, but here you get a ==> file name <== line slipped in.

That is handy when you are checking with your eyes, but a nuisance when you are handing the result to the next command. When it is in the way, pass one file at a time, or add -q to drop the headings.

~ $ head -n 1 records/short.txt records/long.txt==> records/short.txt <==alpha==> records/long.txt <==line1

08 / 08

Now have a go

access.log is a 15 line file. Every line starts with its number, so you can see with your eyes which part you sliced out.

Change the number after -n and watch how the range moves.