NAME
find - walk a path and name what is under it
SYNOPSIS
find [path ...] [-maxdepth n] [-name glob] [-type f|d|l] [-print] [-exec command {} \;]
DESCRIPTION
Walks a path and names everything at or below it, one to a line, beginning
with the path itself. With nothing to go on it walks from where you are
standing, so find on its own is everything under you — and man hier is the
map of what the rest of it is.
The paths come first and the expression after them, which is find’s one piece
of grammar: find ~ /tmp -name '*.md' walks two places, and
find . -name '*.md' writing is the mistake find has a whole sentence
about.
Every path it prints is a link. A directory takes the shell there, a note
opens, a file you made opens the page that reads it back out of this browser,
and the machine’s own files are plain text because they have no page —
man cat for those.
Walking
It walks in the order ls lists things, depth first, printing as it goes.
A directory it may not read it names and does not go into:
$ find /var
/var
/var/empty
/var/log
find: '/var/log': Permission denied
then it carries on with the rest and answers 1 at the end, because what it
could not read is an error rather than an absence. -maxdepth is a promise
it keeps while walking rather than a filter afterwards, so it never goes
deeper than you said.
Every test you give has to be true; there is no -o.
-exec
Runs a command for each result — a command, not a shell line. The words go
straight to the shell, so there is no pipe, no redirection and no second round
of expansion inside them. {} stands for the path, and the \; that ends it
is written with a backslash because a bare ; is the shell’s own: it would
end the find line before find ever saw it.
find ~ -name '*.md' -exec wc -l {} \;
An action replaces the printing, as it does in real find: that line prints
what wc prints and nothing else, and -print beside it asks for both. The
shell’s ceiling on how much one line may run applies here too — see man sh.
What is not here
-delete, because rm is right there and nothing here is precious enough to
need two ways of going. -size and -newer, because a note is measured in
reading time and ls -l already says when it changed. -exec ... +, which
batches, when there is nothing here big enough to need it. And -o, -not
and parentheses: an expression here is a list of things that must all be true.
OPTIONS
- -name glob
- the name matches this pattern -- *, ? and [abc], the same ones the shell matches with
- -iname glob
- the same, without caring about case
- -type
- f a file, d a directory, l a symlink -- and there are real symlinks here to find
- -maxdepth n
- how far down to walk: 0 is the path itself, 1 is what is in it
- print the path, which is what find does anyway until you give it something else to do
- -exec
- run a command for each one, with {} standing for the path and a \; ending the command
EXAMPLES
find
find ~ -name '*.md'
find / -maxdepth 1 -type d
find ~ -name '*.md' -exec wc -l {} \;