How To Search & Find Files with Commands on Linux

In this tutorial, we explain the different commands that you must use in Linux to search and find different files.

For Linux distros, a command called find has been developed which has been designed to recursively filter objects in the file system based on a conditional mechanism.

With this find command, we can find a file or directory in your file system.

If we include the -exec parameter, the files can be found and processed immediately within the same command.

Find Files By Name Or Extension Commands

We can use find from the command line to locate a specific file by name or extension.

Its syntax is:

find /home/username/ -name "*.extension"

For example, we will search for files ending in .png:

Practical Ways To Use Find Command

With the find command, we have some options of parameters like:

find options start / path expression

The options attribute will control the behavior and optimization method of the search process. The start /path attribute will define the top-level directory where find will start filtering.

The expression attribute controls the tests that they search in the directory hierarchy to produce output.
For example, we can execute the following:

find -O3 -L /var/www/ -name "*.html"

This command enables the maximum level of optimization (-O3) and allows you to monitor the symbolic links (-L) to search the complete directory tree in the path /var/www/ for the files that end with .html.

Find Command Settings & Optimization

The default configuration of Find to search will ignore symbolic links (direct access files). If we want find to follow and take symbolic links, we can add the -L option to the command as we saw earlier. Find has three (3) optimization levels that are -O1, -O2 and -O3.

The optimization -O1 is the default and forces search to filter based on file name before executing all other tests.

Optimization at the -O2 level gives priority to file name filters, such as -O1, and then executes all file type filtering before continuing with other more resource-intensive conditions.

The optimization of level -O3 allows find to perform a deeper optimization and reorders all the tests based on their relative expenditure and the probability of better results.

Some of the options are:

It is a filter based on the first file name

-O1 (Default)

Use the file name first and then the file type


Reorder results:

Allows search to reorder automatically search based on the efficient use of resources and the likelihood of better results.


Search in the current directory, as well as all subdirectories to X depth levels

-maxdepth X

Search without taking into account the text case


Returns only the results that do not match the test case


Make a file search

-type f

Search directories

-type d

Find Files by Modification Date Commands

The find command has the ability to filter a directory hierarchy based on the last modification of the file.
The syntaxes are:

find / -name "*conf" -mtime 8
find /home/user/ -name "*conf" -mtime 2

The first command will display a list of all the files in the entire file system that end with the conf characters and have been modified in the last 8 days. The second command filters the user's home directory for files with names that end with conf characters and have been modified within the previous 2 days.

Use Grep to Find Content-based Files Commands

The find command can only filter the directory hierarchy based on the name of a file and the metadata, so if it is necessary to search based on the contents of the file, use a tool such as grep.

We will use the following syntax:

find . -type f -exec grep "ejemplo" '{}' \; -print

This searches for all the objects in the current directory hierarchy (.) Which is a file (-type f) and then executes the “example” grep command for each file that meets the conditions. Matching files are printed on the screen (-print). The braces ({}) are a placeholder for the results of the search result. The {} is enclosed in single quotes (‘) to avoid delivering grep a malformed file name. The -exec command is terminated with a semicolon “(;)”, which must be escaped “(\;)” to avoid interpretation by the command interpreter.

How to Find & Process Files using Command Find

We can use the -exec option to execute commands against all the objects that match the search expression.

Its syntax is:

find . -name "rc.conf" -exec chmod o+r '{}' \;

This will filter all the objects in the current hierarchy (.) For the files named rc.conf and execute the chmod or + r command to modify the permissions of the search results.

Thus find is an ally for the complete search of files in Linux.

Similar Posts

Leave a Reply

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