The locate come in used to search for files and directories is very fast, but has two major drawbacks. 1
It has only a few options and most importantly, it doesn't do real time search of the file system, 2
but of its own database file, which should be constantly updated. 3
And other Linux command used to search for files is find. 4
It's a complex command line utility that searches for files in a diregtory and recursively throw subdirectories 5
as well. 6
Using find, you can search for files by name, owner, group type, permissions , date and other criteria. 7
The syntax of the fine command is find path and criteria. 8
Let's see some examples. 9
I'm creating a new directory with two files, mkdir projects and touch projects 10
report.txt 11
and another file in the same directory todo.txt 12
This is the directory tree. 13
And find the path the directory, it will search in the current directory, by name, so name todo.txt 14
and it has found the file. 15
As anything else in Linux, the name of the file is case sensitive. 16
Another option is I name, which is similar to name, but the match is case insensitive. 17
So instead of name, I'll use iname and the search will be case insensitive. 18
If you want to specify only a part of the filename, find will search by that exact name. 19
So find. 20
the current working directory -name todo 21
And hasn't found anything. 22
The name of the file is todo.txt, but I can search like this. 23
todo and a * 24
It found old files whose names start with the string todo. 25
Star asterisk here means any character, and when you use Sheremeta characters, it's recommended to 26
enclose them in double quotes. 27
So it's better to specify the searched string like this, it is better. 28
Let's try another one. 29
find . -name "*rep*" 30
And it found a few files. 31
All the files contain the string rep in their file names. To find and delete a file pass the -delete 32
option to find. This will delete the file with no possibility of recovering it. 33
So be very careful. 34
find . -name todo.txt -delete 35
And the file was found and deleted. 36
If you search a location, your user is not allowed to see, you'll get the permission denied error: 37
find /etc- name 38
ps passwd 39
In this example, I've searched for the finest named passwd in the etc directory recursively, 40
and there are some directories whose contents are not readable by the unprivileged user that ran the 41
command. 42
To search in those directories as well, I need to run the command as root. 43
Another option you can pass to find is -ls and it will execute the ls command on each file 44
it will find. 45
-ls 46
It executed ls on both files. 47
Let's see how to search files by type and for that 48
there is the-type option. I'm searching for directories in /etc find 49
/etc-type d 50
-type d means directory. 51
In fact, I am listing all directories from /etc and its subdirectories recursively. This is its default 52
behavior which's time and resource consuming. 53
However, you can specify the depth of a directory traversal using - maxdepth option. 54
-maxdepth. 55
This command will search for directories in /etc and will go maximum two levels deep. 56
Let's add another option -perm 57
755 58
I've introduced a new option: -perm which will search by those exact permissions. 59
Let's see how many directories we have found. 60
So| wc -l 61
And there are 254 directories, and if I go three levels deep, I'll have more directories. 62
Now there are 343e directories. 63
We can ignore this permission denied errors. 64
I've already explained to you what they mean. 65
Note that when you specify more options or search criteria, those criteria should be true at the same 66
time. 67
In fact, there's a logical end between them. Let's see other useful options. 68
To search by size, use the -size option and the number find 69
/var 70
-type f -size 100k -ls ; it will search the directories recursively for files with the 71
size of one kilobytes rounded up to the next unit. 72
I'll execute the command as root so sudo. 73
And there are three files! By the way, the size value displayed by ls seems bigger, but that is 74
because the key suffix displays the size in units of 1024 bytes, not in units of 1000 bytes. 75
So it's only a display issue. 76
You can also use the + and - prefixes to signify greater than and less than the size of n units. 77
I'm searching for all files that are greater than 10 megabytes. 78
So +10M 79
And there are a lot of files. 80
or I'm searching for the files that are smaller than 10 kilobytes. 81
-10 K. 82
These are the files. 83
They are smaller than 10 kilobytes. 84
You can also use the size option many times in the same command. Let's find all files in /var between 85
5 and 10 megabytes. 86
So +5M 87
and-size-10m 88
These are the files. 89
They are between 5 and 10 megabytes. 90
Let's go ahead and see how to search by time, acsess time, modification time or change time. To search 91
by timestamps recorded on the files, used the -atime, -mtime or -ctime options and 92
the number that represents the number of days. 93
Let's find all files that were modified in the last 24 hours or a day. 94
find /var by the way, a / at the end 95
doesn't matter 96
So -type f -mtime 0 -ls 97
These are the files modified in the last 24 hours. 98
Maybe you wonder why I've used the zero as value for mtime. 99
That's because any fractional part is ignored. 100
So if a file was modified 12 hours ago in days, it means 0.5 days and the fractional part 101
is ignored. 102
So, in fact, it means 0 days. 103
Let's see 104
another example! I'm writing 1 instead of 0. 105
If I use -mtime1, it means files modify to between 1 and 2 days ago. 106
Another example find /var -type f -atime +1 -ls , it will find the files 107
that were accessed at least two days ago, +1 means greater than 1 and 2 is the next number 108
greater than 1. 109
So the file was accessed at least two days ago. 110
If you want to search by minute instead of day use the -amin 111
mmin or cmin options. For example, the next command will find the files that are modified in the 112
last hour. 113
And I'm searching in var -type f -mmin -60 114
So the modification time is less than 60 Minutes. 115
These are the files. I'm running the command as root. 116
They were modified in the last hour. Let's see other search options: to search by owner use the -user option 117
and to search by group, use the -group option. 118
I’m searching in /var directory for files that belong to the gdm user: 119
-user gdm -ls I'll run all these commands as root. 120
These are the files that belong to this user. 121
And to negate an option, use -not before the option. For example, let's see all files that 122
do not belong to the root group. 123
Is both a user in the group in Linux. 124
The only user that belongs to the root group is root. 125
I'm searching in /etc, I'm searching for files, -not -group root 126
These are the files that do not belong to the root group.