Skip to main content

Pruning Directories

When executing find against the root partition / or large application directories, the tool will inevitably stumble into high-latency paths, network mounts (NFS), or massive caches (like node_modules).

Waiting for find to crawl 500,000 irrelevant files wastes CPU, I/O, and time. The solution is the -prune action.

The -prune Action Explained

-prune is an action that tells find: "If the current file is a directory, do not descend into it."

Because it is an action, it must be paired with an OR (-o) statement to instruct find on what to do with the files that are not pruned.

The Canonical Prune Syntax

The standard pattern for pruning looks like this:

find [path] -path [exclude_pattern] -prune -o [search_pattern] -print
  1. -path [exclude_pattern]: Check if the current object matches the forbidden path.
  2. -prune: If it does match, do not enter it. (This action returns true).
  3. -o: OR... (if the left side was false, i.e., it wasn't the forbidden path).
  4. [search_pattern] -print: Run our actual search and print.

Practical Pruning Examples

1. Skipping node_modules

Developers frequently need to search source code but avoid deep dependency folders.

find /projects -name "node_modules" -prune -o -name "*.js" -print

2. Skipping Multiple Directories

Use grouping \( ... \) to specify multiple paths to prune.

# Skip /var/cache, /proc, and /sys
find / -type d \( -path /var/cache -o -path /proc -o -path /sys \) -prune -o -name "nginx.conf" -print

3. Skipping Hidden Directories (e.g., .git)

To skip all directories that start with a dot (like .git, .idea, .venv), use a regex or path wildcard.

# Prune any directory starting with a dot
find /projects -type d -name ".*" -prune -o -type f -name "*.py" -print

The -prune vs ! Pitfall

A common mistake is trying to exclude directories using NOT (!).

WRONG:

find /projects ! -path "*/node_modules/*" -name "*.js"

Why this is bad: find will still enter the node_modules directory, read every single inode inside it, test it against the ! condition (which returns false), and discard it. It does not save any I/O. It just hides the results.

-prune physically stops find from crossing the directory boundary, saving massive amounts of traversal time.