find Command - Search Files & Directories
What Is the find Command?
The find command searches for files and directories based on criteria such as name, type, and whether they are empty.
Unlike tools that search an existing filename database, find walks through the directory structure from a specified starting location. By default, it searches recursively, including subdirectories.
It is useful for locating files, finding empty directories, and managing files across a Linux system.
Basic Syntax
find [path] [expression]path: The directory where the search begins. Use.for the current directory.expression: The conditions used to identify matching files or directories.
Searching by Name
1. Find a Specific File
find . -name "telusko.txt"Searches for entries named telusko.txt in the current directory and all its subdirectories.
The -name option performs a case-sensitive name match.
For a case-insensitive search, use -iname:
find . -iname "telusko.txt"This also matches names such as Telusko.txt and TELUSKO.TXT.
2. Find Files by Extension
Use the wildcard * to match multiple filenames.
find . -name "*.txt"This finds entries whose names end in .txt, including files in nested directories.
Similarly, to find log files:
find . -name "*.log"Note: Keep the wildcard pattern in quotes so the shell passes it to find instead of expanding it before the command runs.
3. Search in a Specific Directory
find /home -name "*.txt"Searches for .txt entries inside /home and its subdirectories.
You can replace /home with any directory you want to search.
Searching by Type
The -type option lets you restrict results to a particular kind of filesystem entry.
4. Find Directories
find . -type dDisplays directories under the current location, including nested directories.
5. Find Regular Files
find . -type fDisplays regular files under the current location and its subdirectories.
This excludes directories and other types of filesystem entries, such as symbolic links.
Finding Empty Files and Directories
1. Find Empty Directories
find . -type d -emptyDisplays directories that contain no files or subdirectories.
2. Find Empty Files
find . -type f -emptyDisplays regular files with a size of zero bytes.
This can help identify unused placeholder files or files that were created but never populated.
Combining find with Pipes
You can combine find with other commands when you need to process its results further.
For example:
find . -name "*.log" | grep "error"How it works:
findlocates entries whose names end in.log.- The pipe (
|) passes the resulting paths togrep. grepdisplays paths containing the texterror.
Important: This searches the file paths, not the contents of the log files. To search inside log files for lines containing error, use:
find . -type f -name "*.log" -exec grep -n "error" {} +Here, find locates regular .log files, and grep searches their contents while displaying matching line numbers.
Quick Reference
| Command | Purpose |
|---|---|
find . -name "file.txt" | Find entries with a specific name |
find . -iname "file.txt" | Find entries by name, ignoring case |
find . -name "*.txt" | Find entries ending in .txt |
find /home -name "*.txt" | Search for .txt entries under /home |
find . -type d | Find directories |
find . -type f | Find regular files |
find . -type d -empty | Find empty directories |
find . -type f -empty | Find empty regular files |
Summary
findsearches the filesystem starting from a specified path.- It searches subdirectories recursively by default.
- Use
-nameor-inameto search by name, and-typeto filter by entry type. - Use
-emptyto locate empty files or directories. - Combine
findwith commands such asgrepwhen additional filtering or processing is needed.
Remember: Use find when you need to locate files or directories by their names, types, or other conditions.
Written By: Muskan Garg
How is this guide?
Last updated on
