Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:44:26+00:00 2026-06-15T13:44:26+00:00

(via https://stackoverflow.com/a/8624829/23582 ) How does (head; tail) < file work? Note that cat file

  • 0

(via https://stackoverflow.com/a/8624829/23582)

How does (head; tail) < file work? Note that cat file | (head;tail) doesn’t.

Also, why does (head; wc -l) < file give 0 for the output of wc?

Note: I understand how head and tail work. Just not the subtleties involved with these particular invocations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:44:27+00:00Added an answer on June 15, 2026 at 1:44 pm

    OS X

    For OS X, you can look at the source code for head and the source code for tail to figure out some of what’s going on. In the case of tail, you’ll want to look at forward.c.

    So, it turns out that head doesn’t do anything special. It just reads its input using the stdio library, so it reads a buffer at a time and might read too much. This means cat file | (head; tail) won’t work for small files where head‘s buffering makes it read some (or all) of the last 10 lines.

    On the other hand, tail checks the type of its input file. If it’s a regular file, tail seeks to the end and reads backwards until it finds enough lines to emit. This is why (head; tail) < file works on any regular file, regardless of size.

    Linux

    You could look at the source for head and tail on Linux too, but it’s easier to just use strace, like this:

    (strace -o /tmp/head.trace head; strace -o /tmp/tail.trace tail) < file
    

    Take a look at /tmp/head.trace. You’ll see that the head command tries to fill a buffer (of 8192 bytes in my test) by reading from standard input (file descriptor 0). Depending on the size of file, it may or may not fill the buffer. Anyway, let’s assume that it reads 10 lines in that first read. Then, it uses lseek to back up the file descriptor to the end of the 10th line, essentially “unreading” any extra bytes it read. This works because the file descriptor is open on a normal, seekable file. So (head; tail) < file will work for any seekable file, but it won’t make cat file | (head; tail) work.

    On the other hand, tail does not (in my testing) seek to the end and read backwards, like it does on OS X. At least, it doesn’t read all the way back to the beginning of the file.

    Here’s my test. Create a small, 12-line input file:

    yes | head -12 | cat -n > /tmp/file
    

    Then, try (head; tail) < /tmp/file on Linux. I get this with GNU coreutils 5.97:

         1  y
         2  y
         3  y
         4  y
         5  y
         6  y
         7  y
         8  y
         9  y
        10  y
        11  y
        12  y
    

    But on OS X, I get this:

         1  y
         2  y
         3  y
         4  y
         5  y
         6  y
         7  y
         8  y
         9  y
        10  y
         3  y
         4  y
         5  y
         6  y
         7  y
         8  y
         9  y
        10  y
        11  y
        12  y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a remote SVN repository that I connect to via HTTPS. The repository
I have a WCF service that needs to be secured via https. The WCF
Does urllib2 in Python 2.6.1 support proxy via https? I've found the following at
Let's say you've got a WCF service that is accessible via HTTP and HTTPS,
I am having trouble access tweets via twitters json search https://search.twitter.com/search.json?callback=?&rpp=5&q=from:secretdreameruk The strange this
I followed the advice here: https://stackoverflow.com/questions/6721702/windows-7-64-bit-odbc-drivers-for-ms-access-missing but it does not apply. I have Oracle
This question is very similar to https://stackoverflow.com/questions/6839516/outlook-2010-reopen-messages which was closed as not being a
This question is an extension of https://stackoverflow.com/q/438163/1168342 I'd like to know specifically if there's
I was looking around for jQuery grid recommendations and came across this question/answers: https://stackoverflow.com/questions/159025/jquery-grid-recommendations
I have a problem POSTing data via HTTPS in Java. The server response is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.