Remember that we can use find to find all instances of a file whose name matches a given pattern. Let's say I want to find all of the spec files in the example's directory. Next, we're going to pipe this command into xargs.
By default, what xargs does is it takes its input and then it executes a command on it and by default, it'll use the echo command. Running xargs echo does the exact same thing as what you see right here.
Now, let's search through all of these files. Instead of typing find examples-names spec.js and then piping that into xargs echo, we're going to pipe it into grep and we're going to look for all of the instances of the word describe.
We also have the controller spec from Vanilla JS and we can see that we're getting all of these described blocks from them, just as if we had used grep ourselves.
When I use xargs to invoke grep, I can use it just as I normally would. Whatever I type in, it's going to work the same way as if I had been running grep over multiple files manually. The way that this works is that...
What's happening here is that find is outputting all these results to standardin, which is getting piped into xargs and then xargs runs the utility -- which is grep right here -- then it passes whatever else I type here as arguments to grep.
Now, if I wanted to run the same search without using xargs or find, I could just do it in grep. I could type grep-r for recursive, then --include in order to limit which files I'm searching for and tell it that I'm looking for stuff ending in spec.js, and then, I could type my search query and the folder I want to search through.
It'll give me the same results. Now, technically, this is possible using grep and not using find or xargs, personally, I don't think that this ordering of various arguments and flags makes a whole lot of sense. The other benefit to using find and xargs together is that you can debug different pieces of your search in isolation.
For example, when we run find examples-name *spec.js, we can verify that we're actually looking at the files we want to before we go ahead and construct our search and from there, piping it to xargs and searching for our search string is much, much easier.