CHAPTER 2 · 5 lessons · 65 exercises

Look inside a file

Check what is in a file without opening it. See just the top and the bottom, count the lines, work out what it really is.

This is the chapter where you find out what is in the files you made, without opening an editor.

You can use ls now, from the last chapter. But all ls tells you is the name. It will not tell you what is written in memo.txt.

There are five tools here.

  • cat — put the whole thing on the screen
  • head and tail — just the top, just the bottom
  • less — read it a page at a time
  • wc — count the lines and the characters
  • stat and od — not the name but what it really is, and every single byte inside it

The thing to take away most of all is do not use cat on a big file. cat a log with tens of thousands of lines in it and the screen pours past like a waterfall, leaving you having read none of it. head and less exist for exactly that.

By the end of this chapter, "look inside a file" stops being three moves (open it, read it, close it) and becomes one line.

Lessons in this chapter

Read without opening

  1. 03Put what is in a file on the screenUse cat to put a file on the screen. Join files, number the lines, make new ones.Go to the exercises
  2. 04Just the top and the bottomUse head and tail to slice the start and the end out of a big file.Go to the exercises
  3. 05Read it a page at a timeUse less to move up and down a long file. Open it, search it, get out of it.Go to the exercises

Count it, and identify it

  1. 06Counting thingsUse wc to count lines, words and bytes. And see how a byte differs from a character.Go to the exercises
  2. 07What a file is made ofUse stat to see everything except the contents. Use od to see the bytes themselves.Go to the exercises

Next up is “Search”. Search by name, and search by the text inside. The tools that save you opening things one by one.