How to Use the Find Command in Linux

Updated on October 11, 2024
How to Use the Find Command in Linux header image

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
    
  2. Create vultr.txt, Vultr.txt, and VULTR.txt sample files.

    console
    $ touch vultr.txt Vultr.txt VULTR.txt
    
  3. Create dir, dir1 directories. Then create a sub_dir sub-directory under dir1.

    console
    $ mkdir -p dir dir1/sub_dir
    
  4. Create a new file.txt text file.

    console
    $ touch file.txt
    
  5. Change the file.txt file permissions mode to 755.

    console
    $ chmod 755 file.txt
    

Check out how to use the tar command in Linux 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

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

    console
    $ find . -iname "vultr.txt"
    

    Output:

    Case-Insensitive Search

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

    console
    $ find . -type d
    

    Output:

    Find All Directories

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

    console
    $ find . -size +100M
    

    Output:

    Find Large Files

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

    console
    $ find . -mtime -7
    

    Output:

    Find Recent Files

  6. 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

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

    console
    $ find . -user vultr_user
    

    Output:

    Find Files Owned by owner

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

    console
    $ find . -group vultr_user
    

    Output:

    Find Files Owned by Group

  9. 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

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

  2. Delete all files named vultr.txt.

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

    Output:

    Delete vultr.txt Files

  3. 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

  4. 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

  5. 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

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

  2. 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

  3. 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

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.