so I have the following script

find "my-path-here"  -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo "{} : $(find "{}" -type f | wc -l | sort)" file\(s\)' \;

and it counts the files (recursively) on the all the folders (depth=1)

The screenshot below was run against /usr and as can be seen, the folders are not sorted alphabetically.

Any ideas on how to change the command so it comes out alphabetically sorted?


enter image description here


One way to do it, assuming GNU find and assuming your filenames don't have embedded newlines:

find /some/dir -type f -printf '%P\n' | grep / | cut -d/ -f1 | sort | uniq -c

Sample output:

 5322 X11R6
  316 bin
   89 lib
 2165 libdata
   50 libexec
19220 local
   10 mdec
  206 sbin
 8970 share

You can use awk or sed to format this if you need to.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.