1

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 . 
2
  • @A.B Perhaps I should change the Title - it seems incorrect. They are not name-inode maps are they? Would be good to know what they are really called. Your first point is a good one as find is intended to navigate the tree in forward order only? Commented Dec 31, 2019 at 10:27
  • @A.B The crux of the question is that ls lists directory contents - so . and .. are contained within a directory but I don"t think they really are else find would list them too. Find only lists . but this is not really contained in the directory its just a notation for the current directory. A satisfactory answer would point to some formal specification which describes this. Commented Dec 31, 2019 at 10:34

1 Answer 1

3

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 <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 relative portion shall contain no dot or dot-dot components, no trailing <slash> characters, and only single <slash> characters between pathname components.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html

3
  • Well, find shows . like shown in the question. Commented Jun 6, 2020 at 10:41
  • 2
    @jarno "Each path operand shall be evaluated unaltered as it was provided", and the dot was provided by the user as the search path, therefore it needs to be evaluated. However, ./. is not evaluated. Commented Nov 19, 2024 at 9:45
  • 1
    Addendum: In the "naked find" case (no arguments), the dot It was provided by find as the search path since GNU find adds it as the default search path if the user does not specify a search path. Commented Nov 19, 2024 at 13:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.