In this video, we'll talk about a find utility.
The find utility can find files based on multiple properties, like
file names, but also username or size, and much more.
Find is the most efficient utility to find files, but on large systems with many files, it may be slow.
When combined with the exec option, specific commands can be executed on the result of the find command.
Let's check out some examples for your convenience. I've listed the examples on the slide as well.
So let me start by using find/name hosts, which is doing what? Oh no, so
many. Permission denied. Well, you know what, I don't want to see these Permission denied.
So I'm going to put sudo in front of all of my
find commands so that we don't have any access permissions to files.
There we can see still two error messages, but these are to
be ignored. We can see four files that have the name hosts.
Now, the thing is that when you use find, the name that you specify is the exact file name.
And if you want it to be occurring in the file name, but not
the exact file name, you might want to use the star, the wildcard character.
And here we can see that. Now it also contains files
like ghostscript, which does contain hosts, but it's not the same.
You can also use find based on a user account.
Now, I didn't create many users. Let me quickly create a user user adlinda and
then I can use sudo find user lynda and there we can see all files
that are owned by user. Linda, in case you are wondering, how come you just
created the user? Well, that is because when you create a user, a home directory
is created and some files are copied into the user home directory.
Now, find can also be used on size, so sudo find minus size.
So sudo find size plus 200m, which is
showing files that have a size larger than
200 megabytes. That's not too many.
And you can find based on permissions.
So sudo find perm for three.
Now, what is this 4,000?
Well, that is a search that makes sure that you find files that have a 4 in the first
permissions. For now, that doesn't make too much sense. But later on, when we have learned about permissions, you
will learn that this is for files that have the set user ID permission, a potentially dangerous permission.
Now, one very powerful feature of the find command is that you can
also run a command based on the result of the find command.
So sudo find user lynda and let me
first use a exec LS l semicolon.
So what is this doing? Well, it's first finding files owned by user Linda.
And on the result of the find command, it is using the exec command.
So it starts with exec and it ends with a backslash semicolon. And the command to be executed is LS L.
This is what it looks like. Long listing of all these files.
Is that so?
Look at that.
This is not owned by you, Solinda.
Well, you know what? I made an error, but this is a nice error and
that's why I didn't edit it out. I use LS L without anything behind it.
We need to put something behind it because without anything behind it, it will just list files in the current directory.
I was missing the opening and closing curly braces.
The curly braces refer to the result of the find command. I only want to do an LS
L on these valves that are owned by user Linda. And as you can see, this result makes
so much more sense based on that. We can do so many powerful things with the exec.
Let me use CP A for instance, with the curly braces to root Linda.
Here we are doing something that's probably not going all right, I want to copy my
valves into a directory with the name root Linda. Guess what? That directory does not exist.
You know what I'm going to do?
I'm using CTRL A to move my cursor to the start of the command line and I'm
creating the directory sudo mkdir root lynda and then I'm using a semicolon. The semicolon is a
command separator. So this is in fact running two commands from one command command line.
And there we get what? Well, we get. Cannot override non directory root
Linda Linda with directory home Linda. I do understand that it's trying to
copy directories as well, but I never told it to do so.
You know what one potentially useful addition is minus type F.
Because the command that we have just seen is because of the fact that that it was trying to copy directories.
I don't want to copy directories. I want it to work on files only.
Type F means that it works on files only.
Now we are getting a couple of the same file. I know because I already did this beforehand.
But it's showing some examples of the kind of powerful things you can do with the find utility.