Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm getting broken pipe errors from a command that does something like:

ls -tr1 /a/path | awk -F '\n' -vpath=/prepend/path/ '{print path$1}' | head -n 50

Essentially I want to list (with absolute path) the oldest X files in a directory.

What seems to happen is that the output is correct (I get 50 file paths output) but that when head has output the 50 files it closes stdin causing awk to throw a broken pipe error as it is still outputting more rows.

share|improve this question
    
Please answer your question using the answer your question button below as soon as you can! Then you can even get reputation for it and accept it. –  slhck Nov 28 '11 at 17:11
    
I would have, but as a new user I can't do that for another... 5 hours apparently. I wanted to write the answer down so I didn't forget. –  Jon Nov 28 '11 at 17:11

1 Answer 1

Solution from the OP, revision 2


Turns out I was being pretty dumb.

Firstly there is no need to have awk prepend the path to every single file just to throw most of it away. So the awk statement should be the last pipe.

Secondly instead of reversing sorting with ls we can do a standard time sort and use tail to extract the lines we're after. This ensure the pipe remains open for the entire process.

The new command would look like:

ls -t1 /a/path | tail -n 50 | awk -F '\n' -vpath=/prepend/path/ '{print path$1}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.