This behaviour comes from
find, and is specified by POSIX:
Each path operand shall be evaluated unaltered as it was provided, including all trailing <slash> characters; all pathnames for other files encountered in the hierarchy shall consist of the concatenation of the current path operand, a <slash> if the current path operand did not end in one, and the filename relative to the path operand.
The default action,
-print, outputs the full pathname to standard out.
find outputs the paths of files it finds starting from the path(s) given on its command line.
find to look for files under
. and its subdirectories, and it presents the results starting with
would do the same but starting with
foo, and it would produce results starting with
I don’t think
find does this specifically to prevent problems with un-prefixed file names; rather, it does this for consistency — regardless of the path provided as argument, the output of
-print always starts with that path.
With the GNU implementation of
find, you can strip the initial path off the start of the printed file by using
-printf '%P\n' in place of
-print. For instance with
find foo/bar -name file -printf '%P\n' or
find . -name file -printf '%P\n', you'd get
dir/file instead of
./dir/file for those files.
More generally, having
./ as a prefix can help prevent errors, e.g. if you have files with names starting with dashes; for example if you have a file named
rm -f won’t delete it, but
rm ./-f will.
When running commands with a shell or with
exec*p() standard C functions (and their equivalent in other languages), when the command name doesn't contain a
/, the path of command is looked in
$PATH instead of being interpreted as a relative path (the file in the current working directory). Same applies for the argument to the
source special builtins of several shells (including POSIX compliant
sh implementations). Using
./cmd in that case instead of
cmd, which is another way to specify the same relative path, but with a
/ in it is how you typically invoke a command stored in the current working directory.