Searching files in Linux with the find command

Searching files in Linux from the command line

Searching files in Linux from the command line is easy using the find command. This Linux command uses a simple conditional mechanism to filter objects. Let’s see how to use the find command in Linux to locate a file from the command line.

Linux find command

The find command is used to locate files in Linux, based on user-specified criteria. The basic syntax of the command is as follows:

find -options /path expression

Find command attributes

  • -options: options or search parameters enable control of the behavior and optimization method of the find process.
  • /path: it defines the top-level directory where the find command will start filtering.
  • expression: it defines the actions to perform to create an output.

Find command options

Locating files by name in Linux

For finding files by name in a directory and all sub-directories, type the following command into the command line:

find /path -name filename

Locating files by extension

For finding files by extension in a directory and all sub-directories, type the following command into the command line:

find /path -name *.html

Locating files in the current directory

For finding files in the current directory, type the following command into the command line:

find . -name filename.html

Locating files modified during a period of time

For finding files that have been changed during the previous week, type the following command into the command line:

find /path -mtime 7 “.html”

Locating files modified during a period of time by a particular user

For finding files that have been changed during the previous week by a particular user, type the following command into the command line:

find /path -user username -mtime 7 “.html”

Limiting the search to a specific number of sub-directories

For limiting the search to a maximum amount of sub-directories, add the -maxdepth X option to the command:

find -maxdepth 3 /path -name *.html

Ignoring text case

For making a case insensitive search, add the -iname option to the command:

find /path -iname *.html

Searching files

For exclusively searching files, add the -type f option to the command:

find /path -type f -name “*.html”

Searching directories

For exclusively searching directories, add the -type d option to the command:

find /path -type d -name “*.html”

Following and showing symbolic links

Since the find command ignores symbolic links by default, add the -L option to the command to follow and show them:

find -L /path -name *.html

Locating files by file size

For finding bigger than 500MB files in a directory and all sub-directories, type the following command into the command line:

find /path -size +500M

Combining two conditions or filters

For instance, for filtering files ranging between 500MB and 1GB in a directory and all sub-directories, type the following command into the command line:

find /path -size +500M -and -size -1G

Filtering by at least one of the specified conditions

For instance, for filtering files by one of the indicated extensions in a directory and all sub-directories, type the following command into the command line:

find /path -name *.docx -or -name *.odt

Excluding the second condition

For instance, for excluding an extension from a search in a directory and all sub-directories, type the following command into the command line:

find /path -name *.docx -not -name *.odt
Option or search parameterUsage
-nameFiltering files by name.
-inameMaking a case insensitive search by name to locate a file.
.Searching in the current directory.
-typeFiltering files by type of file.
-userFiltering files by user.
-mtimeFiltering files limiting the period of time of its modification.
-maxdepthLimiting the search to a given amount of sub-directories.
-LIncluding symbolic links in the search.
-sizeFiltering by file size.
-andPlaced between two conditions to indicate that it is necessary to include both conditions.
-orPlaced between two conditions to indicate that it is necessary to filter by at least one of the conditions.
-notPlaced between two conditions to indicate that it is necessary to ignore the second condition. 

Find command optimizations

There are three stages of optimization for enhancing filtering when using the find Linux command.

-O1: default

-O1 is the standard setting. It enables find to start filtering by filename before running any other test.

find -O1 /path -name "*.html"

-O2: filename and type of file

-O2 enables find to filter by filename and type of file before running more demanding filters.

find -O2 /path -name "*.html"

-O3: efficiency and likelihood of success

O3 enables find to automatically reorder filters to prioritize efficiency and likelihood of success.

find -O3 /path -name "*.html"

Searches based on content: Find + Grep

The find Linux command only filters files based on filenames and metadata. Therefore, in order to search files based on their content, it is necessary to add the grep command to the command line:

find /path -type f -exec grep “content” ‘{}’ \; -print
  • -exec: it is a command that enables the find command to execute a given task once per matched file.
  • ‘{}’: curly braces are the placeholder for the matched results.
  • \;: the -exec option is closed by an escape and a semicolon to avoid it can be interpreted by the shell.
  • -print: it prints the results on the screen.

Share it on Social Media!

Managed services

System administration and IT outsourcing adapted to the needs of each project.

DISCOVER MORE
Cookies customization
Stackscale, Grupo Aire logo

By allowing cookies, you voluntarily agree to the processing of your data. This also includes, for a limited period of time, your consent in accordance with the Article 49 (1) (a) GDPR in regard to the processing of data outside the EEA, for instead, in the USA. In these countries, despite the careful selection and obligation of service providers, the European high level of data protection cannot be guaranteed.

In case of the data being transferred to the USA, there is, for instance, the risk of USA authorities processing that data for control and supervision purposes without having effective legal resources available or without being able to enforce all the rights of the interested party. You can revoke your consent at any moment.

Necessary Cookies

Necessary cookies help make a web page usable by activating basic functions such as the page navigation and the access to secure areas in the web page. The web page will not be able to work properly without these cookies. We inform you about the possibility to set up your browser in order to block or alert about these cookies, however, it is possible that certain areas of the web page do not work. These cookies do not store any personal data.

- moove_gdpr_popup

 

Analytical cookies

Analytical cookies allow its Editor to track and analyze the websites’ users behavior. The information collected through this type of cookie is used for measuring the activity on websites, applications or platforms, as well as for building user navigation profiles for said websites, application or platform, in order to implement improvements based on the analysis of data on the usage of the service by users.

Google Analytics: It registers a single identification used to generate statistical data about how the visitor uses the website. The data generated by the cookie about the usage of this website is generally transferred to a Google server in the USA and stored there by Google LLC, 1600 Amphitheatre Parkway Mountain View, CA 94043, USA.

- _dc_gtm_UA-XXXXXXXX-X

- _gat_gtag_UA_XXXXXXXX_X

- _ga

- _gcl_au

- _gid