Hello, very beautiful people.
And welcome back.
We're going to be continuing on our discussion of the find command in this video by starting off showing
you how to search for things by name.
So let's say we want to find this file here called Five-test and we're back in our home folder.
We're not in our documents folder anymore, we're just in our home folder.
And we know that this five-test is somewhere in our home directory.
It's somewhere below this point in the file system, but we're not exactly sure where and we're going
to use the find command to find it for us.
So if we clear the screen, let's say, okay, we're going to find command.
We're going to start from this point in the file system, by the way, this is just a matter of personal
preference.
Actually having the dot, you don't need it, but I'd like to put it there just to make it a bit more
complete.
Okay.
So there we are find we're starting at this position in the file directory and we want to search by
name.
So we give it the name option again just with one dash and then in speech marks.
So in double quotes, we want to search.
We're going to put the pattern that we're searching for.
We're going to put five txt and it does need to be in quotes, otherwise the find command plays up some
time.
So make sure you just put it in speech marks in these double quotes and you know that that will be that
would be much better.
So if we do if we do that and press enter, we see that the fine command has indeed found for the five
dot txt and it's given us the path all the way to it starting at the current working directory.
So what if we wanted to find three dot txt?
That will work as well.
So it's found a path to three dot txt.
You can build regular expressions in here.
So if I put a star it will find every dot txt file from this point in the file system.
So we see we've got one in music, we've got find me 4.2 which I'd forgotten created earlier in the,
in the, in the course.
We've got some in some hidden folders, we have them all over the place.
We can actually also limit the depth that we search to.
So if we go to a maximum depth of, let's say two, remembering that we're going to put the max depth
option as the first option, otherwise we get a big warning, don't we?
So if I do max depth to name any name that ends in txt, we can see that we found all the files at ending
not txt but only going to depths in.
So that's really, really cool, right?
You can search by name like that and you can also have you can also have wildcards.
So I could do file and then a number txt and nothing comes up.
What if I did?
What if I did something like question marked txt nothing comes up.
But if I go to a big depth of like let's say ten now we see that we just get 4.5.3. txt and two point
txt, but these find me one find me to find me four etc. are ignored because they don't match this world,
this regular expression.
So that is a really powerful ability.
Just by building upon your knowledge of regular expressions and wildcards, you can really tailor the
fine command to your needs.
And the one thing I should note here is that you can't do Bray's expansion inside inside the name Commander.
So that's just something to bear in mind.
Okay?
At least, at least inside double quotes.
Let's just leave that there.
Okay.
So the the other thing is that you can search in a case insensitive way by searching dot in name.
So if I change this here to txt all in uppercase, let me clear the screen.
TXT in uppercase that will now match.
Okay.
I probably did that wrong way around.
If I do it like this, just search name.
Okay.
Now you'll see nothing matches because now it's searching case sensitive but if I change it to I name
so it's case insensitive searching for the name so uppercase and lowercase don't matter now it will
match because it doesn't care that this text is in uppercase.
So that's good.
If you're not sure whether you put an uppercase in the file name, it's good to search with IE name
to make the search just a bit more general.
Now another interesting way to search for files with the find command is by file size.
So let's say that we wanted to search the entire file system for every file that is larger than 100
kilobytes.
So let's clear the screen.
How would we do that?
Well, what we do is we type the find command.
Then we need to give it, of course, a place to start searching from.
So let's start from the very base of the file system, because we want to find every file on the system.
So let's start at the very base of the system.
We want to search for files that are larger than 100 kilobytes.
So let's say that we want files.
So let's specify a type of F, so because we're just looking for files, not folders, and we want a
size.
So we're going to give it the size option.
Again, one dash.
And now this is how you'd say greater than 100 kilobytes.
So you see the plus meaning greater if it was less than you'd have minus.
So that's less than 100 kilobytes.
But we want more than so more than 100 then a lowercase k for kilobytes.
So if I press enter you'll see that we get a whole bunch of.
As we look for all the files that are bigger than 100 kilobytes starting at the very base of our file
system.
But one thing you might have noticed and if you had a quick I if I scroll up to the top is that we got
a lot of permission denied errors.
If I can show you by scrolling all the way back up.
There we go.
You can see that we've got a lot of permission denied, permission denied, permission denied.
And the reason is that's because we started at the base of our file system.
And remember, the find command will try to enter every folder, but it couldn't because it didn't have
the required privileges to enter some of the folders in our base directory that are only accessible
to the root user, to the super user, to the administrator.
Okay.
So to fix that, we just prefix the command with the sudo command.
So we're going to go sudo and then do the rest of it and we'll get a whole lot less errors because we're
not going to have those access errors before now.
We can count the amount of results that we actually got out of this by piping the entire thing into
the word command with the WL option.
And what this will do is it will count the amount of lines in the output that you give it.
So if you notice when we run this, we get one result per line.
So if we pipe all of that into the word command and give it the WL option, this will count how many
lines there are.
And if we do that, we get 3144 results, which is a whole bunch of results.
Now, one thing that's interesting is the way that you can chain together different sizes.
So here we're saying it needs to be greater than 100 kilobytes, and this is how we'd say it has to
be less than five megabytes.
So here what we're doing is we're chaining together all these different requirements.
We're saying it has to be a file and the file size has to be bigger than 100 kilobytes, and the file
size has to be less than five megabytes.
And if we count how many of them there are, we see that we get a quite a few less.
Now we're going to get 3030 files instead of 3144.
So there's about 114 files that aren't being shown anymore because they they were bigger than five megabytes.
So if I click the screen, one thing that I really want to highlight here is the way that we're chaining
together different requirements.
Okay.
So here we're saying it has to be a file and the size has to be greater than 100 kilobytes and the size
has to be less than five megabytes in order for it to be contained in our requirement.
Okay.
So how would we say something like, okay, I want to find all the files that are less than 100 kilobytes
or bigger than five megabytes.
Okay, well, the way that we do that, I mean, first of all, if we wanted to say less than 100 kilobytes,
we'd do that.
And if wouldn't say greater than five megabytes, we need to change that to a plus.
But at the minute here, let's see how many results we get.
We're going to get zero results because it's impossible for a file to be less than 100 kilobytes and
greater than five megabytes.
You can't be bigger than five megabytes at the same time that you're less than 100 kilobytes.
That's not possible.
What we want to say is it needs to be less than 100 kilobytes or greater than five megabytes not.
And it needs to be less than a hundred gigabytes or greater than 100 greater than five megabytes.
The way that you do that is you type a-0 in between the two requirements.
So here what we're saying is it's got to be a file and its size needs to be less than 100 kilobytes
or greater than five.
So if we do that, you can see now that we get a whole bunch of results, 199,112 results.
Now, you can go quite wild with this, this ending and ignoring, but I think that will do what I think
that will do for now.
You can also do not and loads of other different logical operations and if you're familiar with Boolean
logic, but I'll leave that up to you if you want to find more by looking at the man pages, it would
just be a bit of a mess to show by video.
So we've covered quite a lot in this video.
We've seen how to use the name option for the fine command to search for things by name, so you'd use
dash name and then give it in double quotes, a certain regular expression that you wanted file names
to match so I could do anything ending in dot txt for example, using the star wild card and that would
give us all the files that ended in txt.
And you can make the search case insensitive so that uppercase and lowercase don't matter by using the
by using the the enum option.
So now even though we have said dot text in uppercase, it doesn't matter because we're using the name
option.
So that's the first thing we looked at how to find files by name.
Then we took a look at how you could find files by their file size and you saw that you use the size
option and then you give it a certain file size and the plus means above, the minus means below.
Or you could actually give it just exactly.
For example, 500 megabytes.
That's exactly 500 megabytes.
This is above 500 megabytes.
This is below 500 megabytes.
A capital G, by the way, is gigabyte.
And you can find more on that in the man page for the find command.
But in the next video, what we're going to be doing is we're going to be jumping on to some very exciting
stuff.
And what we're going to be showing you is how you can use the find command to not only give you a whole
bunch of results, but in the same command also execute another command or even another pipeline on
every single one of those results.
So this is a this is a turbocharger for your work on the Linux command line, and I hope you're very,
very excited.
So let's cut the video here and jump over into the next one where we're going to learn how to gain even
more superpowers.