Re:Show HN: Get lists of files in a directory that contains ...
by vidarh on Sunday August 29, 2021 @06:00AM
(#28345523)
"ls" without additional options just does getdents() on Linux (confirmed with strace to be sure). But "ls" without additional options loads the entire directory into memory and sorts the content (EDIT: It also tries columnar output, which probably also would make it read everything first). Give "-f" to "ls" on a large directory and it starts producing output right away. The total runtime probably won't be all that different, but in practice what people tend to get annoyed with when dealing with a large directory tends to be waiting for the initial output.
You're right that the moment you give an option to ls that requires stat calls, though, the stat calls tends to dominate.
You're right that the moment you give an option to ls that requires stat calls, though, the stat calls tends to dominate.