Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does piping ls to head stop it's execution half way? [duplicate]

Tags:

linux

bash

pipe

If I run ls folder | head in a directory with a lot of files, the execution time is about 50 times faster than ls folder | tail. Does the head command stops ls from executing wholy when it has enough (10) lines?

I couldn't find the answer to this anywhere because "pipe to head" gives me tons of unrelated results on Google or here.

If the answer is no, then is there a more efficient way to list only some files instead of running ls completely and cutting the output with head?

like image 717
dr_ate Avatar asked Sep 01 '15 22:09

dr_ate


1 Answers

Once head has read enough lines from it's stdin (stdout of ls), then it exits and therefore closes the stdin. When that happens, the pipe is considered to be broken and ls receives a SIGPIPE signal. As a reaction to that it terminates as well, way before processing everything that it would normally do. In the case of tail, it has to wait for ls to terminate to know which X number of lines were the last ones.

like image 188
ntki Avatar answered Oct 13 '22 23:10

ntki