Hello, you beautiful people.
And welcome back where we're going to be continuing our discussion of the find command and how we can
control the search depth that the find command searches to.
So if I open up our file explorer, I've made a few, few new folders.
So in our documents folder I have made a folder called Level two.
And inside there I've got a file called two dot txt and then a folder called level three.
And inside there I've got a file called three dot tfsi then level 44. txt, level five and five point
txt where it ends.
And we're going to be using this to take a look at how we can control the search depth that the find
command searches to.
So all of that stuff is in our documents folder, as you can see here.
So I'm just going to change into our documents folder and use an LS and you can see that level two is
in there.
So this is what we're seeing at the minute, this is what we are currently in.
So let's clear the screen and start our discussion.
If I just run the find command from here, the find command will search to an infinite depth.
It will list every file and every folder from this point in the file system.
Okay, so from here and downwards.
So if I run that, you'll see it shows us level two and it will show us everything inside there.
So you'll see we get to dot txt and it shows us a level 33. txt, level four, 4.2 SDA level five and
five SDA.
Everything is shown in there, but we can control the depth that the fine command searches to by using
the max depth option.
So let me show you how we do that.
So if we type find we're going to again start from our current working directory, we can actually specify
that to make it a bit clearer by using the dot.
By the way, remember dot means this directory, so I'm actually going to use this from now on.
I'm just going to always start by saying, okay, start from this directory by using the dot.
So the max depth option, you type one dash and then max depth and you can specify the depth here.
So let's say we put one.
So the first thing you might know is, is Ziad, why is there just one dash?
I thought you said for long form options you need to have two dashes.
Well, that's right.
But for the find command, you only have one dash for these options because there's so many of them.
There's so many long form options.
I guess they tried to save you typing by just reducing it to one dash.
Okay, so here we are.
We have just one dash max depth and then one.
And this is saying only search to a maximum depth of one.
Really.
So if we see that, let's see what happens.
You see here that we only see the level two folder.
So a max depth of one is just like seeing what you see here.
It only shows you what is in the current directory.
Okay, now if I do max depth of two, that's like showing you what's here and what's one level down.
Okay.
So if we if we take a look here, what we should see is we should see level two and then it will go
inside and it will show you to TXT and the level three folder.
So if we run that max depth of two, we see level two, which is here, it's showing us the level two
folder.
This is where we start.
So it's showing us the level two folder going in there, showing us to dot txt and level three.
So we see the level two folder two point txt and level three.
Okay.
Now if we want to go to a max depth of three, that's going to go down another level into level three.
So if I run that, we'll see now that we see three dot txt and level the level four folder shown as
well.
If we keep going deeper, if we do max up the four, we're now starting to take a look inside level
four.
We're seeing four dot txt and the level five folder and if we do max depth of five, it's going to go
down even further all the way until we see five txt and you can give it like you know, a max depth
of 100 and it will still work because all you're defining is a maximum depth.
Okay?
It doesn't mean it will go all that way.
That's just as far as it could go if it was possible.
Now by default there is no maximum depth, so it will list everything here.
We didn't have to specify a maximum depth if you wanted to go all the way into the file tree.
But if you want to limit how far it goes, that's when you use the max depth option and you say, Hey,
I only want you to show me what's in this folder.
That's when you use max depth of one, and it's incredibly useful for listing out what's in the current
folder.
Now, if we just clear the screen, there's another really great option that the fine command has,
which is the type option.
So if we just go all the way back up to our documents here, the type option allows you to search for
just files or just directories.
Notice by default the find command lists both.
It lists both files and directories, but the type option lets you specify just one.
So if I start here in our documents folder and I list out everything starting at this folder, you see
that we list out the folders and their files.
But if I said, okay, I just want to list out the files, what I do is type the type option with again
noticing just one dash and if I just want files, I would type f f for file.
Okay.
So if I press enter now.
Now you'll see that we just get 4.5.23.2 HD and 2.2 HD.
We just get the files.
Likewise.
I could just get the folders by pressing instead of having f have dx.
So if I type dx, you know, we now just get the folders.
So level two folder, the level three folder, the level four folder and the level five folder.
Starting at this directory here and you can combine this of course with the max depth option.
So if I do max depth of one now all we're doing is we're going to a maximum depth of one and just showing
the folders.
Now I did this on purpose to show you that if you put them this way around, you'll get a warning and
it says warning.
You have specified the max depth option after a non option argument type, but options are not positional.
I'll tell you what this means shortly.
Really quickly.
Okay.
What this means is just put max depth first.
So that's all you've got to do.
Okay.
And that warning goes away.
So if you're going to specify multiple options, just put the max depth first.
If I if I clear the screen, you'll see that warning goes away.
So now we're searching to a maximum depth of one and showing all the directories.
Let's go to a maximum depth of two.
We're now seeing the level three folder.
If I go to a maximum depth of four, let's say oops, not 42, max depth of four as do three.
Can't type today.
Maximum depth three.
We're going to level three and we're seeing the level four folder.
And if we go into the level four folder, we'll see the level five folder.
And if we go into the level five, go to level five, it won't make any difference because all that's
in level five is another file which isn't being shown because all we're looking for is directories.
So if I go and change that to an F now, you'll see we list all the files and you know, it works exactly
the same.
So these are really good options to combine together and there's loads of things that you can do to
customize the way that the find command actually searches.
But just to quickly summarize that the type option has two possible inputs.
You could give it F for file or D for directory.
And if you're going to use it with the find command, make sure that you give it the max depth option
first.
And if we're going to specify where to start from, make sure that comes as well.
So if we're going to do in the C folder and we're going to list every single file in the Z folder,
let's say, but only to a maximum depth of one.
There you are.
This is every single file that is in the Z folder.
Oops.
Let me just clear the screen and do that.
This is every single file in the C folder without any directories.
And if we just want to see the directories, then here they are.
So that's a very useful combination of commands to have there and it will come very useful in your assignment.
So I'm going to break this video here, keep it nice and short and sweet.
And in the next video, we're going to pick up with some more awesome stuff you can do with the fine
command.