Say today is April 8th and I execute the following in bash.
cd /tmp
mkdir hello
touch -d 2015-04-01 hello
Then, say I want to delete all files in /tmp that are older than one day, but NOT directories and I execute this:
find /tmp -mtime +1 -delete -type f
Why is directory "hello" deleted if it's not a file?
Thanks!
The find command executes the expression in order. Since
-delete
is before-type
,-type
is never reached. Try:-mtime ...
.Calling them "options" is understandable, but since the very fact that they're not options is the cause of the problem,
find
's terminology and concepts warrant a closer look:-type f
)-delete
)-maxdepth 1
) - note that such options are distinct from the standard options that must come before even the input paths (e.g.,find -L /tmp ...
)find
terminology, which is helpfully more fine-grained than the one in the POSIX spec. forfind
, where all three constructs are called by a single name, primaries (BSDfind
also just uses primaries in itsman
page).-a
(-and
) for logical AND,-o
(-or
) for logical OR, and!
(-not
) for negation; the alternative forms in parentheses are not POSIX-compliant, but supported by GNU and BSD find.-a
).-a
and-o
apply short-circuiting (see below)\(
and\)
to alter precedence (the\
-escaping is to protect the parentheses from interpretation by the shell).\(...\)
,!
,-a
,-o
find
options, by contrast, are not positional, but GNUfind
by default issues a warning, if they aren't placed before tests and actions. To avoid the warning, and for conceptual clarity in general, it is better to do so.-a
implied - this means that subsequent test and actions are NOT evaluated, once a previous test or action has returned false:find . -false -print # !! -print is NOT executed
-o
(-or
) expression is NOT executed, if the 1st one has returned true:find . -print -o -print # !! 2nd -print is NOT executed