Please help me to build the right syntax.
What you used:
find . -type f -mtime +6 -name '*[0-9]' -print -delete
is the right syntax, with few caveats:
-delete
is not portable. In general, implementations offind
may or may not support it. The portable equivalent is-exec rm {} \;
.-name
uses glob-like patterns. Depending on your locale,[0-9]
may or may not mean exactly what you want: it may match a character from a set different than just ASCII decimal digits (e.g. it may match𝟛
, but then maybe it doesn't match𝟡
at the same time); and it's possible to create a locale where[0-9]
cannot match anything. If you want to match any character your locale considers a decimal digit, use[[:digit:]]
. If you want to match a character being one of the ASCII decimal digits, nothing more, then use[0123456789]
explicitly.
The reason your command did not work for you is -mtime +6
did not match any of the files in question. The files were modified* on August 7, some on August 5. The question was posted on August 8. On that day the files were too recent to match -mtime +6
. This has nothing to do with the syntax.
* I assume the example list of files is from ls -l
, so it shows modification times.
That other answer advises replacing -name '*[0-9]'
with -regex '^.*[0-9]$'
. -regex
is not portable, but where it works, -regex '^.*[0-9]$'
is almost equivalent to -name '*[0-9]'
(almost, because e.g. that -regex
cannot match files with pathnames containing a newline character, while that -name
can; such pathnames are rare though). [0-9]
in -regex
is similarly locale-dependent as [0-9]
in -name
.
That answer replaced your right syntax with a similarly right syntax.