With your original command, it's not actually find
that fails to match anything but your shell.
Ie, when you run
find /tmp/my_files_*.cache -mtime +1 -delete
If there were matching files, the shell's globbing functionality expanded that wildcard and the actual find
invocation would look something like:
find /tmp/my_files_1.cache /tmp/my_files_2.cache /tmp/my_files_3.cache -mtime +1 -delete
(this could easily become a very long command, which in some cases can be a problem in itself)
When there are no matches, the wildcard is passed as-is, so the find
invocation would look like:
find /tmp/my_files_*.cache -mtime +1 -delete
and behaving like most programs find
does not do any sort of wildcard expansion on its own for the path argument(s), so it tries to find a file/directory that is literally named /tmp/my_files_*.cache
, hence the error.
As was pointed out by Ipor Sircer, the more idiomatic way of using find
is to instead give it the base path(s) and let find
itself look through the directory contents and apply filters as needed.
Something like this should match what you were trying to do:
find /tmp -maxdepth 1 -name "my_files_*.cache" -mtime +1 -delete