WillSquire
8/6/2015 - 2:38 PM

Count directory files with BASH

Count directory files with BASH

Count directory files with BASH

Useful for helping to lower the dcachesize strain on the server (finding where too many files are and deleting them).

To list how many files there are in each sub directory in the currently directory path:

for ii in $(find -maxdepth 1 -type d); do echo -e "${ii}\t$(find "${ii}" -type l -o -type d -o -type f | wc -l)"; done | sort -n -k 2 | column -t

If permissions are needed (i.e. the user needs to use sudo), then wrap the function in sudo bash -c '<commands>', like so:

sudo bash -c 'for ii in $(find -maxdepth 1 -type d); do echo -e "${ii}\t$(find "${ii}" -type l -o -type d -o -type f | wc -l)"; done | sort -n -k 2 | column -t'