This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Search matching files by combining find and grep with xargs

    Bonnie EisenmanBonnie Eisenman

    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.

    grepgrep
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss