---
title: How to Use the Find Command in Linux
url: https://docs.vultr.com/how-to-use-the-find-command-in-linux
description: Learn how to use the find command in Linux to search for files and directories, filter by criteria, and execute actions on search results efficiently.
publish_date: 2024-10-11T11:30:14.302147Z
last_updated: 2024-10-11T11:30:14.302147Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1963/f2f0bb02-43d5-4c98-941a-e5b7e20f787f.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

The `find` command in Linux is a powerful and versatile utility for searching or locating files and directories in a file system. The command uses file attributes like name, size, time, and permissions to perform the search. You can also combine `find` with other commands to perform complex searches.

This article explains how to use the `find` command in Linux to effectively manage and search files.

## The `find` Command Syntax

The following is a basic `find` command syntax:

```
find [path] [expression]
```

In the above `find` command:

* `[path]`: Specifies the directory to search. It defaults to the working directory `.` if you don't specify a path.
* `[expression]`: Combines options, tests, and actions to filter and manipulate the search results such as the filename, file type, size, and modification time.

## Set Up Example Directories and Files

Follow the steps below to set up example directories and files to test the `find` command.

1. Switch to your user's home directory.

    ```console
    $ cd
    ```

1. Create `vultr.txt`, `Vultr.txt`, and `VULTR.txt` sample files.

    ```console
    $ touch vultr.txt Vultr.txt VULTR.txt
    ```

1. Create `dir`, `dir1` directories. Then create a `sub_dir` sub-directory under `dir1`.

    ```console
    $ mkdir -p dir dir1/sub_dir
    ```

1. Create a new `file.txt` text file.

    ```console
    $ touch file.txt
    ```

1. Change the `file.txt` file permissions mode to `755`.

    ```console
    $ chmod 755 file.txt
    ```

Check out how to use the [`tar` command in Linux](https://docs.vultr.com/how-to-compress-files-in-linux-using-the-tar-command) for quick file archiving and unzipping.

## Most Common `find` Command Options

The `find` command supports the following options for refining search results.

|    Option    |    Description                                               |
|--------------|--------------------------------------------------------------|
| `-name`      | Searches for files and directories with a specific name.     |
| `-iname`     | Case-insensitive version of `-name`.                         |
| `-type`      | Searches for files of a specific type (`f` for regular files, `d` for directories).|
| `-size`      | Searches for files of a specific size (for example, `+100M` for files larger than 100 MB).|
| `-mtime`     | Searches for files modified within a specific number of days (for example, `-7` for files modified in the last 7 days).  |
| `-atime`     | Searches for files accessed within a specific number of days.|
| `-user`      | Searches for files owned by a specific user.                 |
| `-group`     | Searches for files owned by a specific group.                |
| `-perm`      | Searches for files with specific permissions (for example, `755`).  |
| `-exec`      | Executes a command on each matched file.                     |
| `-delete`    | Deletes the matched files.                                   |
| `-ls`        | Lists the matched files in `ls -dils` format.                |
| `-print`     | Prints the full filename on the standard output.            |

## Use Practical `find` Command in Linux with Examples

Follow the steps below to test the `find` command and search the example files you created earlier.

1. Find a file named `vultr.txt` in your working directory and subdirectories.

    ```console
    $ find . -name "vultr.txt"
    ```

    Output:

    ![Find vultr.txt Files](https://docs.vultr.com/public/doc-assets/1963/d9f2bdea-bf2e-4e77-be95-655843989bb1.gif)

1. Perform a case-insensitive search for all files that match `vultr.txt`.

    ```console
    $ find . -iname "vultr.txt"
    ```

    Output:

    ![Case-Insensitive Search](https://docs.vultr.com/public/doc-assets/1963/d8f3ac31-84e5-48c5-8296-31e1e532d2e3.gif)

1. Find all directories in the working directory using the `-type d` option.

    ```console
    $ find . -type d
    ```

    Output:

    ![Find All Directories](https://docs.vultr.com/public/doc-assets/1963/a6ccb294-4736-4d34-a1c4-151df77010d5.gif)

1. Find files by size. For example, files larger than `100MB`.

    ```console
    $ find . -size +100M
    ```

    Output:

    ![Find Large Files](https://docs.vultr.com/public/doc-assets/1963/850771b2-6e36-41a8-a8ed-01234404a75e.gif)

1. Find files by modification time. For example, find files modified in the last 7 days.

    ```console
    $ find . -mtime -7
    ```

    Output:

    ![Find Recent Files](https://docs.vultr.com/public/doc-assets/1963/8e7dbf83-d373-4572-b4c2-3dd26c532096.gif)

1. Find all files by access time. For example, find all files accessed in the last 7 days.

    ```console
    $ find . -atime -7
    ```

    Output:

    ![Find Recently Accessed Files](https://docs.vultr.com/public/doc-assets/1963/ddb97695-b268-47aa-b56a-9e62e841cee8.gif)

1. Find all files a specific user owns, such as `vultr_user`.

    ```console
    $ find . -user vultr_user
    ```

    Output:

    ![Find Files Owned by owner](https://docs.vultr.com/public/doc-assets/1963/6474c71c-836a-46f5-818d-dc90862007f0.gif)

1. Find all files owned by a specific group such as `vultr_user`.

    ```console
    $ find . -group vultr_user
    ```

    Output:

    ![Find Files Owned by Group](https://docs.vultr.com/public/doc-assets/1963/35537ad4-7be9-4a37-ac00-6d281bfb2151.gif)

1. Find all files with `755` permissions.

    ```console
    $ find . -perm 755
    ```

    The `755` permission means the owner can read, write, and execute the file, while others can only read and execute it.

    Output:

    ![Find Files with 755 Permissions](https://docs.vultr.com/public/doc-assets/1963/ce182eef-e668-4ea8-bb5a-72bc3f86017a.gif)

## Use Advanced `find` Command Options

Follow the steps below to perform advanced `find` command searches.

1. Execute a command on each matching file using the `-exec` option.

    ```console
    $ find . -name "vultr.txt" -exec ls -l {} \;
    ```

    The above command finds all files named `vultr.txt` and runs the `ls -l` command on each file to list them in a long format. `{}` is a placeholder for the working filename, and the `\;` specifies the end of the command to execute.

    Output:

    ![Execute Command on Files](https://docs.vultr.com/public/doc-assets/1963/34bdfd84-5f2c-418b-8cac-49db757f6cc5.gif)

1. Delete all files named `vultr.txt`.

    ```console
    $ find . -name "vultr.txt" -delete
    ```

    Output:

    ![Delete vultr.txt Files](https://docs.vultr.com/public/doc-assets/1963/dd178823-eb2d-4c9d-b6af-f64b0bc1c121.gif)

1. Find all `.txt` files and copy them to another directory.

    ```console
    $ find . -name "*.txt" -exec cp {} /path/to/directory/ \;
    ```

    The above command finds all `.txt` files and copies them to the `/path/to/directory/` directory. The file name replaces `{}` during execution. Replace `/path/to/directory` with your actual path.

    ![Copy .txt Files](https://docs.vultr.com/public/doc-assets/1963/ba947a67-7a59-4d74-ab59-bce770ffc107.gif)

1. Find all `.txt` files and move them to another directory.

    ```console
    $ find . -name "*.txt" -exec mv {} /path/to/directory/ \;
    ```

    The above command finds all `.txt` files and moves them to the `/path/to/directory/` directory. The filename replaces `{}` during execution. Replace `/path/to/directory` with your actual path.

    Output:

    ![Move .txt Files](https://docs.vultr.com/public/doc-assets/1963/e47a0667-b751-44b9-8c41-e06d2b581e23.gif)

1. Find all `.txt` files a specific user owns, such as `vultr_user` and modified in the last 7 days.

    ```console
    $ find . -name "*.txt" -user vultr_user -mtime -7
    ```

    The above command searches for all `.txt` files owned by the `vultr_user` and modified within the last 7 days. It combines the `-name`, `-user`, and `-mtime` options to create a more specific search.

    Output:

    ![Find Recent .txt Files by owner](https://docs.vultr.com/public/doc-assets/1963/449b71da-98ec-47e0-a3c8-6ae8b1ef3236.gif)

## Run the Interactive `find` Command in Linux with Examples

Follow the steps below to perform interactive searches using the `find` command.

1. Find all files named `Vultr.txt` and list them with detailed information.

    ```console
    $ find . -name "Vultr.txt" -exec ls -l {} \;
    ```

    The above command finds all files named `Vultr.txt` and uses `ls -l` to display detailed information about each file, such as permissions, owner, size, and modification date.

    Output:

    ![List vultr.txt with Details](https://docs.vultr.com/public/doc-assets/1963/de5bd1ab-a449-4ab0-8556-cc75b9a0a5b1.gif)

1. Combine the `find` command with `wc` to locate all files named `Vultr.txt` and count the number of lines.

    ```console
    $ find . -name "Vultr.txt" | wc -l
    ```

    The above command finds all files named `Vultr.txt` and uses the pipe (`|`) option to output the results to the `wc -l` command, which counts the number of matching lines.

    Output:

    ![Count vultr.txt Files](https://docs.vultr.com/public/doc-assets/1963/6f07cc42-1701-463e-97ad-d07e4ab62f3e.gif)

1. Find all files with `644` permissions and change them to `600`.

    ```console
    $ sudo find . -perm 644 -exec chmod 600 {} \;
    ```

    The above command finds all files with `644` permissions and changes the permissions mode to `600` using the `chmod` command. The `{}` option represents the filename, while `\;` marks the end of the command.

    ![Change Permissions to 600](https://docs.vultr.com/public/doc-assets/1963/15f4b513-4965-4c3e-96ae-9562ed4800fe.gif)

## Conclusion

You have used the `find` command in Linux with practical examples to efficiently search for files and directories. The `find` command performs searches based on a specific criteria and runs specific actions on the matched results to streamline your file system workflow.
