LinuxCommandLibrary

find

TLDR

Find files by name

$ find [path] -name "[*.txt]"
copy
Find directories
$ find [path] -type d -name "[dirname]"
copy
Find and delete
$ find [path] -name "[*.tmp]" -delete
copy
Find by modification time
$ find [path] -mtime -[7]
copy
Find and execute command
$ find [path] -name "[*.log]" -exec rm {} \;
copy

SYNOPSIS

find [path...] [expression]

DESCRIPTION

find searches directory trees for files matching criteria. It's one of Unix's most powerful utilities, combining searching with file operations through -exec.
The tool evaluates expressions left to right, short-circuiting with -a (and) and -o (or). Actions like -print, -delete, and -exec operate on matches.
find's flexibility handles complex queries combining name patterns, timestamps, sizes, permissions, and ownership.

PARAMETERS

PATH

Starting directories (default: current).
-name PATTERN
Match filename pattern (glob).
-iname PATTERN
Case-insensitive name match.
-type TYPE
File type: f (file), d (directory), l (link).
-mtime N
Modified N days ago.
-size N
File size (c bytes, k KB, M MB).
-exec CMD {} \;
Execute command on results.
-delete
Delete matched files.
-maxdepth N
Maximum directory depth.
--help
Display help information.

CAVEATS

Glob patterns need quoting. -delete acts immediately without confirmation. Complex expressions need careful ordering.

HISTORY

find appeared in Unix Version 5 (1974). It remains the standard file-finding utility, though modern alternatives like fd offer simpler syntax for common cases.

SEE ALSO

fd(1), locate(1), xargs(1)

Copied to clipboard