Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • In contrast to find and ls -U the shell construct for name in * does sort the results before using them which is not needed here and thus a waste of time. Commented Aug 5, 2017 at 9:42
  • @HaukeLaging yes in order to count entries their order is irelevant so it is unneeded overhead that shell globing sorts the results. But in my tests (and I guess unless you have many entries to sort) the overhead to start the ls or find process is much bigger than the string sorting done by the shell. Commented Aug 5, 2017 at 10:12
  • this will cause annoying delays every time the shell prompt is displayed (e.g. on my reasonably fast system it takes 0.4 secs to run count_files in /usr/bin the first time, then 0.1 secs on subsequent runs). i'd suggest making count_files cache the file count unless called with -c (to clear the cache). then wrap cd, pushd, popd, etc so that they call count_files -c after changing directory. eliminates the delay unless changing directory, but disadvantage is that the cached value won't be current. Commented Aug 5, 2017 at 10:17
  • actually, i'd suggest not doing it at all - it doesn't provide anywhere near enough value to justify it....but people want strange things. Commented Aug 5, 2017 at 10:18
  • 5
    Your find ... | wc -l will get the wrong answer if any filename happens to contain a newline. Instead you can do find ... -printf '.' | wc -c so it will print a dot for each match, then count those dots. Commented Aug 5, 2017 at 10:46