find is used to find files based on their name or other attributes of the file.
- It does not search inside files like the
grepcommand - To find
findexamples for a speicific purpose, I do a web search for: linux find ...
find /path/to/dir -iname '*text*'
Dissecting this:
/path/to/dir: where you want to search, I often use.for my current directory- Subdirectories are automatically searched
-inamesearches file names (case insensitive),-nameis case sensitive'*text*'the text in the filename to search for.- You must use wildcards or only an exact match will be shown
- Use single quotes so the wildcards don't get misintrepreted (or precede each wildcard with a backsplash:
\*text\*)
find is very useful to execute a command on every found file.
For example, this will give the line count for every *.fasta file
find /path/to/dir -iname '*.fasta' -exec wc -l {} \;
Dissecting this:
find /path/to/dir -iname '*.fasta: finds all files ending in *.fasta starting in the directory specified-exec: start of the execute optionwc -l: the command we want to execute on found files{}: this is a special code that will be substituted with the found file\;: the-execoption must be ended with this
Note: you can only run a single command with -exec and you can't use pipes within that command, but there is a workaround using bash -c "...": https://stackoverflow.com/questions/21825393/how-to-use-pipe-within-exec-in-find/21825690#21825690
- Find directories:
find /path/to/dir -type d - Find files:
find /path/to/dir -type f - Find executable files (not in MacOS find):
find /path/to/dir -executable
- Files owned by the Unix user lab:
find /path/to/dir -user matt - Files owned by the Unix group staff:
find /path/to/dir -group staff
- Files changed within the last 15 days:
find /path/to/dir -mtime -15 - Files not changed in the last 15 dats:
find /path/to/dir -mtime +15
- There are logical operators like
-and,-or,-notto combine options and()to create precedence
By default every subdirectory is searched. You can limit how many levels down (depth) each directory is searched. This is very useful for directories with lots of files because find can be slow in those cases.
-maxdepthshould go after the path to the directory and before the other search options. Examples:find /path/to/dir -maxdepth 1 -iname 'dna001*': find files whose names start withdna001directly in/path/to/dirfind /path/to/dir -maxdepth 2 -iname 'dna001*': find files whose names start withdna001in/path/to/diror in its subfolders (but not the subfolder's, subfolders ๐)
- If you don't specify (or incorrectly speficy) what to search for, every file is returned. Don't do these:
find /path/to/dirorfind /path/to/dir *.fasta - Order of the options matters. This won't work:
find -iname '*.fasta' /path/to/dir - Putting
-maxdepthin the wrong place will generate a warning ๐find /path/to/dir -iname '*lostfile*' -maxdepth 2