linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Commands/cmd-find

kb/commands ── Commands ── beginner

find: search files by predicates

`find` walks a directory tree and applies predicates (name, type, time, size, permissions). Actions: `-print` (default), `-delete`, `-exec`, `| xargs`.

view as markdownaka: find-command, find-files

Basic syntax

find [PATH...] [EXPRESSION]

If PATH is omitted, the current directory is used. EXPRESSION is a chain of predicates and actions joined by an implicit -and.

bash
find .                       # all files and directories from .
find /var/log -type f        # files only (not directories)
find ~ -name '*.log'         # by name, glob pattern
find / -maxdepth 2 -type d   # directories, at most 2 levels deep

Type predicates

predicatemeaning
-type fregular file
-type ddirectory
-type lsymlink (see symbolic-link)
-type ssocket
-type pnamed pipe (FIFO)
-type b / -type cblock / character device

Name and path

bash
find . -name '*.py'              # name by glob
find . -iname '*.PY'             # like -name, but case-insensitive
find . -path '*/tests/*.py'      # full path by glob
find . -regex '.*/test_[0-9]+\.py'   # POSIX regex against the full path

Quotes are required. Without them, the shell expands * before find sees it.

By time

Time in find is measured in days for the base flags and in minutes for -mmin/-amin/-cmin. Signs: -N (less than N), +N (more than N), N (exactly N).

flagwhat it matches
-mtime Nmodified N days ago
-atime Nlast accessed N days ago
-ctime Nmetadata changed N days ago (see inode)
-mmin Nmodified N minutes ago
-newer FILEnewer than FILE
bash
find /var/log -type f -mtime -1       # modified in the last 24 hours
find /tmp -type f -mtime +7           # older than a week, candidates for cleanup
find . -mmin -10                       # touched in the last 10 minutes
find . -newer Makefile                 # everything newer than Makefile

By size

Suffixes: c (bytes), k (KiB), M (MiB), G (GiB).

bash
find . -type f -size +100M             # files larger than 100 MiB
find /var/log -type f -size +10M -size -100M   # 10..100 MiB
find . -type f -empty                  # empty files
find . -type d -empty                  # and empty directories

By permissions and ownership

bash
find . -type f -perm -u+x              # execute bit set for the owner
find . -perm -4000                     # SUID files (see [[file-permissions]])
find /etc -user root -group root
find /home -nouser -o -nogroup         # orphaned UID/GID entries

Actions

By default find prints paths (-print). Other options:

bash
find . -name '*.tmp' -delete                     # delete (built-in action)
find . -name '*.log' -exec gzip {} \;            # exec once per file
find . -name '*.log' -exec gzip {} +             # batch (faster, like xargs)
find . -type f -print0 | xargs -0 wc -l          # classic pattern with -print0

The difference between \; and +:

  • \; runs the command once per file. With thousands of files this is slow.
  • + collects arguments into a batch and runs the command fewer times. This is the right default choice.

-print0 + xargs -0

If filenames can contain spaces or newlines, plain xargs will break. The null-delimiter approach handles that:

bash
find . -type f -name '*.log' -print0 | xargs -0 grep -l 'ERROR'

This finds all .log files that contain ERROR. It is a common pairing with cmd-grep.

Logic

bash
find . \( -name '*.tmp' -o -name '*.bak' \) -delete   # OR via -o, parentheses must be escaped
find . -name '*.py' -not -path '*/venv/*'             # NOT via -not (or !)
find . -type f -name '*.log' -size +1M                # implicit AND

fd as a modern alternative

fd (the fd-find package) is a simpler wrapper with better defaults: it respects .gitignore, outputs color, supports parallel execution, and uses -x for exec. When it is available, fd '\.py$' src is shorter and faster. That said, find ships everywhere out of the box, so knowing its basics is worth the time.

§ команды

bash
find . -type f -name '*.log'

All files (not directories) with the .log extension, starting from the current directory

bash
find /var/log -type f -mtime -1 -size +1M

Logs modified in the last day and larger than 1 MiB: what has grown recently

bash
find . -name '*.tmp' -delete

Clean up by pattern. `-delete` is a built-in action; no `-exec` needed

bash
find . -type f -print0 | xargs -0 grep -l 'TODO'

Safe with spaces in filenames: find all files that contain TODO

bash
find . -type f -perm -4000

SUID files for a security audit (see [[file-permissions]])

bash
find . -type d -empty -delete

Delete empty directories (recursive, bottom-up)

§ см. также

  • cmd-grepgrep: search lines by pattern`grep` searches stdin or files for lines matching a regex. Key modes: `-E` (ERE), `-P` (PCRE), `-F` (fixed string), `-r` (recursive tree walk).
  • cmd-sedsed: stream editorsed is a stream editor: it applies commands (`s/a/b/`, `d`, `p`, ...) to each line. `-i` edits a file in place; `-E` enables ERE; the address range `/start/,/end/` filters a block. Hold space is a second buffer.
  • cmd-rsyncrsync: incremental file synchronizationrsync copies only the changed blocks of files, locally or over SSH. `-avz` is the baseline combination (archive + verbose + compress). `--delete` mirrors. `--dry-run` is required before the first run.
  • xargs-and-find-execxargs and find -exec: bulk operationsTwo ways to apply a command to a set of files: `find ... -exec cmd {} +` (inside find) and `... | xargs cmd` (via pipe). For safety with spaces and special characters, use `find -print0 | xargs -0`.
  • inodeInodeAn inode is a filesystem record that holds metadata and pointers to a file's data blocks. The filename lives separately, in a directory, and simply points to the inode.
  • file-permissionsFile permissions: rwx and chmodEvery file has three permission sets: for the owner, the group, and others. Each set is three bits: read (r), write (w), execute (x). You change them with `chmod`.

§ упоминается в уроках

  • ›beginner-11-find-and-grep
  • ›intermediate-10-xargs-and-parallel
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies