Locate and find, search for files from Ubuntu terminal

about locate and find commands

In the next article we are going to take a look at the locate and find commands. Nowadays, in Gnu / Linux we can find many ways with which search and locate files and directories. From the graphical environment we will have different tools with which to obtain results efficiently.

But in this case, if you are a terminal user, you may not want to have to leave the comfort of it to be able to locate what you are looking for. This is why, especially if you just got to Gnu / Linux, you may not know yet the fastest and easiest way to find your files and folders from the terminal. In the following lines we are going to see a few quick examples with which to carry out this task.

Search files from terminal with locate and find

Locate command

El locate command is possibly the first resource a user should use because it is much faster than any other option. The reason for this speed is that this command is not really searching our local hard drive for the files or directories that we need to find. This search is performed as reads through the database file mlocate.db, which contains all the file paths on our system.

In case you do not have this tool available on your Ubuntu system, you will be able to install it from a terminal (Ctrl + Alt + T) by typing in it:

sudo apt install locate

Preparing the locate command

Before we begin we must prepare the locate command for the first use. We must update the mlocate.db database running in terminal (Ctrl + Alt + T):

sudo updatedb

Now to use locate, from the terminal we will only have to write the command followed by the name of the file we are looking for. In this example, I am looking for files that contain the word 'ubunlog' in her name:

locate ubunlog

locate ubunlog

Since locate reads a database file, the results may be out of date. This we can fix updating your file path database, as we did with the first command we used before searching.

For more information about this tool, we can turn to help typing in the terminal:

help locate command

locate -h

Or we can also use man pages:

man locate

man locate

Find command

about F.D.
Related article:
Fd, an alternative to the find command, simple, fast and easy to use

El find it is a much more powerful but also slower search utility. This is because it actually searches our drives for files and directories. It's perfect for when you try locate a file or directory but can't remember its exact name.

Find can search for files that belong to a certain user or group of users, files that were modified or recently accessed, files of a specific size range, hidden files, etc.

If possible, the first thing we should do when executing it is instruct it to search in a specific directory. This will speed up the search process, depending on the size of the directory. If you know where the file can be, open the terminal (Ctrl + Alt + T) and go to the directory to run:

find . [nombre-archivo]

The dot tells find to search the current directory. If you want to search your home directory, replace the period with '~/'. If you want to search your entire file system, use '/'.

Examples

Let's say we want to search for a file in the documents directory. We know that the file name contains the word 'php', but we don't exactly remember the name. The first thing to do is navigate to the documents folder and then type the command:

find name

find . -name "*php*"

This will tell us in this case that there is a pdf file called 'poo-php'in the Documents folder. Now, if we replace the parameter '-name' with '-iname', it could get results without taking into account the exact letters. That is something that we will not find in locate.

find command with iname

find . -iname "*php*"

If all we remember is that file is less than 5MB in size. The command to use would be:

find . -size -5M

In case you know that weighs more than 3MB, the command to use would be:

find . -size +3M

Find supports Boolean operators to make the search more exact. For this example, I will use a command that combines the above data. That the file is less than 5 MB in size and more than 3:

sudo find / -size -5M -and -size +3M

In case all we know is that we accessed the file five minutes ago, the command to use will be:

sudo find ~/ -amin -5

For learn more about find, open a terminal and type:

you find

man find

These are just a few basic examples of how to locate files from terminal. For more information, be sure to check out the man pages for each of them.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.