ls - list directory contents
empty_dir# ls -al
total 0
drwxr-xr-x. 2 root root 6 Dec 31 09:49 .
dr-xr-x---. 6 root root 284 Dec 31 09:49 ..
find - search for files in a directory hierarchy
empty_dir# find
.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityls - list directory contents
empty_dir# ls -al
total 0
drwxr-xr-x. 2 root root 6 Dec 31 09:49 .
dr-xr-x---. 6 root root 284 Dec 31 09:49 ..
find - search for files in a directory hierarchy
empty_dir# find
.
find
has to deliberately exclude .
and ..
It has to avoid descending into them, as it would do for other directories returned by readdir()
.
Rather than show the directories .
and ..
but not show any of their contents, it excludes them entirely.
This is the desired behaviour, for example if you used find -exec touch \{\} \;
. Users would not wish this command to affect ..
(the parent directory).
A satisfactory answer would point to some formal specification which describes this.
Arguably, POSIX is trying to document this. I don't understand well enough to rely on it as a formal spec. But the bolded sentence below suggests that it does not "encounter" .
and ..
.
The find utility shall recursively descend the directory hierarchy from each file specified by path, evaluating a Boolean expression composed of the primaries described in the OPERANDS section for each file encountered. Each path operand shall be evaluated unaltered as it was provided, including all trailing characters; all pathnames for other files encountered in the hierarchy shall consist of the concatenation of the current path operand, a if the current path operand did not end in one, and the filename relative to the path operand. The relative portion shall contain no dot or dot-dot components, no trailing characters, and only single characters between pathname components.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html