Linux is a file-centric operating system and finding files and directories is a common administrative 1
task. 2
There are 2 commands used to find them: locate and find. In this video 3
we’ll see how to use locate and then we’ll learn about find. Even though locate is a classical command from 4
the early days of Unix, it’s not by default installed on Ubuntu as it is on CentoOS. To install it on Ubuntu 5
just run apt install mlocate as root. 6
so sudo apt install mlocate 7
By the way mlocate is the new implementation of locate. So at least on the latest versions of 8
Ubuntu when you run locate it actually runs mlocate. Locate is faster than find because it doesn't 9
search in the file system in real time. 10
It uses a previously built database, whereas the find command searches in the real system, through all the 11
actual directories and files. 12
On the other hand, the find command has more options available. The locate command searches by name and 13
returns a list of path names containing that name in their path. 14
The database used by locate should be updated periodically. 15
It can be done automatically on a daily basis from Chron or you do it manually by running updatedb as root. 16
sudo updatedb I'm updating 17
the database used by locate. We will talk about chron jobs later in the course. 18
The database created by updatedb that will be searched by the locate command is /var 19
/lib/mlocate/ and its name is mlocate.db 20
To see its path together with some statistics, like how many files and directories are indexed, 21
run, locate with the capital S option, locate -S 22
The basic usage of the command is located in the name of of the file or directory you want to find: 23
locate passwords 24
And it has returned a list with all path names that contain the passwords string. Pay attention that 25
it searches for the string in the entire absolute path name. 26
For example, if I search for let's say `eahorse` 27
OK, it will find all the files and directories that have that string in their absolute path name, 28
so it searches the whole path name. To search only the base name against the specified pattern, use 29
the -b option. 30
This is the opposite of the default option, which is -w or --wholename. 31
So locate-b 32
eahorse 33
The selected file will not be found anymore. 34
Horse is not in its base name. 35
Now it will find only files and directories that contain the string in their base name. 36
When you search for a name, it will replace that name with star name star implicitly. 37
So if you search for eahorse, it will replace that name with *eahorse* and will also find 38
the files called seahorse. 39
To disable this behaviour and search for a file by the exact name use a \ before 40
the name like this: locate -b 41
‘\’NAME’ 42
Backslash is a globing character that disables the implicit replacement of of NAME by *NAME*. 43
So, of course there is no such a file, but I'll try to find something like this. 44
\eahorse 45
And it didn't find anything because there is no file or directory with that exact name. Let's see 46
another example. 47
locate -b 'shadow' 48
it found all files that have the string shadow in their base name like rainshadow. 49
But \shadow will find only the files with that exact name. 50
Rainshadow is not there anymore. 51
As I said earlier, the locate command does not search for a file in the file system, but in its own 52
database, which should be updated periodically. I'm creating a new file with a unique name. 53
touch myfile123x 54
I suppose this name is unique and I'm searching for it using locate 55
So locate and the name of the file 56
myfile 57
123x 58
And, as expected, the locate command didn't find anything because the new file does not exist in its database. 59
I'm updating the database to reflect the latest changes in the file system, sudo updatedb 60
And I'm searching again for the same file. 61
And this time it found the file; if I remove the file and then search again for it, locate will still 62
find it. 63
I'm removing the file and I'm searching for it again. 64
It was found even if the file was removed. To avoid this false positive result, the finding of a file 65
that does not exist anymore, 66
I can use the -i option or --existing so that it will check if the file really exists. 67
locate -e myfile 68
123x 69
And nothing was found. Note that when you update the mlocate database, it keeps timestamp information 70
in the database. 71
This allows mlocate to know if the contents of a directory changed without reading the contents 72
again and makes updates to the database faster. 73
Let's go ahead! 74
Another useful option is is -i which will make locate ignore the case. 75
Linux is a case sensitive OS and locate makes no distinction. 76
So locate rainshadow 77
Nothing was found but locate -i rainshadow 78
We'll find a few files. 79
It has ignored the case; and the last option will discuss is -r, which comes from rejects. 80
It allows you to search by using simple regular expressions. 81
This is an advanced topic and we'll go deeper into it later in the course. For the moment 82
you should know that the regular expression or rejects is a way of defining complex search patterns. 83
Let me show you one example. 84
locate -b 85
-r 86
'^ 87
shadow 88
\.[0-9]' This is a regex or a regular expression. 89
I'm searching for all the files and directories whose base name start with the shadow string 90
than the dot sign and and any digit between 0 and 9. Any other characters can follow. 91
And these are the files. At the end I want to tell you a few words about another small command called 92
which. The “which” command locates a command and returns the absolute path of the executable that is called 93
when the command is run. 94
It does this by searching the path for executable files. This can be useful in finding the location 95
of an executable command when you intend to create a shortcut or run it using its absolute path. 96
For example, typing which rm displays the path to the rm command which ls; by default 97
it only displays the first matching executable and if you want to display all matching executables, 98
if they are more, use the -a the option with the command. 99
So which -a find. 100
ls-l/bin/find 101
ls-ld/bin In fact /bin is a symbolic link to /usr/bin 102
We'll have an entire lecture on symlinks later in this section. 103
To search for more executables at once all you have to do is to give the executables as arguments 104
to the which command: which pwd ifconfig find grep firefox