Find and search files

Find

Find allows searching file names and directories under the current location

find <path> <settings>

Exclude specified pattern from find (eg. '*/\.git/*' for ignoring .git directories)

find <path> -not -path '<pattern>'

Show files that match the search criteria. The criteria can be a literal name or use wildcards * (wrap non-alphanumeric search criteria with quotes)

find . -name '<file-pattern>'

Find files (-type f), directories (-type d) or both

find <path> -type f
find <path> -type d
find <path>

Find files that match the file pattern and pass results to grep for searching file contents

find <path> -name '<file-pattern>' | xargs grep '<search-string>'

Find folders that are at a certain depth with the specified name

find . -mindepth 1 -maxdepth 1 -type d -name '<search-string>'

Find files containing case insensitive <search-string> somewhere in their name

find <path> -iname '*<search-string>*' -type f

Find files under path that match file pattern excluding those inside specified directory

Find files under path that match file pattern excluding those inside multiple directories

Notes on using find

  • -o is an OR operator

  • -prune excludes directory contents from search

  • -false when paired with prune removes pruned directories name appearing in output

Grep

Output files with content matching (-l) or not matching (-L) the search string

Search files recursively for search string below path location

Search using extended regex (-E)

Only show matches (-o) and do not print filenames (-h)

Advanced Grep Examples

Find files and print with sizes (Note: -printf is not POSIX and only available with GNU find)

Find files without *.orig and format output to include timestamp and path, then sort and grab the first 100

Show files with non-ascii characters

List all image filenames

Last updated