GNU find cheat sheet
Summary
This cheat sheet provides a comprehensive guide to the GNU find command, covering syntax, filters, actions, and performance tips. A quick reference for GNU find with many practical examples.
Introduction #
The GNU find command is used to search for files and directories on a filesystem based on user-defined criteria. It is part of the GNU Findutils package, commonly available on Unix-like operating systems such as Linux.
This cheat sheet provides command-line patterns and options to help you locate files and take actions on them directly from your terminal.
Basic syntax of the find command #
find [starting-point] [expression]
[starting-point]: the directory to start the search[expression]: one or more search conditions and actions
File name and type filters #
Find files by exact name #
Use -name to locate files that exactly match the specified name.
find . -name "file.txt"
This searches in the current directory (.) and all subdirectories for a file named exactly file.txt.
Case-insensitive file name match #
Use -iname for case-insensitive name matching.
find . -iname "file.txt"
Finds files named file.txt, File.txt, or any other case variation.
Find by file type #
Use -type to restrict results to regular files, directories, or symbolic links.
find . -type f # regular files
find . -type d # directories
find . -type l # symbolic links
Searches for items of a specific type within the current directory tree.
Find files with specific extensions #
Use -iregex with a regular expression (regex) to match file extensions while ignoring case (case-insensitive).
find . -iregex ".*\.(txt\|log)$"
Finds files with .txt or .log extensions, regardless of case.
Find empty files or directories #
Locate files or directories with zero size or no content using -empty.
find . -type f -empty # Empty files
find . -type d -empty # Empty directories
Returns files or directories that contain no data or children, respectively.
Find broken symlinks #
Identify broken symbolic links with -xtype l or search for symlinks matching a pattern using -lname.
find . -xtype l # Broken symbolic links
find -L . -lname "*.so" # Symlinks matching pattern (follows links)
The first command finds broken links; the second finds symlinks that end in .so.
Match file name patterns #
Use wildcards with -name to match file name patterns.
find . -name "*.log"
Finds files ending in .log within the current directory and its subdirectories.
Size and time filters #
Find files by size #
Use -size with units like k, M, or G to match file size.
find . -size +100M
Finds files larger than 100 megabytes.
Find files with exact size #
Search for files that match an exact size value.
find . -size 10k
Finds files that are exactly 10 kilobytes in size.
Find recently modified files #
Use -mtime to find files modified in the last n days.
find . -mtime -1
Finds files modified within the past 24 hours.
Find files accessed in the past days #
Use -atime to find files accessed more than n days ago.
find . -atime +7
Returns files that haven’t been accessed in more than 7 days.
Find files by time in minutes #
Filter files by modification (-mmin), access (-amin), or metadata change time (-cmin) in minutes instead of days.
find . -mmin -30 # Modified in last 30 minutes
find . -amin +60 # Accessed over 60 mins ago
Useful for time-sensitive file management or cleanup tasks.
Find files newer than a reference file #
Use -newer to locate files modified more recently than the specified reference file.
find . -newer reference.txt
Finds files modified more recently than reference.txt.
Permission and ownership filters #
Find files by permissions #
Match files with specific numeric permission modes.
find . -perm 0644
Finds files with permission mode exactly 0644.
Exclude specific permissions #
Find files that do not match a specific permission mode.
find . ! -perm 0777
Finds files that are not world-readable/writable/executable.
Find files by user ownership #
Locate files owned by a specific user.
find . -user username
Finds files belonging to the given username.
Find files by group ownership #
Find files owned by a particular group.
find . -group groupname
Returns files belonging to groupname.
Depth and path filters #
Limit search depth #
Use -maxdepth to limit recursion to a specific level.
find . -maxdepth 1
Searches only the current directory (no recursion).
Set minimum search depth #
Use -mindepth to skip top-level entries.
find . -mindepth 2
Skips the top-level directory and starts matching from deeper levels.
Exclude directories from results #
Use -prune with -path to exclude directories from search.
find . -path "./temp/*" -prune -o -print
Skips the ./temp/ directory and prints all other results.
Avoid crossing filesystems #
Restrict searches to the current filesystem (skip mounted drives) with -xdev.
find / -xdev -name "*.conf"
Searches the root filesystem but avoids traversing into mounted volumes.
Using -exec to act on found files #
Delete files using -exec #
Use -exec to delete each file found by the search.
find . -type f -name "*.tmp" -exec rm {} \;
Deletes all .tmp files found under the current directory.
Batch process files using -exec #
Use + to pass all matching files to the command in one go.
find . -type f -name "*.jpg" -exec mv {} /backup/ \+
Moves all .jpg files to the /backup/ directory in one command.
Use -delete for faster file removal #
Remove files directly without -exec for better performance.
find . -name "*.bak" -delete
Quickly deletes all .bak files under the current path.
Prompt before executing #
Execute commands interactively, with confirmation prompts for each match (-ok for safety).
find . -name "*.tmp" -ok rm {} \;
Asks for confirmation before deleting each .tmp file.
Find and compress files #
Pass matching files to compression tools like gzip in bulk.
find . -name "*.log" -exec gzip {} \+
Compresses all .log files using gzip in batches.
Find files with specific content #
Combine find with grep to search inside files for text patterns.
find . -type f -exec grep -l "error" {} \;
Finds all files that contain the word “error”.
Printing and formatting results #
Print file paths #
The default output shows full paths of matched files.
find . -type f
Lists all regular files under the current directory.
Print file names only #
Use -printf to format output with just the file name.
find . -type f -printf "%f\n"
Displays only the base name of each file.
Print size and path #
Use -printf to display file size and full path.
find . -type f -printf "%s %p\n"
Shows each file’s size in bytes followed by its full path.
Print detailed file info #
Display permissions, ownership, size, and timestamps with -ls.
find . -type f -ls
Provides detailed metadata for each file found.
Null-delimited output #
Output filenames separated by null characters to handle spaces/newlines safely (safe for xargs -0).
find . -name "*.txt" -print0 | xargs -0 rm
Safely deletes all .txt files even if they contain spaces or special characters.
Custom timestamp formatting #
Format output with precise timestamps using -printf placeholders.
find . -printf "%p - Last modified: %TY-%Tm-%Td %TH:%TM\n"
Shows the file path along with its last modification date and time.
Combining expressions with logical operators #
Combine conditions with AND #
Default behavior combines conditions using logical AND.
find . -type f -name "*.sh" -perm -u+x
Finds executable shell scripts owned by the user.
Use OR operator #
Group expressions with parentheses and use -o for OR.
find . \( -name "*.c" -o -name "*.h" \)
Finds both C source and header files.
Negate conditions #
Use ! to negate a condition.
find . ! -name "*.txt"
Finds all files except those ending with .txt.
Performance considerations #
Exclude system directories #
Prevent find from entering specific directories.
find / -path "/proc" -prune -o -print
Skips /proc while searching from root.
Stop after first match #
Use -quit to stop processing after the first match is found.
find . -name "config.json" -print -quit
Finds the first config.json and exits immediately.
Use absolute paths in scripts #
Always use full paths when scripting with find to ensure consistent behavior.
Optimize search speed with -O3 (GNU only) #
Enable GNU find’s highest optimization level for faster searches.
find -O3 . -name "*.pdf"
Optimizes expression evaluation for performance when searching for .pdf files.
Common find options #
| Options | Description |
|---|---|
-name | Match file or directory name (case-sensitive) |
-iname | Match file or directory name (case-insensitive) |
-type | Match file type (f for file, d for directory, etc.) |
-size | Match file size using suffixes (k, M, G) |
-mtime | Match last modification time in days |
-atime | Match last access time in days |
-user | Match file owner by user name |
-group | Match file group by group name |
-perm | Match files with specified permission bits |
-maxdepth | Limit recursion to a number of directory levels |
-mindepth | Set minimum depth to begin applying expressions |
-exec | Execute command on each found item |
-print | Output matched file paths (default behavior) |
-printf | Output formatted result (GNU only) |
-prune | Exclude a directory from recursion |
-quit | Stop searching after the first match |
-o | Logical OR operator |
! or -not | Logical NOT operator |
() or \( \) | Group expressions for precedence |
-empty | Match empty files/directories |
-mmin / -amin | Match time in minutes (modification/access) |
-newer | Match files newer than a reference file |
-ls | Display detailed file info (like ls -l) |
-print0 | Null-delimited output (safe for xargs -0) |
-delete | Delete files directly (no -exec rm needed) |
-ok | Prompt before executing (-exec with confirmation) |
-xdev | Avoid searching mounted filesystems |
-O3 | Enable maximum optimization level (GNU find) |
-xtype | Match broken symlinks (l for broken links) |
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
What is the difference between find and locate in Linux?
find searches the filesystem in real time, while locate uses a prebuilt database for faster results but may be outdated.
How do I find files modified in the last 24 hours?
Use find /path -type f -mtime -1 to find files modified in the last 24 hours.
Can I delete files directly using find?
Yes, you can use -exec rm {} ; to delete files found by find. Be cautious with this operation.
How do I search only the current directory without recursion?
Use -maxdepth 1 to restrict find to the current directory.
Further readings #
Sources and recommended, further resources on the topic:
License
GNU find cheat sheet by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/find-cheat-sheet">GNU find cheat sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.