Hello, the beautiful people.
And welcome back.
Now in this video, we're going to be discussing one of the most powerful features of the find command
and to be honest, of the command line in general.
And that is how you can once you have gotten your big list of results from the fine command, how you
can take those results and perform another command on each of those.
This is an incredibly powerful ability.
So I hope you're very, very, very excited.
So you can see here that I'm on the desktop using the command and we're going to make a directory called
copy copy here.
Okay.
Noticing that I'm having an underscore to represent the space, that's good practice, as I've mentioned
to you before, to stop at command line messing up.
Okay.
So what we're going to do, okay, let's say that we want to search the entire file system for every
file that is bigger than 100 kilobytes and less than five megabytes.
So what I want you to do is I want you to just have a go at writing out that command.
So how would you starting from the very base directory, find every single file on the system and that
is greater than 100 megabytes and less than five megabytes.
Okay, so pause the video now go have a go at it and then come back to see how you did.
How'd you get on?
Did you manage it?
If you did, I'm so proud of you.
That's like a technique that you're going to use so often when you work with the command line.
I'm really, really proud that you've got that skill and if you haven't, practice makes perfect.
Don't worry, you get plenty of practice as we as you go with your assignment later on.
Okay.
But this is how I'd go about it.
Okay.
I'd use the find command and we're starting at the base directory.
So we're going to press, we're going to type a slash, we're going to specify that we want only files
by using the type option.
Then we're going to say the size has to be greater than than 100 kilobytes and the size has to be less
than five megabytes.
Now, because we're starting at the base directory here to make sure that our find command has the ability
to go into every folder, we're going to prefix this entire command with a pseudo because otherwise
we get permission, access errors and we don't want those.
So let's just run the whole command as the administrator.
Okay.
So if we if we run that and we're going to find a big list of results, when I give in my my password,
I'm going to find a massive list of results comes out.
Okay.
And by the way, when these things are running, you can actually cancel it from running any command
on the command line by holding the control key and pressing C.
Okay.
And that will, that will stop the command running.
Okay.
If you know it's taken too long or you type something wrong and you realize it and you don't want to
have to wait until the thing finishes.
But we're getting a whole bunch of results and actually we can tell you how many there are when it let's
see, see by piping it into the word count command.
And you can see we've got 3031 results.
So that's quite a lot of results.
Okay.
Now, let's say that we wanted to copy every single one of those files, every single one of those 3031
results that matched our find commands criteria to this copy here folder.
How would we do that?
Well, the way that we would do that is using another option.
If I clear the screen, using another option for the find command called the exec option.
Now the exec option stands for execute.
And what I'm going to do is I'm actually going to maximize the screen to make this a bit easier to see.
So we're saying, okay, once you've done this search here on each result, execute something.
Now here you just type the command as you normally would.
So we'd we'd say copy because we're going to use the CP command to copy, which is, you know, like
we normally would.
Now if you remember how the copy command works first, what you do is you say what you want to copy
and then you say Where.
Okay.
So you say what you want to copy and then you say where.
Well, we know where we want to copy it, which is going to be on our desktop and it's going to be in
the copy here folder.
So that's our path to the copy here folder, slash home, slash, z ad slash desktop slash copy here.
We can write that a bit shorter by just putting the tilde there as well.
That would be fine.
Okay.
But what do we want to copy?
Well, we want to copy every single one of those files.
And the way that you represent that is by using a pair of square squiggly brackets.
So you type some squiggly brackets and that what fills that space is every single one of those files.
So in turn it'll say Copy whatever file one was to this directory.
Then it will copy file two to this directory and copy file three to this directory.
And so we've got all 3000 results.
Now, when you build out these these expressions, you can have multiple of these if you needed to in
your command for whatever reason.
But once you have created it like we have, so we're saying, okay, find every file that's greater
than 100 kilobytes and less than five megabytes.
And once you found all of those, execute the copy command on it and copy it to our copy here directory
on our desktop.
And then once you've finished all of that to end the command, you don't just press enter, you've got
to tell it when you finished your execute option.
And the way you do that is by pressing a backslash and then a semicolon.
Okay, so you see the backslash and the semicolon and that is the command completely finished.
So if I go ahead and run that and we wait just a little while until our shell prompt comes back, what
it is doing is it is copying and pasting every single one of those options into our copy here folder.
So I guess if I stop that for now and then close this and go to the copy here folder.
There we go.
You'll see that we actually have every single one of those files copied and pasted into our copy here
directory, or at least as many as it did before I stopped running the command.
So it's not awesome, but we've managed to just use the find command to not only find what we want on
the whole system, but also execute a command on each of the results.
So just as an aside, depending upon what type of hardware that you're running, running this command
in its entirety might give your computer a bit of trouble.
So if you're finding that your virtual machine crashes, just try and run the whole command to just
a maximum depth of four to save it, you know, finding every file on your file system, this should
reduce it quite significantly the load on your computer anyway or try and maximum depth of three and
that should that should you know stop any problems if you find your virtual machine is crashing.
But first of all, what I want you to do at the minute is just take a look at this command.
Isn't that awesome?
I mean, considering that you haven't you maybe didn't know anything about the Linux command line just
a few videos ago.
Now you're looking at this command, you're probably thinking, wow, if I actually come that far and
this is the thing, the Linux command line isn't like, you know, unbelievably complicated.
It's just a few concepts that if they're explained to you badly or you don't quite understand what they
mean, it can be an absolute nightmare.
But once you understand them, it's very easy to take, you know, just a few building blocks here and
build something incredibly powerful.
So here you found a way to search for every file in your system in between 100 kilobytes and five megabytes
in size, and copy them all to a specific folder in just a command.
And yet now you know how to do that.
So I think that's amazing.
And I really just wanted to pause in this video just to say I'm very, very proud of you for making
it this far.
And you're going to get many more congratulations on this in the conclusion section four for this section
of the course.
Now, what I want to do is I want to move on just a little bit to talk about this exec option here.
This this exec option is awesome.
It allows us to execute a command, but it's not the safest of all things.
So if what you can do if is instead of using the exact option, you can actually type the OC option.
And what this will do is it'll do exactly the same.
It works exactly the same.
You don't have to change anything.
But instead of just doing the action directly.
So if I, if I try this, it will now ask me every time that it tries to copy something, whether it's
OC or not.
So if I type Y, it'll continue.
And if I type Y, it'll continue.
If I type, no, it won't, and so on.
So I have a choice here about what it does for each file.
Now, this would clearly be a massive pain if you had thousands of files, but it's a good thing to
bear in mind if you are doing something for a few files and you're doing something destructive, like
deleting or moving files instead of just copying, like in this case, and you want to make sure, sure
that nothing goes wrong, you can use the OC option instead of the exact option in order to do exactly
the same stuff.
So the OC option is exactly the same as the execute option.
You don't have to change anything else from, from here on.
Just, just bear in mind that, you know, it's, it's, it's a bit safer than using the exact option,
but it, you know, it's a massive pain if you've got thousands of files to do so.
As I was saying, I'm really, really proud of you for getting as far as you have in this section,
in this video.
You could, by the way, you can cancel your way out of the OC option by pressing control and c like
you see I've just done here and of course you can clear the screen by pressing control and l but what
I want to do in the next video is we're going to take this to the extreme, okay?
We're going to really, really show you how powerful the fine command can be by doing a little finding
the needle in the haystack kind of activity.
In the next video, we're going to create a folder called Haystack that contains 500 other folders.
And in each of those folders, we're going to make another 100 files.
And this will be very easy using our knowledge of and knowledge of a brace expansion.
But inside one of those folders at random, we're going to create a file called Needle Dot txt, and
then we're going to use the find command to find the needle in the haystack and move it into our home
directory.
Okay.
So out of about 50,000 possible files, we're going to find that one and copy it exactly where we need.
And it's going to be so easy to do.
You won't believe it.
Okay.
So what we're going to do is we're going to shift over into the next video and I'll see you there to
find the needle in the haystack.