POSIXly:
find /test/. ! -name . -type d -mtime +0 -exec rm -rf {} \; -prune
(we use -prune
for the directories that we successfully remove so that find
doesn't complain that they're suddenly gone).
In any case, note that the modification time (as checked by -mtime
above) of a directory file only reflects the last time an entry was added, removed or renamed in it.
It is not updated when the content of any of the files (of type regular or directory or other) linked in it are modified. In particular, any change made to subdirectories or their content doesn't affect the modification time of a directory.
Note that all of -mindepth
, -maxdepth
and -mmin
are GNU extensions (though they are supported in some other implementations).
The standard equivalent of find . -maxdepth 1
would be:
find . -name . -o -prune
For -mindepth 1
:
find . ! -name .
For -mindepth 1 -maxdepth 1
:
find . ! -name . -prune
For directories other than .
, use find some/dir/. ...
as above.
For other values of depths, you can use -path
, but note that since it has only been recently added to the standard, some systems (like AIX) still don't have it.
For: -maxdepth 2
:
find . ! -path '*/*/*' -o -prune
For: -mindepth 2
:
find . -path '*/*/*'
For another dir:
find some/dir//. -path '*//*/*/*'
find /test/*
. – meuh Aug 27 '15 at 09:39