1

When you want to get a sample of file names from a directory that contains many files, it is advised to do "ls -U | head", because otherwise doing "ls" alone can take quite some time.

However, why doesn't "ls -U" by it self does not start returning outputs as soon as it ran?

iddober
  • 113
  • 3

1 Answers1

1

GNU ls -U means unsorted output, so it does not need to wait for all the directory contents to start printing them.

head will close stdin and quit after a small number of lines. When the writer to the pipe, ls in this case, has no more readers, it will quit. This will flush all the I/O involved after only a small number of lines, making it seem snappy in interactive use.

Further reading: Process not closing when stdin is closed


A noticeable to humans delay in listing directory contents is a sign of exceeding practical limits. Lots of files in a directory means large file system metadata, and so a lot of I/O. Lots typically means hundreds of thousands of files, even for production tested file systems.

Beyond a certain number of files, it makes sense to reconsider alterative directory layouts and databases. Short term, put the problem volume on fast SSDs to make its performance tolerable.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34