2

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.

slhck
  • 235,242
Jon
  • 21

1 Answers1

0

Solution from the OP, revision 2


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 ensures 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}'
Melebius
  • 2,059
slhck
  • 235,242