find - search for files in a directory hierarchy
find [path...] [expression]
Options you will use
-name PATTERN- Match the base name against a shell glob. Quote it!
-iname PATTERN- Case-insensitive -name.
-type [fdl]- f regular file, d directory, l symlink.
-size +N[ckMG]- Size greater than N (- for smaller). -size +1G.
-mtime +N / -N- Modified more than / less than N*24 hours ago.
-mmin +N / -N- Modified more / less than N minutes ago.
-perm -MODE- All of these permission bits set. -perm -4000 finds setuid binaries.
-user NAME, -group NAME- Owned by a user or group.
-maxdepth N, -mindepth N- Limit the descent. Put these BEFORE other tests.
-empty- Empty file or directory.
-path PATTERN, -not, -o, -a, ( )- Path match, negation, or, and, grouping.
-print, -printf FORMAT- Print the name, or a custom format (%p path, %s size, %m octal mode, %u user).
-delete- Delete matching files. Test with -print FIRST.
-exec cmd {} ;- Run cmd once per file. With + instead of ; batch many files per run.
-L- Follow symbolic links (look at what they point to).
-xdev- Do not descend into directories on other file systems (other mounts).
-nouser- Files whose owner UID has no user name (a deleted user).
-nogroup- Files whose group GID has no group name.
-print0- Print the name followed by a NUL byte instead of a newline: safe with spaces, pairs with xargs -0.
-xtype TYPE- Like -type, but for a symlink tests what it points to; -xtype l finds broken links.
Examples
$ find /data -size +1G -type fthe space hogs
$ sudo find /srv/cache/sessions -type f -mtime +7 -deletesurvives "Argument list too long" where rm * does not
$ find . -type d -empty -not -path './.git*' -exec touch {}/.gitkeep ;make empty dirs visible to git
$ find /usr/bin -perm -4000 -type fevery setuid binary on the box
Gotchas
- Always quote the pattern: unquoted *.log is expanded by the shell first.
Taught in
Try find in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.