← Linux and the Command Line Lesson 14 of 51 · 10 XP

head and tail — the start and the end

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

head file.txt          # the first 10 lines
head -n 3 file.txt     # the first 3
tail file.txt          # the last 10
tail -n 5 file.txt     # the last 5
tail -n +2 file.txt    # everything STARTING from line 2 (handy for skipping a CSV header)
tail -f app.log        # "follow": show new lines as they appear (Ctrl+C to exit)

tail -f is an administrator's main tool: start it in one window and you see what the server is writing right now.

To cut out the middle — a pipe of two commands:

head -n 20 file.txt | tail -n 5      # lines 16 to 20

Task. The input is a CSV table: the first line is the header, the rest is data. Print the first two lines of data, without the header.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.