Let's say that I had a bunch of .php files, and a bunch of javascript files, and
other files that have functions in them, but I only wanted to look in all of the .php
files. How I would do that is by using the "find" command. So, in the previous
video we learn that we can look in the current directory for "-type f" "f" is for
file or "d" for directory, and we can give it what we're looking for.
So, in this case we're gonna be looking for all .php files, and the "-iname"
means ignore case sensitivity in the string that we're looking for, so we
found .php files with extensions in both lowercase, uppercase, and a mixed case here.
That's cool, but how do we use "find" with "grep"? And, this is going to be the big thing
that I want to show you guys, this is really handy. So, what you can do, you
can give it a certain search through the "find" command. So, we're gonna "find -type f
-iname "*.php" and then what we can do is we can pass the find
command another flag which is going to be "-exec" So, what this is gonna do it's
going to first find all files that match this criteria, and then it's going to
execute the next command on it. So, "grep "function" and I'm going to pass the "-i" and
"-n" flags here, and then we need to add " +"
end the "-exec" flag. So, everything in here is going to be executed within this flag.
So, if I hit enter it's going to look through all .php files, and it's gonna
find case sensitive,
or case-insensitive, functions with line numbers.
So, that's really cool. We can use this to "find -type f -size -10k"
and we can group search parameters here. So, I just passed a flag
of size and the value of that flag is under 10 kilobytes, but I'm also gonna
pass it "-iname" which is going to be "*.php" So, this is gonna find all . php files
that are less than 10 kilobytes, and then I can "-exec grep -i -n "function" +" in here. And, right now
that looks the same cause I'm pretty sure all my files are less than 10
kilobytes, they don't have much in it, but but you can kind of see how this works.
So, this is a really powerful tool, and you guys can use grep and find together
to search through a very specific set of files for very specific strings and
instances.