Find any document, folder or file in Linux

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

grep -iRl search-words /home/softzone

  • parameter -Yo will search for the text regardless of whether it is uppercase or lowercase text.
  • with the modifier R.all subdirectories of the location we set at the end of the line will be searched.
  • The variable he will replace the document text that matches the search with the file name, which is what we’re actually 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 the 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 downward arrow that is shown on the left.

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 search for folders 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 file-name

From GUI

On 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 perform the search directly from the Files application.

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

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 folders.

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

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.

See also  Do you spend a lot of time in front of the computer? These extensions remind you when you should take a break

From terminal with find and locate

If we search for files in Linux, we have two tools: find and location. While the former is available natively, the latter has to 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 include words that help us find it. If you usually save documents as Document1, Document 2… it will be of no use to us to use neither the command line nor the graphical interface to find the files you are looking for.

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

find /home -iname filename

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

find / -iname filename

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

find . -iname filename

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

find .-iname *rememberedname*

We can also use asterisks if we do not know the file extension.

The modifier -inname forces the system to be case insensitive. if we add -not to the search command, all files that do not match what you set search will be displayed.

find . -not filename

We can also use the parameter -and to search for the file or folder that meets the two search criteria we have specified. Another option is to filter the results that at least meet one of the two search criteria using the parameter -or.

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

find .-iname filename -delete

Other parameters that we can also use to filter the searches, an ideal option for when the number of files to search for is very high, is to use the following parameters:

  • -perm. It will allow us to filter the files depending on the rights you have.
  • -atime. This parameter invites us to filter by days the last access that the file has had.
  • -ctime. It is measured in days and allows us to filter the results by the number of days since its creation.
  • -mtime. Filters the results based on the number of minutes since it was last accessed.
  • -user. It will show the results based on its owner.
  • -group. This parameter will show the files according to the group.
  • -type. Ideal parameter to filter by file types. The file types in Linux are F for file and d for directory. If we want to search for files, the parameter to use will be -type f while to search for folders it will be -type d. We can also search for links, whose file type is he.
  • -size. This parameter is especially useful when we are looking for files that occupy a certain space, so it is ideal for looking for files that we can delete from the computer. We must accompany the parameter along with the file size we are looking for (does not work for folders). If, for example, we are looking for a file that occupies more than 1 GB, the command will be -size 1G. If we search for a file between 1 GB and 2 GB, thus reducing the number of possible results, the parameter to use together with the find command will be -size +1G -and -size -2G

We can also improve the results offered by this command by optimizing its effectiveness. To do so, we have three different options.

  • -o1. The -o2 parameter will start filtering the search results by name. This parameter is placed right after the find command.
  • -02. With this parameter, find will first display the file name and its type. Like the previous one, it is used right after the find command.
  • -o3. This parameter will tell the find command to display the results more efficiently.
See also  4 free Artificial Intelligences that work better than ChatGPT with GPT-4

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 must 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 filename

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

locate -r filename$

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

locate -i filename

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 filename 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 find files in Linux using the graphical interface, we must use the file manager on the drive where the file is located, or where we think it should be. If we want to expand the number of directories to search, we must start the search at the root directory. In this way, the Windows graphical interface will expand the search range to the entire system and we will have more chances of finding it.

See also  Artificial Intelligences to create personalized avatars

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 file location. Once we have found the file, with the right mouse button we can access the location to move the file to another directory where we can have more control over the file we were looking for so as not to lose it again.

How the Linux file index works

Windows creates an index of all the files that are stored on the same unit where the operating system is located and we can extend its scope to the rest of the connected units. Linux also has its own file index, just like macOS.

On Linux, this file index is called an inode. The inode is a record of all the files and directories stored on the operating system drive. Each of these records includes, in addition to the location, the type of file and its size, the ID of the owner and the group (if applicable), the write and read permissions along with the modification, access and edition data.

Each record, that is, each file is associated with a unique inode number. If we duplicate the file, a new unique inode number will be created, just like if we create an identical record in a database, since it will have a different record number in order to identify it.

The maximum number of records that the Windows inode is capable of managing is 4.3 billion, a number that it is very unlikely that we can reach at home or even at a professional level, so we should not worry about its limit. If we want Linux searches to be as fast as possible, we must try to periodically clean up the files we use in order to free up records in the inode and make searches faster and easier.

How to improve search on Linux

Linux allows us to carry out searches using both the graphical interface and the command line as we have shown you above. There really isn’t any way to improve search on Linux, but what if we can…

Loading Facebook Comments ...
Loading Disqus Comments ...