---
title: "Searching files in Linux with the find command"
description: "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..."
url: https://www.stackscale.com/blog/find-command-linux/
date: 2022-04-28
modified: 2023-11-03
author: "Marketing Stackscale"
image: https://www.stackscale.com/wp-content/uploads/2022/04/find-command-linux-stackscale.jpg
categories: ["Systems"]
tags: ["Linux"]
type: post
lang: en
---

# Searching files in Linux with the find command

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](https://www.stackscale.com/blog/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 parameter** | **Usage** |
| --- | --- |
| `-name` | Filtering files by name. |
| `-iname` | Making a case insensitive search by name to locate a file. |
| `.` | Searching in the current directory. |
| `-type` | Filtering files by type of file. |
| `-user` | Filtering files by user. |
| `-mtime` | Filtering files limiting the period of time of its modification. |
| `-maxdepth` | Limiting the search to a given amount of sub-directories. |
| `-L` | Including symbolic links in the search. |
| `-size` | Filtering by file size. |
| `-and` | Placed between two conditions to indicate that it is necessary to include both conditions. |
| `-or` | Placed between two conditions to indicate that it is necessary to filter by at least one of the conditions. |
| `-not` | Placed 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.
