I recently came across the following two examples
Example 1: List only directories
ls -l | grep "^d"
Example 2: Search inside files
ls | xargs grep -i "Find Some String"
Now i have read from here that
If you just use a pipe, it receives data on STDIN (the standard input stream) as a raw pile of data that it can sort through one line at a time.
So now I am a little confused as to why grep requires xargs in the second one and not in the first one. From what I understand so far is that xargs is used for converting stdin
into arguments
. (Since some programs require arguments instead of stdin). Does this mean grep can take both stdin and arguments ? I would appreciate it if some one could explain why is xargs used in the second example why cant it work without xargs? What purpose is it serving ?