Large Files and Live Files

✦ Getting out of less

This is the single most common way people get stuck in a terminal. less takes over the screen, and the usual escapes do nothing. Press q. Also useful inside it: Space scrolls down, b scrolls back, and typing /word searches forward. This sandbox treats less like cat, because there is nothing to scroll, but on your own machine q is the exit.

Following a file as it changes. tail -f file prints the last lines and then keeps watching, printing new lines as they are written. It is how you watch a server log while you use the application, and it is one of the most useful commands in real development.

You leave it with Control and C, which is the general way to stop a running command.

Reading a specific slice. Combining head and tail gets you the middle of a file. head -n 20 file | tail -n 5 gives you lines 16 through 20, because you take the first twenty and then the last five of those. That pattern of narrowing twice is worth remembering.

SANKOFA SANDBOX
learner:~$

tail -f cannot be demonstrated here, because nothing is writing to these files. On your machine it is how you watch a live log.

Try it → Get lines 3 to 5 of logs/app.log by combining head and tail with a pipe. Check your answer against `cat -n` style counting by running `wc -l` first.

◈ Graded Task

Inspect the log files without opening an editor. Write the number of lines in logs/app.log into a file called count.txt in your home directory, and write the last two lines of logs/access.log into a file called recent.txt.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 4 visible and 3 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back