find

Walk a directory tree and print every file that matches the tests you give, such as a name, a type, a size or an age.

find <directory> -name "<pattern>"
Save

Angle brackets are yours to fill in. Linux, macOS.

Examples for find

  • Find a file by name

    find . -name "<name>"
    Save

    Searches the current directory and everything under it. Quote the name when it has a wildcard, such as "*.log", or the shell expands it before find sees it. Use -iname to ignore case.

    findtool .here and below-name "<name>"flag with value
  • Find all files with an extension

    find . -type f -name "*.log"
    Save

    -type f leaves out directories that happen to match. The quotes stop the shell from replacing *.log with the matching files in the current directory.

    findtool .path-type ffiles only-name "*.log"flag with value
  • Find files bigger than a size

    find . -type f -size +100M
    Save

    +100M means more than 100 MB. Use k for kilobytes and G for gigabytes. Add -exec ls -lh {} + to see the size of each.

    findtool .path-type fflag with value-size +100Mmore than 100 MB
  • Find files changed in the last day

    find . -type f -mtime -1
    Save

    -1 means less than one day ago. For a shorter time, use -mmin: -mmin -30 is the last 30 minutes.

    findtool .path-type fflag with value-mtime -1within 24 hours
  • Delete files that match

    find . -name "*.tmp" -delete
    Save

    Run it first without -delete to see what it will remove. Put -delete last: find reads left to right, and a -delete before -name deletes everything.

    Deletes without confirmation. Check the list first.

    findtool .path-name "*.tmp"flag with value-deletealways last
  • Run a command on every file found

    find . -name "*.sh" -exec chmod +x {} +
    Save

    {} stands for the found files. Ending with + passes many files to one command, which is faster. Ending with \; runs the command once per file, for commands that take only one.

    findtool .path-name "*.sh"flag with value-execflagchmod +xthe command to run{} +the files, in batches
  • Search but skip node_modules

    find . -path ./node_modules -prune -o -name "<name>" -print
    Save

    -prune stops find from going into the matched path; -o means or. The -print at the end is needed: without it, find also prints the pruned directory.

    findtool.-path ./node_modules -prunedo not go in here-oor-name "<name>"flag with value-printflag
  • Find empty files and directories

    find . -empty
    Save

    Add -type d for only empty directories, and -delete to remove them. find deletes the deepest first, so nested empty directories go too.

    findtool .path-emptyflag

Flags

-name <pattern>
Match the file name against a shell wildcard, such as *.log. Case-sensitive.
-iname <pattern>
As -name, but ignore case.
-type <f|d|l>
Match only files (f), directories (d), or symbolic links (l).
-size <n>
Match by size. +100M is more than 100 MB, -1k is less than 1 KB.
-mtime <days>
Match by days since last change. -7 is within the last week, +30 is older than 30 days.
-mmin <minutes>
As -mtime, in minutes.
-maxdepth <n>
Go no more than <n> directories down. 1 is the start directory only.
-path <pattern>
Match the whole path, not just the name. Use it with -prune to skip a directory.
-exec <command> ;
Run a command on each match. {} is the file name, and \; or + ends the command.
-delete
Delete each match.There is no confirmation and no undo. Run the same find without -delete first and read the list.
-empty
Match empty files and empty directories.