In this video, you'll learn how to find files.
So we have seen LS already.
LS is useful if you want to list files, but not to find files.
Which is a convenient utility.
It looks for binaries in the path variable.
And you remember the path variable is a list of directories that typically contain executable
files.
Locate uses a database, but this database needs to be updated by the updated DB command.
Otherwise, locate won't work.
Most powerful of all is the find command.
That allows you to find files based on many criteria.
So here we have a couple of examples about the find command.
And instead of reading you through all of these examples, let me just run these examples
for you.
So let me start by doing locate hosts.
And then what do I get?
I get an error message that locate no such file or directory.
That is because locate works on a database that you need to generate using the update
DB command.
And that's what I'm doing right now.
And then when I use locate on hosts, oh boy, it is showing me all file names that have
hosts somewhere in the name.
Not very useful, but in combination with grep, it might help you to find what you want to
find.
How about which?
Well, that's not working because which is only looking for executable files, which are
typically in a limited number of directories in your path.
So which pass WD, for instance, is showing user bin pass WD.
But if you want something really powerful to find files, use the find command.
I'm going to put sudo in front of it because I don't want to meet any limitations by directories
that are not searchable for an ordinary user.
So if you do find slash, slash is where I want to start my find operation minus name
hosts.
I want to find files that have the name hosts.
And notice that these files with the name host, that's an exact match.
So we are not looking for files that have hosts somewhere in the file name.
No, we are looking for files that have the name hosts.
If you are looking for somewhere in the file name, you need wildcard characters.
As you can see, that is giving a little bit more.
Now what I like a lot about find is that you can also use it based on other properties.
So find slash minus type F, which means I'm only looking for files.
Minus type D, for instance, would only be looking for directories.
Minus size is looking for a size, and I want a size that is bigger than 100 mebibytes.
And there we can see a list of all files that are bigger than 100 mebibytes.
It can be very useful if ever you run out of disk space on your system, then this is
going to help you.
Now how about sudo find slash etc minus exec grep minus l student opening curly brace closing
curly brace backslash semicolon.
Wow, that's a complex one.
So what are we doing here?
Well, I am going to look for files in the etc directory.
In these files, I'm using the grep command, and the grep command will list file names
if the file contains the text student.
How should you see this?
Well, minus exec allows you to embed another command.
And here highlighted, we have the embedded command.
So grep minus l student curly braces.
The curly braces point to the result of the find command, and this should show me a list
of files.
And oh, boy, that's a lot of error messages about its directory.
So let's take that, and let's put a two-grade then dev null behind it so that we have to
create text result.
And here we can see the result of the find command.
So the interesting thing here is that we have this embedded option to do stuff with the
result of your find command.
And you can do this once.
You can even do it multiple times.
So minus exec cp curly braces to refer to the result of the find command.
Find contents.
So what do I want to do?
I want to copy it to a directory in the current directory name.
This directory has the name find slash contents.
And also I need the backslash semicolon to close this command.
So not only are we going to look for files that contain a text student, we're also going
to copy these files to the directory find contents.
But oh, boy, now I have a problem.
And my problem is that I don't have this directory find contents.
So fortunately, I can easily fix that.
I'm going to use control A to move my cursor to the start of the command line.
And there I'm going to use mkdir with a minus P option to create the entire path to create
the directory slash find, to create the directory find slash contents.
And then I need to separate the mkdir command from the sudo find command.
And that's what I'm doing with the semicolon.
Now what is also interesting in this quite complex command line is that we have three
semicolons.
One semicolon does have nothing in front of it.
One is this one.
And there the semicolon is a command separator.
It allows you to run multiple commands in one run.
Then we have a semicolon with a backslash in front of it.
This backslash, as you probably remember from the lesson about the bash shell, is escaping
the character.
What does it mean?
That means that during the bash command line parsing, that's a process where bash is reading
your command line.
Bash is in the first run not going to interpret the semicolon.
And that's good because the semicolon normally is a command separator.
But within find, the semicolon has the function of closing the minus exec.
And that is why we need a backslash semicolon for every time you are using minus exec.
So let's run it and see how it is working out while it's listing these files.
And if all went well, then in find contents, we can see that the files have been copied.
So this minus exec option on find is a really powerful command line option.
And I would advise that you try to master it and fully understand it.
Now next, I want to show you another example.
And that is sudo find slash etc minus name star and minus type F.
What does that mean?
Well, basically it means that it's going to show all files in etc and its subdirectories.
And then I'm going to use xargs on it.
Xargs allows you to run a command on the output of the first command.
So xargs grab 127.0.0.1 is going to show you lines in all these files that contain the
text 127.0.0.1.
And oh my goodness, what is happening here?
Well, I'm getting a lot of permission denied.
And is there a reason for that?
Yes, there is a reason.
In this construct here, I'm using sudo only on my find command, but not on the xargs command.
Now there's a trick in order to fix that.
And that is sudo sh minus c.
And then you put the entire string between double quotes.
Now the double quotes here are confusing, so I'm removing the double quote here in front
of the 127.0.0.1.
And there we can see the result.
So what is this doing?
Sudo, in fact, in this case, is starting a subshell.
SH is running the subshell.
And in that subshell, we want to run the command that is between the double quotes.
And because sudo is starting the subshell, the entire string between the double quotes
is executed in this sudo subshell.
So it's just showing all the files that contain 127.0.0.1.
I know that's pretty tough, right?
But you should try to figure out how this xargs command is working, because it is really
a powerful command.