In this lesson,
we're going to discuss the tools that you can use
to search for files or applications
when you don't know their exact location on a Linux system.
The first one is the locate command.
The locate command performs a quick search
for any specified string, inside file names,
and path stored inside the mlocate database.
Now this database must be updated regularly
for the search to be really effective.
The results displayed may be restricted to files
that the user has permissions to access or execute, though,
so keep that in mind.
To use this, just type in locate,
the options, and the string you want to search for.
The locate command supports different options
that enable you to make your searches more effective.
For example, you can use the -r option to search
for file names using regular expressions.
If you use the -c option,
you can display only the number of matching entries found
instead of showing the actual file names.
So for example, if I wanted to find all the files
that had the word JSON in them,
I can use -c and get a result that says there are 543 files
with the name JSON in them.
Also, you can use -e.
This is going to return only files that exist
at the time of the search
and not things that were previously deleted.
If you use -i,
it's going to be used to ignore the casing
in a file name or path.
And if you use --n and the number of entries,
it's going to be used to return only the first few matches
up to the specified number.
So for the example of JSON,
I said there was 500 and something files,
but I only want to show the first three,
I could use --n 3
and only see the first three that are found.
If you want to update that database,
you do this by using the updatedb command.
The updatedb command is used to build a database
of files based on the /etc/updatedb.conf file.
This command is used to update
the /var/lib/mlocate/mlocate.db database file.
The /etc/update database config file consists
of the path that should be excluded
when building up your database.
To add a path that needs to be excluded
when building the database,
simply open up the /etc/updatedb.conf file,
and in the PRUNEPATH variable,
specify which paths need to not be included
when you're building this database.
For example, if I use PRUNEPATH="/etc",
this would exclude the etc directory
when building my database.
Another command you can use to search is known
as the find command.
The find command enables you to search a specific location
for files and directories that adhere
to some specific search criteria.
It's going to be used recursively to do searching
throughout the directory structure,
including any subdirectories and their contents,
beginning with the search location that you enter.
You can perform one or more actions
on the files that are found.
If you use the -type option,
this enables you to specify
the type of object you're looking for,
such as d for directory or f for file.
If you use the -name option, this is where you specify
the name of the object you're looking for.
So you might enter something like find, the options,
the search locations, the search criteria,
and the actions to be able to run this command.
For example, let's say you want to search
for a user's home directory
for all the files named 2019_report.
I can do that by typing
in find /home/user -type f -name 2019_report.
Now I'm going to be able to find all those files
that are called 2019 report
underneath that home user's directory.
Now you may be wondering what's the difference
between using the locate command and the find command?
Well, in general,
the locate command will give you an answer much more quickly
because it's searching a database,
but it's only going to retrieve information
on files that are present on your system in that database.
Now, if you don't keep that database updated,
you're going to get outdated results,
and those files may no longer exist.
On the other hand,
the find command is going to perform a live search
of your file system,
and it can be concentrated on a specific location.
So the find command may take more time to complete a search
than your locate command.
But when the system finds a listing
that meets your criteria,
those things are really on your system
if you're using the find command,
and then you can take actions on those results.
For example, I might use -print.
This would display the location
of those files that were found.
I can type -exec which will execute the command
that follows that prompt on those files.
I can type -ok which executes the command
that follows interactively.
I might use something like -delete
which will delete those files that I found.
Or I can use the option -fprint
which stores the results in a target file.
Now, in some cases, you may need help to locate
where a program has already been installed on your system,
for example, if you want to modify it
or identify which version of a command you're using
if there's multiple binaries of that command stored
in different locations on your system.
And to do that, you can use the which command.
The which command is going to display the complete path
of a specified command
by searching the directories assigned to the PATH variable.
For example, if I entered which cat,
I'm going to get the output that says /bin/cat.
This way, I know where the cat command is actually located,
in this case, the /bin directory.
If you want to use this, just type in the word which,
the options you want,
and the program name you want to search for.
Now another command you can use is known as whereis.
The whereis command is used
to display various details associated with the command.
For example, if I entered whereis ls and I hit Enter,
I'm going to get something that looks like this.
It says ls: /bin/ls
/usr/share/man/man1/ls.1.gz
/usr/share/man/man1p/ls.1p.gz,
where /bin/ls indicates the location of the ls command,
in this case, bin/ls.
The /usr/share/man/man1/ls.1.gz
and /usr/share/man
/man1p/ls.1p.gz indicates the location
of the man pages for the ls command.
Now the proper way to run
the whereis command is to type whereis,
the options, and the directory name, and the file name.
Now the whereis command has
several different options you can choose from.
For example, you can use -b to search only for binaries,
m to search only for manual sections,
s to search only for sources,
and -o to search for unusual entries.
So as you can see, there are many different ways
to search for information on your systems,
including the locate command, the find command,
the which command, and the whereis command.