The find command is one of the most powerful and widely used tools in Linux for searching files and directories. It allows administrators and developers to locate files based on various conditions such as file name, type, size, permissions, ownership, and modification time.
Unlike many other search utilities, find performs a recursive search starting from a specified directory and explores all subdirectories. Because of this flexibility, it is commonly used in system administration tasks such as log cleanup, file auditing, automation scripts, and security checks.
The find command can also perform actions on matched files, such as deleting files, executing commands, changing permissions, or filtering content using other Linux utilities like grep, awk, and xargs.
In this cheat sheet, you will learn the syntax of the find command along with practical examples to help you quickly locate and manage files in Linux systems.
Syntax of the find Command
The general syntax of the find command is:
find [options] [path] [expression]
Where:
- options – Control the behavior of the search operation.
- path – The directory where the search should begin.
- expression – Conditions used to match files and directories.
If no path is specified, find searches the current working directory by default.
Example:
find .
This command lists all files and directories in the current directory and its subdirectories.
Quick Reference: Linux find Command Cheat Sheet
The following table lists the most commonly used find command examples for quickly locating and managing files in Linux systems.
| Task | Command |
|---|---|
| Find a file by name | find . -name "file.txt" |
| Find files ignoring case | find . -iname "file.txt" |
| Find files by extension | find . -name "*.log" |
| Find directories only | find . -type d |
| Find regular files only | find . -type f |
| Find symbolic links | find . -type l |
| Find files larger than 100MB | find . -type f -size +100M |
| Find files smaller than 10MB | find . -type f -size -10M |
| Find files modified in last 7 days | find . -mtime -7 |
| Find files modified more than 30 days ago | find . -mtime +30 |
| Find files accessed in last 24 hours | find . -atime -1 |
| Find files owned by a specific user | find . -user username |
| Find files belonging to a specific group | find . -group groupname |
| Find files with specific permissions | find . -perm 755 |
| Find files with SUID permission | find / -perm -4000 |
| Limit search depth to 2 directories | find . -maxdepth 2 |
| Start searching after a depth of 2 | find . -mindepth 2 |
| Find empty files | find . -type f -empty |
| Find empty directories | find . -type d -empty |
| Find files and delete them | find . -name "*.tmp" -delete |
| Find files and execute a command | find . -name "*.log" -exec rm {} \; |
| Find files containing specific permissions | find . -perm -644 |
| Find files and change permissions | find . -type f -exec chmod 644 {} \; |
| Find files and copy them | find . -name "*.conf" -exec cp {} /backup/ \; |
Find Files and Directories
Find all files in the current directory
To list all files and directories inside the current working directory and its subdirectories, use:
find .
This command starts searching from the current directory (.).
Find files in a specific directory
You can specify a path to search within a particular directory.
find /path/to/directory
Example:
find /var/log
This searches all files and directories inside /var/log.
Find only files
To search only for regular files and exclude directories, use the -type f option.
find . -type f
This command lists only files in the current directory and its subdirectories.
Find only directories
To search only for directories, use the -type d option.
find . -type d
This displays only directories inside the specified location.
Find symbolic links
To locate symbolic links in a directory structure, use the -type l option.
find . -type l
This helps identify symlinks within the filesystem.
Find Files by Name and Pattern
Find files by exact name
To locate files with a specific name, use the -name option.
find . -name "file.txt"
This command searches for file.txt in the current directory and all subdirectories.
Find files ignoring case
To perform a case-insensitive search, use the -iname option.
find . -iname "file.txt"
This will match filenames such as File.txt, FILE.txt, or file.txt.
Find files by extension
To find files with a particular extension, combine -name with wildcard characters.
find . -name "*.log"
This command searches for all .log files.
Find files matching wildcard patterns
You can use wildcard patterns to search for files that match certain naming conventions.
Example: find files starting with config.
find . -name "config*"
Example: find files ending with .conf.
find . -name "*.conf"
Wildcard searches are useful when working with multiple related files.
Find Files Based on Size
The find command can locate files based on their size. This is useful for identifying large files consuming disk space or locating small temporary files.
Find files by exact size
To locate files that match an exact size, use the -size option.
find . -size 100M
This command finds files that are exactly 100 MB in size.
Find files larger than a specific size
To find files larger than a certain size, use the + prefix.
find . -size +100M
This searches for files larger than 100 MB.
Find files smaller than a specific size
To find files smaller than a specific size, use the - prefix.
find . -size -10M
This command lists files smaller than 10 MB.
Find files within a size range
You can combine multiple size filters to find files within a specific range.
find . -size +50M -size -200M
This finds files between 50 MB and 200 MB.
Find Files Based on Time
The find command can also search for files based on time attributes such as modification time, access time, and change time.
Find files by modification time
The -mtime option searches files based on when they were last modified.
find . -mtime -7
This finds files modified within the last 7 days.
To find files modified more than 30 days ago:
find . -mtime +30
Find files by last access time
The -atime option searches files based on when they were last accessed.
find . -atime -1
This finds files accessed within the last 24 hours.
Find files by change time
The -ctime option searches files based on when file metadata was last changed.
find . -ctime -3
This finds files whose metadata was changed within the last 3 days.
Find Files Based on Ownership and Permissions
You can use find to locate files based on their ownership or permission settings.
Find files owned by a specific user
Use the -user option to locate files owned by a particular user.
find . -user username
Example:
find /home -user deepak
This finds files owned by user deepak.
Find files belonging to a specific group
Use the -group option to locate files belonging to a specific group.
find . -group groupname
Example:
find /var/www -group www-data
Find files by permission
Use the -perm option to search files based on permissions.
find . -perm 755
This finds files with exact permission 755.
To find files with at least a specific permission:
find . -perm -644
Limit Search Depth in find
When searching large directory structures, it can be helpful to limit how deep the search should go.
Limit search using maxdepth
The -maxdepth option restricts how deep find searches into directories.
find . -maxdepth 2
This searches only two directory levels deep.
Skip upper directories using mindepth
The -mindepth option tells find to start searching after a certain depth.
find . -mindepth 2
This skips the current directory and begins searching from level 2 onward.
Perform Actions on Files Using the find Command
The find command can do more than just locate files. It can also perform actions on the matched files such as executing commands, filtering content, or processing files using other utilities.
Use find with exec
The -exec option allows you to run a command on each file that matches the search criteria.
find . -name "*.log" -exec ls -lh {} \;
This command searches for .log files and runs ls -lh on each matched file.
Example: delete .tmp files.
find . -name "*.tmp" -exec rm {} \;
The {} placeholder represents the matched filename.
Use find with grep
You can combine find with grep to search for text within files.
Example: search for the word “error” in all .log files.
find . -name "*.log" -exec grep "error" {} \;
This command scans each log file and prints lines containing the word error.
Use find with xargs
The xargs command allows you to pass the output of find as input to another command.
Example: delete all .tmp files.
find . -name "*.tmp" | xargs rm
Example: display disk usage of matched files.
find . -name "*.log" | xargs du -h
Using xargs can be faster when processing a large number of files.
Delete Files Using find
The find command can also remove files directly. However, it is recommended to preview files before deleting them to avoid accidental removal.
Safely preview files before deleting
Before deleting files, first verify which files will be matched.
find . -name "*.tmp"
Once you confirm the results, you can proceed with deletion.
Delete files matching a pattern
Use the -delete option to remove files matching a specific pattern.
find . -name "*.tmp" -delete
Example: remove log files older than 30 days.
find /var/log -name "*.log" -mtime +30 -delete
Be cautious when using the -delete option because it permanently removes files.
Advanced find Command Examples
The find command becomes very powerful when combined with other Linux utilities. These advanced examples demonstrate how administrators commonly automate file management tasks.
Search and execute commands on files
You can execute custom commands on matched files.
Example: compress all .log files.
find . -name "*.log" -exec gzip {} \;
This command compresses each log file individually.
Find files and modify permissions
Use find to update permissions across multiple files.
Example: change permissions for all .sh scripts.
find . -name "*.sh" -exec chmod 755 {} \;
This sets executable permissions for all shell scripts.
Combine find with other Linux commands
The find command can work together with commands such as tar, du, or grep.
Example: archive all .conf files.
find /etc -name "*.conf" | tar -czvf configs.tar.gz -T -
Example: count all .txt files.
find . -name "*.txt" | wc -l
Common find Command Mistakes and Useful Tips
Always quote wildcard patterns
Shell wildcard characters such as * can be expanded by the shell before find processes them. To avoid unexpected results, always quote wildcard patterns.
Incorrect example:
find . -name *.log
Correct example:
find . -name "*.log"
Quoting ensures that the pattern is interpreted by find rather than by the shell.
Handle permission denied errors
When searching system directories, you may encounter permission denied errors. These appear when the current user does not have access to certain directories.
Example:
find / -name "config.conf"
To suppress permission errors:
find / -name "config.conf" 2>/dev/null
Alternatively, run the command with elevated privileges if appropriate.
sudo find / -name "config.conf"
Exclude specific directories from search
Sometimes you may want to exclude directories such as .git, node_modules, or backup folders from the search.
Example:
find . -path "./node_modules" -prune -o -name "*.js" -print
This skips the node_modules directory and searches for .js files elsewhere.
Use maxdepth to improve performance
Searching very large directory structures can take time. You can limit recursion using the -maxdepth option.
Example:
find . -maxdepth 2 -name "*.conf"
This restricts the search to two directory levels, which improves performance.
Use find with care when deleting files
When deleting files using find, always preview the results first to avoid accidental data loss.
Preview files:
find . -name "*.tmp"
Delete files after verification:
find . -name "*.tmp" -delete
This two-step approach helps prevent unintended file removal.
Prefer xargs for bulk operations
When processing a large number of files, combining find with xargs can improve efficiency.
Example:
find . -name "*.log" | xargs rm
This allows multiple files to be passed to the command at once instead of executing the command separately for each file.
Frequently Asked Questions
1. What is the find command used for in Linux?
The find command in Linux is used to search files and directories in a directory hierarchy based on various criteria such as name, size, permissions, modification time, ownership, and file type.2. How do I search for a file by name using the find command?
You can search for a file by name using the -name option. For example, run find . -name “file.txt” to search for a file named file.txt in the current directory and its subdirectories.3. How do I find files larger than a specific size in Linux?
Use the -size option with a plus sign. For example, find /home -type f -size +100M will locate files larger than 100MB in the /home directory.4. How do I execute a command on files found using find?
You can use the -exec option to run commands on matched files. For example, find . -name “*.log” -exec rm {} ; will delete all log files found in the current directory.5. What is the difference between find and locate commands?
The find command performs a real-time search in the filesystem, while the locate command searches a pre-built database of file paths, making locate faster but sometimes outdated.Conclusion
The find command is one of the most powerful file search utilities available in Linux. It allows administrators and users to locate files based on name, size, time, permissions, ownership, and many other attributes. In addition to searching, the command can also execute actions such as deleting files, modifying permissions, or processing results using other Linux utilities.
By mastering the options and examples covered in this guide, you can efficiently manage files across large directory structures and automate many common administrative tasks.
What’s Next in Your Linux Learning Path
To continue learning about Linux file management and command-line tools, you may find the following guides helpful:
