I don't understand exactly what xargs
does that's why I'm surprised why these 2 return different results:
find ~/Downloads -iname *btsync* | ls -al
find ~/Downloads -iname *btsync* | xargs ls -al
Why the first one doesn't return what I want? Instead it prints show all the files in the current directory.
Not all programs take input. The
ls
command can take a directory or a file as an argument (e.g.ls /etc
) but you can't pipe (|
) to it. So, the first command is the same as doing:The pipe is ignored because
ls
has no way of reading from the standard input.xargs
, on the other hand, does something completely different. It reads standard input and then runs the command you give it on each line of the input. Fromman xargs
:So,
xargs
will take each result of thefind
command and runls
on it which is what you want.