How to use to find files recursively?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
When working with file systems, being able to search through directories recursively to find files that meet certain criteria is a crucial skill. This can be done using different approaches and tools depending on the operating system you are using. In this article, we will cover techniques for finding files recursively in Unix-like systems (including Linux and macOS) using commands like find, grep, and tools such as locate.
Using the find Command
The find command is one of the most powerful and frequently used tools in Unix-like operating systems for searching through directories recursively. The basic syntax of the find command is:
- Starting Directory: This is the directory where the
findcommand begins its search. If not specified, the default is the current directory. - Options: Modify the behavior of the
findcommand. - Expression: This defines what to find (files, directories, etc.) and actions to take.
Examples of find Command
- To find all
.txtfiles in the current directory and its subdirectories:
- To find all directories named "docs" within
/home/user:
- To find files modified in the last 7 days:
Using grep with find
Combining find with grep can be useful for searching inside files for specific content.
This command will search for Python files (.py) that contain the import statement import os.
Using locate
Another tool for finding files by name is locate. Unlike find, locate uses a database that is updated regularly (typically by a scheduled job) to store the path of every file and directory. This makes searches with locate much faster, but less up-to-date.
Example:
To find files that include project in their name:
Note: You might need to update the database by running sudo updatedb before using locate to ensure it has current data.
Tips
- Use
-maxdepthwithfindto limit the search to a certain number of subdirectories. - Combine
findandgrepusingxargsfor more complex searches. - Regularly update the
locatedatabase to keep the search results relevant.
Summary Table
| Command | Use Case | Example Command | Description |
find | Directly search file system recursively | find . -name "*.txt" | Finds all .txt files in the current directory and subdirectories. |
grep | Search within files | grep -r "search_term" . | Recursively searches for "search_term" within files in the current directory. |
locate | Quickly find files by name using a database | locate config.json | Looks for files named config.json using a pre-built database. |
Advanced Find Tactics
Beyond simple file name matching, find supports a host of other features:
- Permissions: Search for files with specific permissions.
- Exec: Execute a command on each file found.
- Size: Find files above or below a certain size.
Finds files in /var/log larger than 100M.
Conclusion
Understanding how to use tools like find, grep, and locate allows for efficient navigation and manipulation of the filesystem. Whether you are a system admin, a developer, or just a curious user, mastering these commands can significantly enhance your productivity and insights into the file system.
Related reading
- how to use weight when training a weak learner for adaboost
- How to walk binary abstract syntax tree to generate infix notation with minimally correct parentheses
- How to work out the complexity of the game 2048?
- How to write a function to generate random number 0/1 use another random function?
- How to write a probability algorithm that can be maintained easily?
- how to write a recurrence relation for a given piece of code
- How to write an algorithm to check if the sum of any two numbers in an array/list matches a given number?
- How to write function for N-ary tree traversal in Haskell

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.