Suppose I use the printf in the find command like this:
find ./folder -printf "%f\n" | other command which uses the result of printf
in the other command part, I may be having a sort or something similar
what exactly does printf do in this case? where does it print the file names before the process in the part after “|” happens?
if I sort the filenames for example, it will first sort them, and then print them sorted on the monitor, but before that, how exactly does the part after | get the files unsorted in order to sort them? does the printf in this case give the filenames as input to the part after | and then the part after | prints the file names sorted in the output?
sorry for my english 🙁
Your shell calls
pipe()which creates two file descriptors. Writing into one buffers data in the kernel which is available to be read by the other. Then it callsfork()to make a new process for thefindcommand. After thefork()it closesstdout(always fd 1) and usesdup2()to copy one end of the pipe tostdout. Then it usesexec()to runfind(replacing the copy of the shell in the subprocess withfind). Whenfindruns it just prints tostdoutas normal, but it has inherited it from the shell which made it the pipe. Meanwhile the shell is doing the same thing forother command...withstdinso that it is created with fd 0 connected to the other end of the pipe.