Open In App

How to Find a File in Linux | Find Command

Last Updated : 05 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.

find command uses are:

  • Search based on modification time (e.g., files edited last week).
  • Locate files with specific permissions or content.
  • Automate tasks like deleting or executing commands on found files.

Syntax of find Command in Linux

Here is the syntax for the find Command in Linux:

find [path] [options] [expression]
  • Path: Where to start searching (e.g., ~/Documents).
  • Options: Refine your search (e.g., -type for files/directories).
  • Expression: Criteria like filenames or sizes.

This syntax allows you to customize your file search by specifying the path, adding options, and defining search criteria using expressions.

Options in find Command in Linux

Here are the `find` command options along with brief descriptions of their purposes.

OptionWhat It DoesExample
-name "pattern"Searches files by name (case-sensitive).find ~ -name "notes.txt"
-iname "pattern"Case-insensitive name search.find ~ -iname "notes.*"
-type f/dFinds only files (f) or directories (d).find /var/log -type f
-size +10MFinds files larger than 10MB.find / -size +100M
-mtime -7Finds files modified in the last 7 days.find ~ -mtime -7
-perm 644Finds files with specific permissions.find ~ -perm 644
-execRuns commands on found files (e.g., delete).find . -name "*.tmp" -exec rm {} \;
-emptyFinds empty files/directories.find ~ -empty

How find Command Works in Linux

The find command performs real-time searches like they directly traversing the specified directory structure in the filesystem. Unlike tools such as locate, which is depend on a prebuilt database that may not reflect recent changes, find command in linux examines the files and directories as they exist at the moment of search. This ensures that it can accurately locate even newly created or modified files. Here is the working of search process:

  • Starting Point: The find command begins its search from the directory specified in the path argument. It recursively explores all files and subdirectories.
  • Applying Criteria: The expressions define the search conditions. Each file or directory is evaluated based on these criteria.
  • Refining with Options: Options further narrow the search.
  • Taking Action: By default, find prints the paths of matching files. However, it can also execute commands on the results using the -exec option.

The find command is included in the GNU findutils package, typically pre-installed on most Linux distributions.

How to find a File in Linux from the Command Line

Using the find command is straightforward. To find a file in Linux, open a terminal

Syntax:

find /path/to/search -options criteria

Replace "/path/to/search" with the directory where you want to start the search and customize the options and criteria based on your requirements.

For example :

To find a file named "example.txt" in the home directory, you would use:

find ~ -name "example.txt"

This command will locate and display the path to the file if it exists in the specified directory or its subdirectories.

Examples of find Command in Linux

1. How to find a Specific File Using `find` Command in Linux

This query is designed to identify a file within a designated directory. In the provided example, it seeks a file named "sample.txt" within the "GFG" directory.

find ./GFG -name sample.txt 

The find command traverses the specified directory (./GFG) and looks for a file named "sample.txt." If found, it displays the path to the file.

Output:


Search a file with specific name
Search a file with specific name

2. How to Search Files with a Pattern Using `find` Command in Linux

This command is use for discovering files within a directory that attach to a specific naming pattern. In this case, it identifies files ending with '.txt' within the "GFG" directory.

find ./GFG -name *.txt 

The command looks for files with names ending in '.txt' within the "GFG" directory, presenting a list of matching files.

Output:


Search a file with pattern
Search a file with pattern

3. How to find and Confirm File Deletion Using `find` Command in Linux

This command not only locates a specified file but also prompts the user for confirmation before initiating its removal. The example seeks to delete a file named "sample.txt" within the "GFG" directory.

find ./GFG -name sample.txt -exec rm -i {} \; 

The -exec option executes the rm command on the located file, and the -i flag prompts the user for confirmation before deletion. When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if you enter 'Y/y' it will delete the file. 

Output :

 find and delete a file with confirmation
find and delete a file with confirmation

4. Search for Empty Files and Directories Using `find` Command in Linux

This query is tailored for discovering and listing empty files and directories within a specified directory.

find ./GFG -empty

The `find` command identifies and lists all empty folders and files within the "GFG" directory or its subdirectories.

Output:

Search for empty files and directories
Search for empty files and directories

5. Search Files with Specific Permissions Using `find` Command in Linux

This command is used to locate files within a directory that have specific permissions. In the provided example, it identifies files with permissions set to 664 within the "GFG" directory.

find ./GFG -perm 664

The command searches for files within the "GFG" directory with the specified permissions (664) and displays the results.

Output:

Search for file with entered permissions
Search for file with entered permissions

6. Display Repository Hierarchy Using `find` Command in Linux

This command is utilized to display the hierarchical structure of repositories and sub-repositories within a given directory.

find . -type d

This command displays all the repositories and sub-repositories present in the current repository. In the below example, we are currently in a repository namely "GeeksforGeeks" which contains a repo "Linux", which contains sub-repo "LinuxCmds" which further contains a repo "FindCmd". The ouput of below cmd is simply displaying this info. Please note that in this case if you will use "ls" cmd then it will only show "/Linux".

Output:

Screenshot-(548)

7. Search Text Within Multiple Files Using `find` Command in Linux

This command is tailored for finding lines containing specific text within multiple files. The example looks for lines containing the word 'Geek' within all '.txt' files in the current directory and its subdirectories.

find ./ -type f -name "*.txt" -exec grep 'Geek'  {} \;

The command searches for '.txt' files (-type f and -name "*.txt") and uses grep to print lines containing the specified text ('Geek').

Output:


Search text within multiple files
Search text within multiple files

8. Find Files by When They Were Modified Using `find` Command in Linux

The -mtime option is handy for finding files based on their modification time. To find files modified within the last 7 days, you can use:

find /path/to/search -mtime -7

This command will list files modified in the last week.

Finding Last modifications
Finding Last modifications

In this example we are searching changes in directory "/home/administrator/Downloads" which are done is past 7 days.

9.  Find Large Files (Over 100MB)

Find the files which is >100MB across the entire system. You can also change the file like 200M, 400M etc

find / -type f -size +100M  
1
Find large files

10. Find Files Modified in the Last 24 Hours

In this it shows the files edited in the last day. You can also change 2 days (-2).

find ~ -type f -mtime -1  
1
Find Files Modified files
  • ~: This represents your home directory.
  • -type f: This option tells find to only look for files
  • -mtime refers to the modification time of a file.
  • -1 means "less than 1 day ago."

11. Find and Delete Temporary Files

This command is used to find and delete temporary files in the /tmp directory.

sudo find /tmp -type f -name "*.tmp" -exec rm {} \;  
1
Find and Delete Temporary Files from your syste,

12. Limit Search Depth

In this we looks for config.yml only in the current directory and one subfolder deep or we can search any file with search depth.

find . -maxdepth 2 -name "*.txt" 
1
Limit Search Depth

13. Use Grep to find Files Based on Content Using `find` Command in Linux

Combining the find command in linux with grep allows you to search for files based on their content. For example, to find files containing the word "pattern" in the current directory and its subdirectories, you can use:

find . -type f -exec grep -l "pattern" {} \;

This command will display the names of files containing the specified content.

Breakdown of the Command:

  • find .: Initiates the search from the current directory (.).
  • -type f: Specifies that the search is for files only, excluding directories.
  • -exec grep -l "pattern" {} \;: Executes the grep command on each found file ({}) to search for the specified content ("pattern"). The -l option in grep ensures that only the names of files containing the pattern are displayed.

Command Execution:

  1. The find linux command starts the search from the current directory, including all its subdirectories.
  2. For each file (-type f) found in the search, the -exec option executes the grep command.
  3. The grep command searches for the specified content ("pattern") in each file.
  4. If a file contains the specified content, its name is displayed due to the -l option in grep.

Conclusion

The find command in Linux is more than just a search tool — it's a lifesaver for navigating complex file systems. Whether you're managing a large codebase, auditing system logs, or cleaning up old files, find helps you locate exactly what you need in seconds.


Next Article

Similar Reads