Linux on call · Chapter 4 of 30

Reading files

By the end of this chapter you can look at the size of a file before you read it, print just its first or last lines, scroll through it safely, and save what you see. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Finding your way
  • 4 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

Ravi, at nine in the morning: "A customer says their order from last night never came. What was the last order we took yesterday?" Every order of the day is in logs/orders.log, one per line, oldest first. Ravi also wants the day's first line and the last twenty for the complaint, and Kabir wants to know how many orders there were.

You already know cat. Bunty tried it on the log, and the screen scrolled for a long, long time. The last order was on the screen for a moment, and everything above it flew past. On a server, logs grow to thousands or millions of lines: printing the whole file is slow, and you cannot read it anyway.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Save the day's first line in first.txt

    Save the first order of the day in a file.

    1. Use head to show the first line of logs/orders.log. head can show only as many lines as you ask for.

    2. Check the line on the screen.

    3. Save the same line in first.txt, in the chapter folder. > sends the output into a file.

    To pass: first.txt has the first line of logs/orders.log, and nothing else. You ran head in the chapter folder. A command with a typing mistake does not count, so type it again.

  2. Task 2

    Save the last order's number in last-order.txt

    Find the number of the last order of the day.

    1. Use tail to show the last line of logs/orders.log. (less works too.)

    2. On that line, find the number right after the word order.

    3. Save only that number in last-order.txt, in the chapter folder. echo with > writes text into a file.

    To pass: last-order.txt has the number of the last order, and no other order number. You ran tail (or less) in the chapter folder. A command with a typing mistake does not count, so type it again.

  3. Task 3

    Save the last 20 lines in recent.txt

    Save the last 20 orders of the day in a file.

    1. Use tail to show the last 20 lines of logs/orders.log.

    2. Check that the last line on the screen is the last order.

    3. Save that same output in recent.txt, in the chapter folder.

    To pass: recent.txt has exactly the last 20 lines of logs/orders.log, in the same order as in the log, and nothing else.

  4. Task 4

    Save the number of lines in count.txt

    Count the lines in the log.

    1. Use wc with the option for lines on logs/orders.log. It prints the count and the file name.

    2. Check the number on the screen.

    3. Save that same output in count.txt, in the chapter folder.

    To pass: count.txt starts with the number of lines in logs/orders.log. The file name after it is fine. You ran wc in the chapter folder. A command with a typing mistake does not count, so type it again.

What you will learn

  • wc: count the lines, words and bytes of a file.
  • head and tail: print the first or the last lines.
  • less: scroll through a file of any size.
  • > again: save exactly what a command prints.

More in Finding your way

Build "Reading files" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.