98

I want to see how many files are in subdirectories to find out where all the inode usage is on the system. Kind of like I would do this for space usage

du -sh /*

which will give me the space used in the directories off of root, but in this case I want the number of files, not the size.

xenoterracide
  • 59,188
  • 74
  • 187
  • 252

12 Answers12

120
find . -maxdepth 1 -type d | while read -r dir
do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done

Thanks to Gilles and xenoterracide for safety/compatibility fixes.

The first part: find . -maxdepth 1 -type d will return a list of all directories in the current working directory.  (Warning: -maxdepth is a GNU extension and might not be present in non-GNU versions of find.)  This is piped to...

The second part: while read -r dir; do (shown above as while read -r dir(newline)do) begins a while loop – as long as the pipe coming into the while is open (which is until the entire list of directories is sent), the read command will place the next line into the variable dir. Then it continues...

The third part: printf "%s:\t" "$dir" will print the string in $dir (which is holding one of the directory names) followed by a colon and a tab (but not a newline).

The fourth part: find "$dir" -type f makes a list of all the files inside the directory whose name is held in $dir. This list is sent to...

The fifth part: wc -l counts the number of lines that are sent into its standard input.

The final part: done simply ends the while loop.

So we get a list of all the directories in the current directory. For each of those directories, we generate a list of all the files in it so that we can count them all using wc -l. The result will look like:

./dir1: 234
./dir2: 11
./dir3: 2199
...
Shawn J. Goff
  • 46,081
  • Always use read -r as plain read treats backslashes specially. Then echo -en "$dir:\t" will again mangle backslashes; a simple fix is to use printf '%s:\t' "$dir" instead. Next, $dir should be "$dir" (always use double quotes around variable substitutions). – Gilles 'SO- stop being evil' Nov 18 '10 at 01:40
  • modified per @Giles suggestions find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" | wc -l; done – xenoterracide Nov 18 '10 at 09:30
  • 3
    I'm adding sort -n -r -k2 to the end of this, for lots of directories, so that I know where the most usage is – xenoterracide Jan 25 '11 at 10:45
  • The fourth part: find "$dir" makes a list of all the files inside the directory name held in "$dir". You forgot to add -type f to make it list files: find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done – Krzysztof Boduch Jun 20 '14 at 12:02
  • @krzysiek-boduch Thanks! I updated the answer. – Shawn J. Goff Jun 20 '14 at 20:40
  • where do I write the name of the root directory? – Charlie Parker Jul 19 '17 at 19:57
  • 1
    I'm not getting this to work on macOS Sierra 10.12.5. illegal option -- m in the find command. I changed it to find . -maxdepth ... and got it to work. – Jeff Jul 24 '17 at 13:28
  • @xenoterracide you might have dirs with spaces, maybe better setting tab as delimiter sort -rt $'\t' -k 2,2 -V. Also 2>/dev/null after wc pipe to get rid of permission errors. – Pablo A Sep 27 '17 at 05:29
  • Why do you count only files? Directories take inodes as well as files. And well, the directory itself also takes an inode, but that doesn't affect the result much. Anyways, the other answer must be top-most and accepted. – x-yuri Apr 25 '19 at 13:27
  • On Mac OS, it also counts the .DS_Store inside each directory. You can exclude them with the -type f ! -iname ".*" find parameter. You could also want to exclude files starting by ~*. – sglessard Feb 04 '20 at 16:26
77

Try find . -type f | wc -l, it will count of all the files in the current directory as well as all the files in subdirectories. Note that all directories will not be counted as files, only ordinary files do.

22

Here's a compilation of some useful listing commands (re-hashed based on previous users code):

List folders with file count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find "$dir" -type f | wc -l); printf "%4d : %s\n" $n "$dir"; done

List folders with non-zero file count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find "$dir" -type f | wc -l); if [ $n -gt 0 ]; then printf "%4d : %s\n" $n "$dir"; fi; done

List folders with sub-folder count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find "$dir" -type d | wc -l); let n--; printf "%4d : %s\n" $n "$dir"; done

List folders with non-zero sub-folder count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find "$dir" -type d | wc -l); let n--; if [ $n -gt 0 ]; then printf "%4d : %s\n" $n "$dir"; fi; done

List empty folders:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find "$dir" | wc -l); let n--; if [ $n -eq 0 ]; then printf "%4d : %s\n" $n "$dir"; fi; done

List non-empty folders with content count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find "$dir" | wc -l); let n--; if [ $n -gt 0 ]; then printf "%4d : %s\n" $n "$dir"; fi; done
16

Try:

find /path/to/start/at -type f -print | wc -l

as a starting point, or if you really only want to recurse through the subdirectories of a directory (and skip the files in that top level directory)

find `find /path/to/start/at -mindepth 1 -maxdepth 1 -type d -print` -type f -print | wc -l
Cry Havok
  • 2,048
  • +1 for something | wc -l ... word count is such a nice little tool – Johan Nov 16 '10 at 12:32
  • yeah but this only does 1 directory.... I'd like to get the count for all directories in a directory, and I don't want to run it seperately each time... of course I suppose I could use a loop... but I'm being lazy. – xenoterracide Nov 16 '10 at 12:49
  • 1
    find works recursively through all sub directories by default. If you want it to work in multiple locations, you can specify all of them between find and -type. – Didier Trosset Nov 16 '10 at 14:33
  • that second one certainly doesn't work.... I tried it on /home . I got 698035. I should see about 6 numbers. – xenoterracide Nov 16 '10 at 21:02
  • It works for me - are you sure you only have 6 files under /home? I'd be 100% certain you don't. – Cry Havok Nov 17 '10 at 17:28
  • @Cry I have maybe 6 directories... not 6 files – xenoterracide Nov 18 '10 at 07:51
11

du --inodes

I'm not sure why no one (myself included) was aware of:

du --inodes
--inodes
      list inode usage information instead of block usage

I'm pretty sure this solves the OP's problem. I've started using it a lot to find out where all the junk in my huge drives is (and offload it to an older disk).

Further info

If you DON'T want to recurse (which can be useful in other situations), add

-S, --separate-dirs
Sridhar Sarnobat
  • 1,802
  • 20
  • 27
7

If you have ncdu installed (a must-have when you want to do some cleanup), simply type c to "Toggle display of child item counts". And C to "Sort by items".

Demi-Lune
  • 194
4

The following solution counts the actual number of used inodes starting from current directory:

find . -print0 | xargs -0 -n 1 ls -id | cut -d' ' -f1 | sort -u | wc -l

To get the number of files of the same subset, use:

find . | wc -l

For solutions exploring only subdirectories, without taking into account files in current directory, you can refer to other answers.

mouviciel
  • 1,235
  • 2
    Good idea taking hard links into account. Assuming GNU find, you don't need so many steps: find -printf '%i\n' | sort -u | wc -l. If you wanted to be portable, you'd need find . -exec ls -id {} + | cut … instead. – Gilles 'SO- stop being evil' Nov 19 '10 at 20:24
2

OS X 10.6 chokes on the command in the accepted answer, because it doesn't specify a path for find. Instead use:

find . -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done
2

I know I'm late to the party, but I believe this pure bash (or other shell which accept double star glob) solution could be much faster in some situations:

shopt -s globstar    # to enable ** glob in bash
for dir in */; do a=( "$dir"/**/* ); printf "%s\t%s\n" "$dir:" "${#a[*]}"; done

output:

d1/:    302
d2/:    24
d3/:    640
...
jimmij
  • 47,140
2

Give this a try:

find -type d -print0 | xargs -0 -I {} sh -c 'printf "%s\t%s\n" "$(find "{}" -maxdepth 1 -type f | wc -l)" "{}"'

It should work fine unless filenames include newlines.

  • way too recursive... I only want to see the top level, where it totals everything underneath it. totaled... this ends up printing every directory. – xenoterracide Nov 16 '10 at 21:05
  • @xenoterracide: Try adding -maxdepth 1 immediately after the first find. If you want to include the number of subdirectories in your count, remove the -type f at the end (that should have really been ! -type d anyway, so that all non-directory files would have been included). – Dennis Williamson Nov 16 '10 at 23:15
0

Use this recursive function to list total files in a directory recursively, up to a certain depth (it counts files and directories from all depths, but show print total count up to the max_depth):

#!/bin/bash
# set -x

export max_depth="2" export found_files="/tmp/found_files.txt"

function get_all_the_files() { depth="$1"; base_directory="$2";

if [[ "$depth" -ge "$max_depth" ]];
then
    return;
fi

find "$base_directory" -maxdepth 1 -type d | while read -r inner_directory
do
    printf "%s\t%s\n" "$(find "$inner_directory" | wc -l)" "$inner_directory" | tee -a "$found_files";
    if [[ "w$(realpath "$base_directory")" != "w$(realpath "$inner_directory")" ]];
    then
        get_all_the_files "$(( depth + 1 ))" "$inner_directory";
    fi;
done;

}

rm -f "$found_files" get_all_the_files 0 /tmp/

printf '\nFinished searching files, sorting all:\n' sort --version-sort "$found_files"

user
  • 781
0

Inside the folder (directory) you want to count (cd /my/dir) you can do the following:

  1. To count all folders and files: find . | wc -l
  2. To count only folders (directories): find . -type d | wc -l
  3. To count only files: find . -type f | wc -l

This way you are able to verify that: folders (2) + files (3) = total (1)


Some explanation:

  • The command find . will print each of all the folders (directories) and files line by line.
    • The parameter: -type d says to print only directories (folders)
    • The parameter: -type f says to print only files.
  • The command wc -l should mean something like: word count with the parameter: lines.