How to Peek at Top, Middle, or Bottom of Text File Without Opening It in Linux

Let’s say you have a really large text file. By large, I mean it has a lot of lines of text in the file. I frequently work with files that have millions or billions of lines of text in them. If I try to open the file in a text editor, it usually takes a really long time and can sometimes cause the text editor to crash. I’m sure there are text editors out there that handle this better, but sometimes you don’t have the luxury of using your preferred tools. In Unix/Linux, one way you can “peek” at the top of a file is to use the head command. For example, the following command will let you look at the top 100 lines in a file.

head -n 100 myfile.txt

Or if you want to see the bottom 100 lines, you can use tail:

tail -n 100 myfile.txt

But let’s say you have 1,000,000 lines in a text file, and you want to see what’s on lines 500,001-500,010 without opening the file. You can use a little trickery by combining head and tail.

tail -n +500001 myfile.txt | head -n 10

The way this works is that the tail command outputs the file starting from the value specified after “+” and then that output is redirected (via a pipe) to the head command, which then displays the top lines of that output. So the tail command above would output lines 500,001-1,000,000. And the head command would spit out the first ten of those lines. Fortunately, you won’t see the output of tail on the screen. It will only display the final result. (See also http://www.fastechws.com/tricks/unix/head_tail_mid_files.php.)

Leave a Reply