Search matching files by combining find and grep with xargs

Bonnie Eisenman
InstructorBonnie Eisenman
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

Learn to use the xargs Unix command to combine find and grep. First we can use find to identify which files we want to search; then we can use xargs to tell grep which files to search through.

[00:00] 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.

[00:15] 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.

[00:26] 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.

[00:37] This is a very common thing you'll see in JavaScript tasks. When I do that, you can see that we have all of these different files. We have the directive spec under the unit test from AngularJS.

[00:49] 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.

[00:59] 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...

[01:11] 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.

[01:26] 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.

[01:45] 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.

[02:02] 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.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today