# How to use the Linux find command

[Linux](https://www.ionos.ca/digitalguide/server/know-how/linux-the-cost-effective-alternative-to-windows/ "Linux – the cost-effective alternative to Windows") is an open-source operating system that was created as a free alternative to UNIX. As with UNIX, the command line is a **fundamental tool to work in Linux**. Here, the user enters a command on the command line which is then executed.

To find a file in Linux, you can use the Linux find command. This starts a **recursive search**, where a directory hierarchy is searched following certain criteria. The Linux find command is a precise tool for finding files and directories and is supported across pretty much all Linux distributions.

## An overview of the Linux find command

To use the find command in Linux, you’ll need to open the **command line**. Let’s have a look at the general structure of the Linux find command:

```none
find <directory_path> <search_parameter>
```

---

### Note

Attention: Be careful with commands on the command line. If you carelessly execute the wrong command, you may damage your system!

---

First, the command itself is written, followed by a directory path, and a variable number of search parameters. A search parameter consists of a hyphen that is immediately followed by the name of the parameter. This is followed by a space and the value of the parameter. Below, you’ll find an overview of the most commonly used search parameters:

<table>
  <tbody>
    <tr>
      <td><strong>Search parameter</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>-name, -iname</td>
      <td>Filter by file name</td>
    </tr>
    <tr>
      <td>-type</td>
      <td>Filter by file type</td>
    </tr>
    <tr>
      <td>-size, -empty</td>
      <td>Filter by file size</td>
    </tr>
    <tr>
      <td>-ctime, -mtime, -atime</td>
      <td>Filter by time stamp</td>
    </tr>
    <tr>
      <td>-user, -group</td>
      <td>Filter by owner and group</td>
    </tr>
    <tr>
      <td>-perm</td>
      <td>Filter by file rights</td>
    </tr>
  </tbody>
</table>

**Several search parameters can also be combined**. Here, a logical AND operation is implicitly assumed. This can be written out explicitly. Furthermore, an OR link can be used or a condition can be negated:

<table>
  <tbody>
    <tr>
      <td><strong>Search parameter</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>-and</td>
      <td>Search results must meet both conditions</td>
    </tr>
    <tr>
      <td>-or</td>
      <td>Search results must meet at least one of the two conditions</td>
    </tr>
    <tr>
      <td>-not</td>
      <td>Negate subsequent condition</td>
    </tr>
  </tbody>
</table>

Note that you must **replace the &lt;placeholders&gt; with actual values** in the following code examples to run the examples.

### Limiting your search to a specific directory

Let’s see how we can limit the search to a specific directory. To **search the current directory**, we use the “.” item as the directory path:

```none
find . <search_parameter>
```

To **search through your own user folder**, use the tilde “~” as the directory path:

```none
find ~ <search_parameter>
```

You can also search the entire system with the Linux find command. However, due to the large number of files and the possibly deep hierarchy of directories, this can take a long time. To **search the entire system**, we use a forward slash “/” as the directory path:

```none
find / <search_parameter>
```

---

### Note

Attention: Be extremely careful when applying the Linux find command in combination with the “-exec” parameter to the entire system!

---

### Customize search results output

The search results of the Linux find command may be extensive. That’s why it can be useful to **influence how results are displayed**. Here, it’s important not to use the command’s own parameters, but make use of so-called pipes. In Linux, they’re useful to use the output of one command as the input of another command.

To **output the results page by page**, we pass the output of the find command to the less command:

```none
find <directory_path> <search_parameter> | less
```

To **count the number of results**, we pass the output of the find command to the wc command with the “-l” parameter:

```none
find <directory_path> <search_parameter> | wc -l
```

To look at **only the first or last *n* search results**, we pass the output of the find command to the “head” and “tail” commands. In each case, we specify the parameter “-n” followed by the desired number of search results:

```none
find <directory_path> <search_parameter> | head -n <amount>
```

```none
find < directory_path> <search_parameter> | tail -n <amount>
```

## Finding files with Linux: simple use cases

The following examples **limit the search to the current directory and any subdirectories**. Use the cd command on the command line to change to any directory. To test these examples, first change to your user folder:

```none
cd ~
```

### Using the Linux file command to filter by file name

To filter for file names, use the “-name” parameter. This requires an **exact file name and is case sensitive**:

```none
find . -name <File_Name>
```

Here we are looking for a file with the exact file name “.gitignore”.

```none
find . -name .gitignore
```

To **not differentiate between lowercase and uppercase letters** we use the “-iname” parameter. The “I” here stands for “insensitive”, from “case-insensitive”:

```none
find . -iname <file_name>
```

Usually, it’s more **practical to search case-insensitive first** and use the “-name” parameter only if the search with “-iname” returns too many results.

If we don’t want to search for an exact file name, but want to use a pattern, we use the asterisk as a **“wildcard” placeholder** and write the search pattern in quotes (the asterisk is internally interpreted as “zero until additional characters have been added”). In our example, we search for files and directories whose names contain the text “git”:

```none
find . -iname "*git*"
```

### Using the Linux file command to filter by file type

A famous aspect of UNIX philosophy is the principle that “everything is a file” – and the same applies under Linux. The term “file” refers to files in a broader sense. In other words, directories are also mapped as files under Linux. But to avoid confusion, the more precise term “file descriptor” is sometimes used.

When we speak of the “file type” under Linux, we’re not talking about whether a document is an Excel file or a JPEG image. Instead, we distinguish between the different file descriptor types that exist under Linux. The Linux find command provides us with the search parameter “-type” to filter by file type. For example, we can **distinguish between files and directories when searching**. Below, we’ve put together an overview of the most commonly used file types:

<table>
  <tbody>
    <tr>
      <td><strong>File type</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>f</td>
      <td>File</td>
    </tr>
    <tr>
      <td>d</td>
      <td>Directory</td>
    </tr>
    <tr>
      <td>l</td>
      <td>Link</td>
    </tr>
  </tbody>
</table>

To **include only files in the search results**, we use the “-type” parameter followed by the value “f”:

```none
find . -type f
```

To **include only directories in the search results**, we use the “-type” parameter followed by the value “d”:

```none
find . -type d
```

To **filter by file extension**, we make use of the “-iname” parameter and use the asterisk as a wildcard placeholder.

Find all files with the jpeg or JPEG extension:

```none
find . -type f -iname "*.jpeg"
```

Find all files with the jpeg/JPEG or jpg/JPG extension:

```none
find . -type f -iname "*.jpeg" -or -iname "*.jpg"
```

### Using the Linux file command to filter by size

In Linux, the **concept of the file links several pieces of information**. This usually includes at least the following:

- Name
- File type
- File size
- Timestamp
- Owner and group
- Access rights

All of these can be filtered by using the find command and the appropriate parameters. To **filter by the size of a file**, we use the “-size” parameter followed by a size specification.

The following find command returns files that are at least 700 megabytes in size:

```none
find . -size +700M
```

---

### Note

Filtering by size only works for files. For directories, no size is stored in the data system. Instead, the size can be calculated recursively if needed.

---

The **size specifications consist of a number followed by a unit**. Here is an overview of the available units:

<table>
  <tbody>
    <tr>
      <td><strong>Unit</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>c</td>
      <td>Bytes</td>
    </tr>
    <tr>
      <td>k</td>
      <td>Kilobytes</td>
    </tr>
    <tr>
      <td>M</td>
      <td>Megabytes</td>
    </tr>
    <tr>
      <td>G</td>
      <td>Gigabytes</td>
    </tr>
    <tr>
      <td>b</td>
      <td>512-byte blocks</td>
    </tr>
  </tbody>
</table>

The size specification is interpreted as the exact file size. This is rarely practical, because often the exact size of a searched file is not known. What’s practical is the **restriction to a certain size range**. For this, the number is prefixed with an optional modifier:

<table>
  <tbody>
    <tr>
      <td><strong>Modifier</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>+</td>
      <td>File is bigger than the listed size</td>
    </tr>
    <tr>
      <td>-</td>
      <td>File is smaller than the listed size</td>
    </tr>
  </tbody>
</table>

The following command provides you with files that are smaller than 500 megabytes:

```none
find . -size -500M
```

The following command provides you with files whose size ranges between 400 and 500 megabytes.

```none
find . -size +400M -and -size -500M
```

In addition to specifying an exact size or size range, “-empty” is a separate parameter for **searching for empty files**:

```none
find . -type f -empty
```

This command also works for **directories**:

```none
find . -type d -empty
```

### Using the Linux file command to filter by timestamp

The operating system manages the file system and logs when files have been accessed. Various time stamps are generated in the process. Linux creates **timestamps for the creation, the last modification, and the last access to a file**. Using the find command, we can filter and find these timestamps. Here is an overview of the most used search parameters:

<table>
  <tbody>
    <tr>
      <td>Search parameter</td>
      <td>Explanation</td>
    </tr>
    <tr>
      <td>-ctime, -cmin</td>
      <td>Filter by creation date</td>
    </tr>
    <tr>
      <td>-mtime, -mmin</td>
      <td>Filter by modification date</td>
    </tr>
    <tr>
      <td>-atime, -amin</td>
      <td>Filter by access date</td>
    </tr>
  </tbody>
</table>

To find files that were changed just a day ago, we use the search parameter “-mtime” followed by the value “1”:

```none
find . -type f -mtime 1
```

The displayed search parameters with “**time**” in the name interpret the following value as the number of *days*. The parameters with “**min**” in the name interpret the following value as the number of *minutes*.

Just like when filtering by file size, here we can also **limit the number of past days to a rang**e. Again, the plus and minus signs are used as modifiers:

<table>
  <tbody>
    <tr>
      <td><strong>Modifier</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>+</td>
      <td>Timestamp is more days ago than specified</td>
    </tr>
    <tr>
      <td>-</td>
      <td>Timestamp is less days ago than specified</td>
    </tr>
  </tbody>
</table>

To find files that were created **more than 100 days ago** we use the search parameter “-ctime” followed by the value “+100”:

```none
find . -type f -ctime +100
```

Just like when filtering by file size, the search parameters can be combined to cover a range. To find files that were **accessed between three and five days ago**, we use the “-atime” search parameter twice, each time with the values “+2” and “-6”. The explicit combination via the “-and” parameter is optional:

```none
find . -type f -atime +2 -and -atime -6
```

To find files whose changes are less **than five minutes old**, we use the search “-mmin” with the value “-5”:

```none
find . -type f -mmin -5
```

### Using the Linux file command to filter by owner, group, and access rights

Under Linux, **each** **file is assigned a user who acts as the owner**. Furthermore, each file belongs to a certain user group. Based on this, certain access rights (permissions) are defined for each file. Based to all this information, we can use the find command to filter and find files under Linux. Here is an overview of the search parameters used:

<table>
  <tbody>
    <tr>
      <td><strong>Search parameter</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>-user</td>
      <td>Filter by owner</td>
    </tr>
    <tr>
      <td>-group</td>
      <td>Filter by group</td>
    </tr>
    <tr>
      <td>-perm</td>
      <td>Filter by access rights</td>
    </tr>
  </tbody>
</table>

To search for files **owned by the root user**, we use the search parameter “-user” followed by the value “root”:

```none
find . -user root
```

To search for files **owned by the own user**, we use the search parameter “-user” followed by the expression “$(whoami)”. The latter is resolved to the name of the logged-in user:

```none
find . -user $(whoami)
```

To search for further files that **belong to the admin group**, we use the search parameter “-group” followed by the value “admin”:

```none
find . -group admin
```

Besides filtering by owner and group, it’s also possible to **filter by access rights**. A triplet of octal numbers is used for this. Frequently used values include “644”, “755” etc. The first number defines the access rights for the owner, the second for the group, the third for other users. Each of the three octal numbers is created by adding the individual rights. We explain exactly how this works in greater detail in our article on [assigning directory permissions with chmod](https://www.ionos.ca/digitalguide/server/know-how/allocating-directory-rights-with-chmod/ "Allocating directory rights with chmod").

To find files that are **fully accessible to any user**, we use the search parameter “-perm” followed by the value “777”:

```none
find . -perm 777
```

To find files that are **fully accessible only to the owner**, we use the search parameter “-perm” followed by the value “700”:

```none
find . -perm 700
```

We can also use the find command to find files under Linux that have, at **minimum, the specified permissions**. To do this, we immediately prefix the octal number with a minus sign:

```none
find . -perm -007
```

### Limiting the recursion depth of the Linux find command

Normally, the Linux find command recursively goes through all subdirectories. However, it’s often useful to **limit the depth of the recursion**. For this, we use the search parameters “-maxdepth” and “-mindepth”:

<table>
  <tbody>
    <tr>
      <td><strong>Search parameter</strong></td>
      <td><strong>Explanation</strong></td>
    </tr>
    <tr>
      <td>-maxdepth</td>
      <td>Maximum recursion depth</td>
    </tr>
    <tr>
      <td>-mindepth</td>
      <td>Minimum recursion depth</td>
    </tr>
  </tbody>
</table>

To find among files larger than 50 megabytes, including only directories that are not more than two levels deeper than the current directory, we use the following command:

```none
find . -type f -maxdepth 2 -size +50M
```

Among the files, to find those that are larger than 50 megabytes, including only directories that are at least three levels and no more than five levels deeper than the current directory, we use the following command:

```none
find . -type f -mindepth 3 -and -maxdepth 5 -size +50M
```

## Using the Linux file command to find and process files

So far, we’ve limited ourselves to finding files under Linux. However, many use cases require **mass processing of the found files**. Common scenarios include repairing access rights for web-based software like WordPress or deleting files after a hack. We also resort to the find command for these use cases.

Next, let’s look at the **general pattern for running a command for each found file**. We use the “-exec” parameter for this, followed by a Linux command and its parameters. The whole command is terminated by the always constant text “{} \\;”:

```none
find <directory_path> <find_parameter> -exec <command_and_parameter> {} \;
```

Note that the **execution of the command happens without prompting you**! Depending on the search parameters selected and the command given, executing the find command with the “-exec” parameter can cause severe damage to the system.

To limit the risk, there is the “-ok” parameter that is similar to the “-exec” parameter. This **forces the interactive confirmation of the processing** of each individual file found:

```none
find <directory_path> <search_parameter> -ok <command_and_parameter> {} \;
```

As a precaution, we limit the recursion depth to only one subdirectory via “-maxdepth 1” in the following examples.

---

### Note

Caution: Be careful with the following examples. We strongly recommend that you create your own folder to try them out. Switch to this folder before running the examples to make sure you don’t damage your system!

---

### Using the Linux file command to adjust the user and groups

To set the owner and group of all files and directories to the value “www-data” we use the following find command with the **chown command**:

```none
find . -maxdepth 1 -exec chown www-data:www-data {} \;
```

### Using the Linux file command to adjust file rights

To find files with rights “777” and set them to “664” we use the following find command with the **chmod command**:

```none
find . -type f -maxdepth 1 -perm 777 -exec chmod 664 {} \;
```

To set the permissions of all directories to “755”, we use the following find command with the chmod command:

```none
find . -type d -maxdepth 1 -exec chmod 755 {} \;
```

### Using the Linux file command to delete empty directories and files

You can also use the find command to **delete found files and directories**. As a precaution, we’ll show this here only for empty files and directories. Furthermore, instead of the “-exec” parameter, we use the “-ok” parameter to force the user to explicitly agree to the deletion.

To delete all empty [Linux directories](https://www.ionos.ca/digitalguide/server/configuration/delete-a-linux-directory/ "Delete a Linux directory") we use the following find command alongside the **rmdir command**:

```none
find . -type d -maxdepth 1 -empty -ok rmdir {} \;
```

To [delete all empty Linux files](https://www.ionos.ca/digitalguide/server/configuration/deleting-files-in-linux/ "Deleting files in Linux"), we use the following find command with the **rm command**:

```none
find . -type f -maxdepth 1 -empty -ok rm {} \;
```


This is a markdown version of: [https://www.ionos.ca/digitalguide/server/configuration/linux-find-command/](https://www.ionos.ca/digitalguide/server/configuration/linux-find-command/) for AI/LLM consumption.