How to Combine Linux head and tail Commands to View the Middle Lines of a File

When working with large text files, logs, or CSV data in a Linux terminal, it is often necessary to inspect a specific chunk of data located somewhere in the middle of the file. The head command allows you to view the very beginning of a file, while the tail command allows you to view the very end. However, if you need to read lines 500 through 520 of a massive 10,000-line server log, neither command can accomplish this on its own. By combining the two commands using a Linux pipe (|), you can easily extract and view the exact middle section of any text file.

Understanding head and tail

Before combining them, you must understand how to control the output of each command individually using the -n (number of lines) flag.

  • head: By default, head prints the first 10 lines of a file. If you run head -n 50 /var/log/syslog, it prints the first 50 lines.
  • tail: By default, tail prints the last 10 lines of a file. If you run tail -n 20 /var/log/syslog, it prints the final 20 lines.

The tail command also has a special, lesser-known feature. If you use a plus sign (+) before the number, it changes the command’s behaviour entirely. Instead of printing the last X lines, tail -n +500 tells Linux to start at line 500 and print everything from there to the very end of the file.

Method 1: Piping head into tail

The most intuitive way to extract a middle section is to use head to grab everything from the top of the file down to your desired end point, and then pipe that output into tail to chop off the top portion you don’t want.

Suppose you have a file named data.csv and you want to view exactly lines 40 through 50 (an 11-line chunk).

  1. First, use head to grab the first 50 lines: head -n 50 data.csv
  2. Now, pipe that 50-line chunk into tail, asking it to display only the final 11 lines of that chunk: head -n 50 data.csv | tail -n 11

The result printed to your terminal will be exactly lines 40 through 50 of the original file.

Method 2: Piping tail into head

The alternative method uses the tail -n + feature. This method is often preferred by system administrators because the mathematical logic is slightly easier to read when dealing with very large files.

Using the same example, you want to view lines 40 through 50.

  1. First, use tail to start at line 40 and grab everything to the end of the file: tail -n +40 data.csv
  2. Now, pipe that massive chunk into head. Since you started at line 40, and you want to stop at line 50, you need exactly 11 lines. Ask head to only show the first 11 lines of the piped data: tail -n +40 data.csv | head -n 11

Handling Extremely Large Files Efficiently

While both methods produce the exact same output on your screen, Method 2 (tail into head) is significantly more efficient for massive files.

If you have a 5-gigabyte log file and want to read lines 4,000,000 through 4,000,010:

  • Method 1 (head into tail): head -n 4000010 huge.log | tail -n 11 forces the system to read and buffer four million lines of text before passing it to tail, which consumes considerable memory and CPU.
  • Method 2 (tail into head): tail -n +4000000 huge.log | head -n 11 tells the system to seek directly to line 4,000,000. It starts reading, pipes the first 11 lines to head, and because head terminates after 11 lines, the entire pipeline immediately stops. It never reads the rest of the 5-gigabyte file, returning the result almost instantly.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.