Skip to content

Instantly share code, notes, and snippets.

@mkweskin
Last active November 8, 2018 19:02
Show Gist options
  • Save mkweskin/b6a57b57fbfe6fe6e7329da61be190c1 to your computer and use it in GitHub Desktop.
Save mkweskin/b6a57b57fbfe6fe6e7329da61be190c1 to your computer and use it in GitHub Desktop.
Linux `find` basics

Linux find

find is used to find files based on their name or other attributes of the file.

  • It does not search inside files like the grep command
  • To find find examples for a speicific purpose, I do a web search for: linux find ...

Basic find command: search for a filename

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
  • -iname searches file names (case insensitive), -name is 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\*)

Executing a command with the found files

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 option
  • wc -l: the command we want to execute on found files
  • {}: this is a special code that will be substituted with the found file
  • \;: the -exec option 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

Other things you can find besides name (far from an exhaustive list)

File type

  • 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

Owned by a user or group

  • 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

Modification times

  • 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

Combining options

  • There are logical operators like -and, -or, -not to combine options and () to create precedence

limiting depth of search -maxdepth int

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.

  • -maxdepth should 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 with dna001 directly in /path/to/dir
  • find /path/to/dir -maxdepth 2 -iname 'dna001*': find files whose names start with dna001 in /path/to/dir or in its subfolders (but not the subfolder's, subfolders 😛)

Common errors

  • If you don't specify (or incorrectly speficy) what to search for, every file is returned. Don't do these: find /path/to/dir or find /path/to/dir *.fasta
  • Order of the options matters. This won't work: find -iname '*.fasta' /path/to/dir
  • Putting -maxdepth in the wrong place will generate a warning 😞 find /path/to/dir -iname '*lostfile*' -maxdepth 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment