Now, let's look at how to search for files in Linux.
After you get a little bit familiar with the Linux, OS,
you learn that files are very nicely organized.
If you want to configure your SSH daemon, you know,
you'll find the relevant config files in slash Etsy slash SSH.
You need to find logd,
you can go to slash bar slash log
most of the time you'll know where everything is at least approximately.
So why would you need to search for files?
Well, let's look at some typical scenarios.
Imagine you have a website
and you want to find all of your image files.
If your website's directory would be slash user slash share,
you could quickly get a list of all.jp G files with a command like this.
Now, in a different scenario, you're almost running out of disk space,
this servers hosting virtual machines.
And you notice that most of the virtual machines require files under 20 gig.
You figure out that you can search for files that are
larger than 20 gig to filter out the abnormally large ones.
Now, we don't have such large files available,
but here's how you would look for files larger than 10 megabytes
or let's say you've just updated an application and
you're curious to see what files were changed.
You can quickly look at all the files that have been
modified in the last minute with a command like this.
Now, of course, this applies to many other scenarios
like you could use a similar command to see what
configuration files your administration team changed in the last hour.
Now, from these examples, it's clear that the command to search for files is fine.
Let's take a look at the syntax we'll use throughout this lesson.
For example, to find a file named file one dot text in the directory slash bin,
run the command, find
slash bin,
dash name, file one dot text.
Now
name is a search parameter used to specify the name of the file you're looking for.
You can sometimes skip specifying the path
to the directory you want to search though
and when you do that, it searches in the current directory.
Now the first few times you use this command,
it may happen that you mix up the directory path with the search parameters,
meaning instead of writing find slash bin, dash name, file one dot text,
you may write, find dash name, file one dot text slash bin.
If you find yourself falling into this trap, just think about it this way.
First, I have to go there, then I will find it.
You have to enter your room and only after that, you can search for your keys.
This will remind you that you first have to
specify the search location and then the search parameters.
Now, with this basic knowledge, out of the way,
let's focus on what makes the real magic happen.
Let's look at some other parameters.
Now, we just saw the name parameter being used already.
It's used to find files with a specific name.
In this case, Felix.
This however is case sensitive,
meaning it won't find a file named Felix with a capital F.
If you'd like to find the command to not be case sensitive or case insensitive
add an I in front of the option to make it I name
at times, you may want to find multiple files that have a pattern in their names.
For example, I want to find all files that start with a lowercase F for this,
I use a wildcard expression
which is a starting expression followed by a star.
The star is like a joker card for text.
It will match anything even if it's zero characters or 100.
In this case, it matches all the names starting with F.
We already saw in the examples.
A command that looks for files modified in the last minute
to remember dash Mmin. Think about modified minute.
But let's try to understand what minus one means here as we can use
things such as one minus one and plus one and they mean different things
Now how find works with time stamps is not exactly intuitive.
So let's make it easier to understand. First, let's imagine the time is 1205
with dash mmin five. We'd see files created exactly at 1201
and not 12 o'clock as we'd expect.
That's because when we backtrack from the current time, we step through 1205,
1204, 1203, 1202 and end up at 1201.
Exactly five values.
Now with dash Mmin dash five, we'd find files modified in the last five minutes.
That means files were modified at 1205, 1204, 1203, 1202 and 1201.
Now, 12 o'clock was not included for the same reason as before from 1205 to 1201.
We have exactly five values
to 12 o'clock. It would be six values. So we'd need to bump up the dash mmin
to dash six
to also include that timestamp.
Now with dash Mmin plus five,
we'd see files created from 12 o'clock to 1159 1158 and beyond,
you might notice there's a twist this time,
we see 12 o'clock here instead of 1201.
When you use a plus sign, the count skips the current time.
So it starts counting from 1204 instead of 1205.
And we have five minutes from 1204 to 12 o'clock.
So another way to think about this find dash mmin plus five command is
that it shows us any object that was created more than five minutes ago
and 1201 is not more than five minutes ago, but rather exactly five minutes ago.
So more than five minutes is 12 o'clock or earlier.
Another similar option is MTIME,
but instead of working with minute units, it works with 24 hour units.
Dash M times zero matches what was modified in the last 24 hours.
Dash mtime. One matches what was modified between 24 and 48 hours ago
and so on.
It's worth noting that modification might mean
either when the file was last edited,
but also when it was created,
if we edit a text file at 1205,
it will show up as modified at 1205.
But if we create a new file at 1206,
this will also count as a modification time.
So it will show up as modified at 1206 even though that's when it was created.
Linux also has a thing called change time for files,
which might sound like the same thing as modify time, but it's actually different.
Modify time refers to the time when the contents have been modified.
Change time refers to the time when the metadata was changed.
Now, metadata is data about data.
So in this case, data about your file,
this might mean something like file permissions or other similar information.
And this is where change time could be useful.
Imagine you suddenly get errors in some application
and you suspect it's because someone changed
some file permissions in the wrong way.
You could find files with permissions changed in the
last five minutes with a command like this.
In our initial examples, we use dash size to search for files based on their size
to find files of a size.
Exactly 512 KB,
we can run the five command with the size
parameter set to 512 KK stands for kilobytes.
Here's a quick table showing the different values. Now, C stands for bytes,
K for kilobytes,
M for megabytes and G for gigabytes
note that the M and G must be capital letters
to search for files greater than 512 KB. We can use plus 512 K
and for files less than 512 KB, we can use minus 512 KB.
We saw how we can find all files or directories that start with
the letter F with a command like find dash size F star.
We also learned you could find files of a certain size with a command like find
dash size 512 K.
Now size 512 K and name F are search parameters.
But in the command's manual, we'll also see that the part where we tell,
find what to look for is called a search expression.
That's because just like in mathematics,
we can combine multiple parameters and build a more complex search expression.
For example,
assume we want to find files that start with the letter
F but also have a size of exactly 512 K.
We can combine our previous two parameters to build the search expression.
And to do that, we simply enumerate both of them.
When we write it like this,
there's an implied and operator between the dash
name parameter and the dash size parameter.
This means that both conditions must be true for the files to show up in the results.
But what if we want an or operator between these parameters,
files or directories that start with the letter F but
also files that have a size of exactly 512 K,
not necessarily meeting both conditions at the same time.
All we need to do is add a dash o where we want our or operator to be.
So we'd end up with this command.
Now,
another interesting thing we can insert into a find expression is the not operator
to make it easier to understand.
Let's look at another example,
say we want to find all files that do not begin with the letter F to exclude files,
beginning with the letter F from our results.
We would use the dash not option before the dash name option.
This would return a list of file names that do not begin with the letter F.
Another way to write the knot operator is to use the exclamation sign.
One important note. However, for a command interpreter, bash,
the exclamation sign has a special meaning.
And when it sees that in a command, it will interpret it a certain way.
Thinking we want to perform special action in that spot
to tell bash to ignore the special meaning of
this character and just consider it a regular character.
We can do what's called escaping it
to escape it. We just add a backslash in front of the exclamation sign.
We can also search for files or directories based on their permissions.
We'll use the 664 octal notation for our permissions.
664 means this set of permissions.
The user can read or write, the group can read and write
and others can read
to search for files based on their permissions. We can use commands like these
and this will look for files which have exactly these permissions.
Now, if we put a dash before that 664,
it will look for files that have at least these permissions,
which means that even if the file has some extra permissions set,
it will still show up in the search results.
But if it has less than these permissions, it won't show up.
For example, 664 denotes that a user should have read and write permissions.
If they only have read permissions, but no right,
then find will not show this in the search result.
Think of it as a bare minimum permissions are these.
Now,
we can use a front slash 664 to look for files which have any of these permissions.
Unlike the bare minimum condition above this one's more inclusive.
For example, if a user can read the file but cannot write to it,
it will still show up in search results as one permission has been matched U equals R.
So it doesn't matter if the other permissions exist or don't exist.
Now, here's an alternative way to write each of these.
Now imagine we have a group of files.
We want to find files which only the
owner can read and write with no other permissions
to do that. We would run find dash perm 600.
Now this would match the files,
Felix James and Bob to find files that the owner can execute at least.
But the rest of the permissions can be anything.
We would run find dash perm dash 100
which would match only Freya and Jacob.
Now imagine you want to make sure that nobody else can
read these files except the users and groups that own them.
In
this case, we would use the not operator
to look for files that others cannot read. We would run this command
which matches Felix James, Bob, Freya, John and Bean.
Finally imagine that we want to find files that can only be read either by the user,
the group or others.
It doesn't matter who it is, but at least one of them should be able to read it
to do this. We would write a command like this. In
this case, all our files match the condition.
If no one can read it,
it won't show up in the results.