Transcript from the "find & grep" Lesson
>> So, we talked about redirection, we talked about the power of standard streams, standard in, standard out, standard error all functions like these makes it really easy to chain things together. And we learned what is this do, foo > bar. Next, let's talk about finding things, you see me grep a few things here and there, grep is one of my favorite commands because it can make sense of really, really large output.
[00:00:27] Earlier, we tried to play around with AWK, AWK, that gets a little tedious, you've probably heard Aachen said those are really powerful commands. But I don't think we're gonna dive into them this class because they're a little bit tricky, but we are gonna use find, we are gonna use grep, and we'll see how powerful these tools can be.
[00:00:49] So, find the syntax, looks a little strange, but once you're you understand it's find, where to look, what exactly you're looking on? Is it a name? Is it a file? And then where to look, see, find actually is pretty accessible. And this is a good slide to keep as a reference, because it's not really intuitive to remember these off top your head unless you're doing them constantly.
[00:01:13] But this is the syntax, find, where you're looking, what you're looking for ,and then what particular are you looking in exactly? Let's try it out. So, can you find all the log files in /var/log? We talked about all the log files, when you look in there you will see how many there actually is, remember, most things you logged.
[00:01:48] Star logged star.
>> Mm-hm, let me try that, does it work? The great thing about finding things, and if you can't get it right, you're not gonna break anything.
>> It's a read only operation.
>> And that's how you do it, find, we're finding, where are we looking?
[00:02:09] Slash for our log, the type, dash type, we use f for file, we're looking for things with the name of star.log, but you probably ran into an issue, what's the issue you ran into? Actually, I already know what issue you ran into, but that was a rhetorical question.
[00:02:31] So, you probably ran into an issue with this command, let's find out why. So, clear, so we'll go before, find/ var/ log, and I'm looking for files. So, type f -name. So we start out log, so I want everything that ends in a log. Mm-hm, that's okay, but I'm still getting permission denied, why is that?
>> Sudo find.
>> Yeah, we didn't sudo it, however, here is a very handy shortcut that I use all the time, if we just say sudo!! It will rerun that command as sudo, and that's a nice shortcut I use all the time. Sudo(!!) It just runs the last command in your history with the sudo applied to it.
[00:03:29] So, that way you don't have to go through and retype sudo and the whole command again. So, we found all the log files, and we learned how to use the sudo(!!) command, you'll use that way more than you think. So next, can we find all directories with the name log?
[00:03:48] I'm just saying log cuz I know there's a lot of manual default installation.
>> Yeah, let's try that one. Say find / -type, so the slash is?
>> Roots, that's right, so we're looking at the very top level of our file system, type is d for directory, just anything named log.
[00:04:13] Oops, forgot the name. Name log, we get a lot of permission denied because, again, you, generally, don't wanna be sniffing around root, the file system doesn't like it, you don't need to be in most of these paths. But if you really, really wanna look, pseudo bang bang. Yeah, so funny thing, it's just part of just muscle memory, it's not that difficult, it looks like it's more complicated than it is, you just have to remember to find where that type of thing you're looking for, that's it.
[00:05:06] So, next we'll try a grep, find is the where something is, and grep is looking inside. So, find will just give you the files, but grep looks inside the files. And grep is, grep the option you're looking for, what expression, a regular expression you're looking for and then where?
[00:05:36] And bonus are something called zgrep, which you can look inside the zipped files, which is pretty awesome too, you don't have to unzip them to search. You're like, what's the benefit of this? Grep is pretty cool, grep is the one you're probably going to use a lot more than find, for example, if we wanna find all of our running node processes.
[00:06:01] Try running this command, but try running it without typing into grep first, and see what you see, I'll go ahead and run that here, ps aux, that's a lot to cipher through. I can't read all this, and there's not even that a lot going on in our system, we're running what?
[00:06:19] Engine X and Node, the only two things we're really running, that's a pain, so how about if we pipe this into grep, so when you use that redirection of the pipe, I'm just gonna say grep. And I wanna find the node process, there we go, look at that, easy peasy.
[00:06:42] And that's why we use grep a lot when you have this giant output, and you're like, I just wanna find one thing, we pipe it to grep to find something for us. You will use this command quite a lot, the pipe and the grep, and I'd say grep is probably a little bit easier for you to kind of wrap your heads around.
[00:06:59] And you can use regular expressions if you want to get really, really specific, but, generally, this works, grep whatever thing I'm looking for, it works pretty well.