How to search files, folders and texts in Linux

If we are not one to lead an organization when it comes to storing the documents that we create on our computer, finding any file can be a titanic task that takes us a long time, as long as we do not use the different methods available in Linux to find documents, folders and texts.

If we do not want to be forced to resort to the Linux search system, which by the way is very complete, we should begin to consider the possibility of creating a directory structure that allows us to correctly store all the files that we create and/or that reach our hands.

How to search files, folders and texts in Linux

Not only will our minds appreciate it, but also our productivity, since we will avoid wasting time searching continuously. In addition, the task of making backup copies will be reduced to a series of directories and not to the entire system.

Search by document content

The first thing we must do when we are looking for a document that we cannot find is to remember a part of the text that it includes. We only need to know a few words of the text that is included to be able to find it.

via terminal

Finding documents that include certain words and are located in the Softzone directory using the Linux command line is as simple as using the following command:

grep -iRl palabras-que-buscamos /home/softzone

  • The -i parameter will search for text regardless of whether it is uppercase or lowercase text.
  • With the R modifier, a search will be performed in all subdirectories of the location that we set at the end of the line.
  • The variable l will replace the text of the document that matches the search with the name of the file, which is what we are really looking for.

We must replace the final location, in this case /softzone with the location where we want to search for the files.

With the graphical interface

When using the Linux graphical interface to search for part of a text found in a document, the first thing we must do is access the drive where the file is located.

If we only have one unit, we must click on the Files application. Next, click on the magnifying glass located at the top and then on the down arrow that is shown on the left.

Buscar texto en Linux

Click on What and select the type of document we want to search for, which, in this case, would be Documents (the full text option must be selected).

Finally, we enter the text of the file we want to find and wait for the results to be displayed, results that will include the location of the document.

Find the folders you are looking for

All Linux distributions allow us to do folder searches both through the terminal command line and using the graphical interface.

With the command line

Linux, through the find command, allows us to filter the type of results it shows depending on the type of file it is. The results of the find command show both files and folders.

If we want to reduce the number of results to only folders, we must add the variable -type d

find / -type d -iname nombre-del-archivo

From GUI

In the taskbar, we must select the drive where the folder we want to search is located. If we only have one partition in the system, we can search directly from the Files application.

From the Files application window, click on the magnifying glass at the top and enter the words that are included in the name of the folder.

Buscar archivos Linux

Unlike carrying out this process using commands, the interface does not allow us to filter the type of content that will be displayed, so the resulting list will show as many files as there are folders.

Folders are represented by a folder while files show the icon of the application they are associated with, if any.

Locate the file you need

The search for files through the terminal in Linux puts at our disposal a large number of tools and variables to filter the content as much as possible, eliminating other possible matches.

From terminal with find

If we search for files in Linux, we have two tools: find and locate . While the first is available natively, the second must be installed, although the command syntax is simpler.

with find

In order to find a file by its name, it is necessary that the name includes words that help us find it. If you usually save documents as Document1, Document 2… it will be useless to use either the command line or the graphical interface to find the files you are looking for.

The command we are going to use to find files from terminal is find . Using the following command, the system will take care of searching and showing us all the results that match the file name and that are found in the home directory and higher.

find /home -iname nombredelarchivo

If we want the search to be performed throughout the system, the code to use is:

find / -iname nombredelarchivo

To perform the search in a directory, without searching higher levels, we must replace the / with a dot “.”

find . -iname nombredelarchivo

If we only remember part of the document name, we will rely on the use of asterisks “*”.

find .-iname *nombrequerecordamos*

Asterisks can also be used if we do not know the file extension.

The -iname switch forces the system to be case insensitive. If we add -not to the search command, all the files that do not match what we set search for will be shown.

find . -not nombredelarchivo

The find command also allows us to directly find and delete all files with the search name by adding -delete to the end of the command.

find .-iname nombredelarchivo -delete

Using Locate

The Locate command offers us faster results, as long as we are looking for files that we have created and that we have located somewhere in the system. This is because it only searches the file base, not the entire system.

The first thing we need to do is install it using the command

sudo apt-get install mlocate

To perform a search using this command, we will use the following command

locate nombredelarchivo

This command also includes a series of variables that allows us to refine the search results. If we add the variable -r and the symbol $ next to the name of the file, only that specific file will be displayed.

locate -r nombredelarchivo$

To make the command case-insensitive, we add the variable -i

locate -i nombredelarchivo

When the number of results is very high, locate allows us to set the maximum number of results to be displayed with the -n command, followed by the number of files.

locate nombredelarchivo n 10

If the Linux file search results obtained with locate refer to files that have already been deleted, we must update the database using the command

sudo updatedb

Through the user interface

To search for files in Linux using the graphical interface, we must use the file manager to the drive where the file is located, or where we think it should be.

Buscar archivos Linux

Next, click on the magnifying glass located at the top of the window and enter the name of the file. That same window will show us, seconds later, all the files and
folders that match the name we have specified. Next to the name, the location of the file will also be displayed