Skip to main content

Print and Delete Actions

Once find evaluates an expression to true, it executes an action. If you don't specify one, find defaults to -print.

Output Formatting (-print vs -print0)

1. -print (Default)

Prints the matched filepath followed by a standard newline character (\n).

find . -name "*.txt" -print

The Danger of -print Standard newlines are problematic if a filename contains spaces, tabs, or its own newline characters (which is legally permissible in Unix filesystems). If you pipe the output of -print to another command (like xargs or a while read loop), the pipeline will break when it encounters a space, treating "My Document.txt" as two separate files: "My" and "Document.txt".

2. -print0 (The Safe Way)

Instead of a newline, -print0 separates each output string with a null byte (\0). Because null bytes are the only character strictly forbidden in Linux filenames, this guarantees 100% safe parsing downstream.

Always pair -print0 with xargs -0.

# Safe pipeline, regardless of spaces in filenames
find . -name "*.txt" -print0 | xargs -0 rm

Built-in Deletion (-delete)

Historically, administrators piped find to rm or used -exec rm {} \;. Modern GNU find includes -delete, a built-in action that calls the unlink() system call directly.

Advantages of -delete

  1. Massively Faster: It avoids spawning a new rm process for every single file.
  2. Safer: It automatically implies -depth (processes directory contents before the directory itself) to ensure directories can be safely removed.
  3. Strict: It will refuse to delete non-empty directories.

Usage and Warnings

# Delete all .tmp files
find /var/app -type f -name "*.tmp" -delete
Position Matters

-delete is an action that always returns true. If you place it at the beginning of your expression, it will delete files before testing their names or metadata.

NEVER DO THIS: find /var/log -delete -name "*.log" DO THIS: find /var/log -name "*.log" -delete

Best Practice: The Dry Run

Before appending -delete to a complex query, run the exact command but substitute -delete with -print to verify the blast radius.

# 1. Dry run (verify what will be deleted)
find /backups -type f -mtime +90 -print

# 2. Execute
find /backups -type f -mtime +90 -delete