find 

Send to Kindle
home » snippets » linux » find



Gotchas

From: Wayne Pollock

If the given expression to find does not contain any of the “action” primaries ‑exec, -ok, or ‑print, the given expression is effectively replaced by:

find \( expression \) -print

The implied parenthesis can cause unexpected results.

For example, consider these two similar commands:

$ find -name tmp -prune -o -name \*.txt
./bin/data/secret.txt
./tmp
./missingEOL.txt
./public_html/graphics/README.txt
./datafile2.txt
./datafile.txt

$ find -name tmp -prune -o -name \*.txt -print
./bin/data/secret.txt
./missingEOL.txt
./public_html/graphics/README.txt
./datafile2.txt
./datafile.txt

The lack of an action in the first command means it is equivalent to:

find . \( -name tmp -prune -o -name \*.txt \) -print

This causes tmp to be included in the output. However for the second find command the normal rules of Boolean operator precedence apply, so the pruned directory does not appear in the output.

Snippets

# Find files modified more than a day ago.
find . -mtime +1

# Find files that are readable by everyone.
find . -perm 644

# Case insensitive.
find . –iname NAME

# Files in the current directory.
find . -maxdepth 1 -type f

# Symlinks in the current directory.
find . -maxdepth 1 -type l

# Find files > 1024 bytes in length.
find . +size +1024c

# Find files < 1024 bytes in length.
find . -size +1024c



# Delete old log files.
find . -maxdepth 1 -iname '*.log' -ctime +10 -mtime +2 -print0 | xargs -0 rm

# -n 1 == only pass 1 param to the command – in this case rm
# -P 8 == run up to 8 invocations of the process in parallel.
find . -maxdepth 1 -iname '*.log' -ctime +10 -mtime +2 -print0 | xargs -0 -n 1 -P 8 rm