0

My total file count was too high on the server where I pay for hosting. I needed to find a way to count the number of files in each sub-folder, so that I could focus my attention in areas where there are a lot of files.

I couldn't find a single answer that showed me how to do what I was trying to do, so I thought I'd post my question and then answer it so others can use it.

My question is:

How can I cycle through the sub-directories in my current directory and create a list that shows the number of recursive files in each sub-directory, sorted by file count?

2
du --inodes ${topdir} 

will list the number of inodes used by each directory (and its subdirectories). You may want to easily spot the biggest offenders:

du --inodes ${topdir} | sort -n

The directories at the bottom will be the bigger ones (and the last one will be directory you passed to du (${topdir} in the commands above).

#>du --inodes /usr/share/gimp | sort -n
1       /usr/share/gimp/2.0/fonts
2       /usr/share/gimp/2.0/tags
2       /usr/share/gimp/2.0/themes/Small
2       /usr/share/gimp/2.0/tips
3       /usr/share/gimp/2.0/tool-presets/Selection
3       /usr/share/gimp/2.0/ui/plug-ins
4       /usr/share/gimp/2.0/dynamics/FX
4       /usr/share/gimp/2.0/images
4       /usr/share/gimp/2.0/ui
6       /usr/share/gimp/2.0/brushes/Splatters
6       /usr/share/gimp/2.0/scripts/images
7       /usr/share/gimp/2.0/tool-presets/Sketch
8       /usr/share/gimp/2.0/brushes/Legacy
8       /usr/share/gimp/2.0/dynamics/Basic
8       /usr/share/gimp/2.0/gflare
9       /usr/share/gimp/2.0/brushes/Sketch
9       /usr/share/gimp/2.0/gradients/gimp-obsolete-files
9       /usr/share/gimp/2.0/tool-presets/FX
10      /usr/share/gimp/2.0/gimpressionist/Paper
11      /usr/share/gimp/2.0/brushes/Basic
11      /usr/share/gimp/2.0/gfig
12      /usr/share/gimp/2.0/brushes/Media
13      /usr/share/gimp/2.0/brushes/Texture
17      /usr/share/gimp/2.0/tool-presets/Paint
21      /usr/share/gimp/2.0/dynamics
26      /usr/share/gimp/2.0/gimpressionist/Presets
32      /usr/share/gimp/2.0/menus
34      /usr/share/gimp/2.0/fractalexplorer
37      /usr/share/gimp/2.0/tool-presets
41      /usr/share/gimp/2.0/brushes/gimp-obsolete-files
41      /usr/share/gimp/2.0/palettes
59      /usr/share/gimp/2.0/patterns
61      /usr/share/gimp/2.0/gimpressionist/Brushes
65      /usr/share/gimp/2.0/themes/Default/images/preferences
70      /usr/share/gimp/2.0/themes/Default/images
72      /usr/share/gimp/2.0/themes/Default
75      /usr/share/gimp/2.0/themes
80      /usr/share/gimp/2.0/gradients
98      /usr/share/gimp/2.0/gimpressionist
101     /usr/share/gimp/2.0/brushes
270     /usr/share/gimp/2.0/scripts
881     /usr/share/gimp/2.0
882     /usr/share/gimp
2
  • --inodes is not supported for du on my dist of linux (Ubuntu). – James L. May 14 '18 at 18:37
  • It is on my Ubuntu 16.04 (du (GNU coreutils) 8.25) – xenoid May 14 '18 at 20:42
1

I created a file in my home folder called filecnt, which contains the following lines of code:

for d in *; do
  if [[ -d $d ]]; then
    echo `find $d -type f | wc -l` $d;
  fi
done | sort -n -k1

Then I changed the file permissions to allow execute:

chmod 755 ~/filecnt

Now, from any directory, I can run ~/filecnt to see a list of sub-directories in the current directory with their recursive file counts. The list is sorted by file count (ascending). For example:

0 access-logs
20 logs
187 etc
232 cache
694 tmp
30007 mail
48325 public_html

You can accomplish the same by simply running the following command from the CLI:

for d in *; do if [[ -d $d ]]; then echo `find $d -type f | wc -l` $d; fi done | sort -n -k1

Add -r to the final sort command to sort descending.

To produce a slightly more pleasing output, you can also use the following code in ~/filecnt instead of the code at the top of this answer:

echo "File Count    Dir Size  Directory"
echo "----------  ----------  -------------------------"
for d in *; do
  if [[ -d $d ]]; then
    echo `find $d -type f | wc -l` | awk '{printf "%10s  ", $0;}'
    echo `du -h --max-depth=0 $d` | cut -d\  -f 1 $1 | awk '{printf "%10s  ", $0;}'
    echo $d
  fi
done | sort -n -k1
echo

The output looks like this:

File Count    Dir Size  Directory
----------  ----------  -------------------------
         0           0  access-logs
        20        8.3M  logs
       187        4.8M  etc
       232        228K  cache
       694         23M  tmp
     30715        6.4G  mail
     48272        2.3G  public_html
0

You may also like to look at "tree" The last line lists how many files and directories were present.

> tree | tail -1
4449 directories, 62681 files

It should be just as fast as "find".

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.