30

I need to organise an external HDD such that there is no more than 500 folders on it. Ubuntu's "Properties" pane shows only the file count, not the folder count.

Is there a simple command line that will tell me the number of subdirectories?

I need to count recursively, and the drive is an external HDD mounted at /media/MUSIC/. It's for a car stereo system whose documentation says it only reads the first 500 folders.

Dean Rather
  • 2,717

9 Answers9

38
  • Find all folders in total, including subdirectories:

    find /mount/point -type d | wc -l
    
  • Find all folders in the root directory (not including subdirectories):

    find /mount/point -maxdepth 1 -mindepth 1 -type d | wc -l
    

    The -maxdepth 1 confines the command to the current directory (i.e., it forbids recursion); the -mindepth 1 causes it not to include the top-level directory (the mount point) itself.

quack quixote
  • 43,504
25

Navigate to your drive (Can open a terminal window there) and simply execute:

ls -lR | grep ^d | wc -l
wasif
  • 9,176
5

Newlines are valid characters in directory names. I suggest letting find print a character for each directory found and then letting wc count those characters:

find /mount/point -mindepth 1 -type d -printf 'a' | wc -c

Specify -mindepth 1 to avoid counting the mount point directory itself.

3

Try the following [but see below]:

ls -1 -Ap /mount/point | grep "/" | wc -l

Note: the first option to ls is dash one, but the option to wc is dash lower case L.

This will print a one-column list of the current directory (including . entries other than . and .. themselves), with trailing slashes for items that are subdirectories, then count the lines with the slashes.

If you want to look at the entire directory tree (i.e., look at the directory recursively), you should probably go with quack quixote's answer, as it is a little more explicit, but I've corrected mine (after taking quack's suggestions into account):

ls -ARp /mount/point | grep '/$' | wc -l
2

I have written ffcnt to speed up recursive file counting under specific circumstances: rotational disks and filesystems that support extent mapping.

It can be an order of magnitude faster than than ls or find based approaches.

wasif
  • 9,176
the8472
  • 531
2

I have found du --inodes useful, but I'm not sure which version of du it requires. On Ubuntu 17.10, the following works:

du --inodes      # all files and subdirectories
du --inodes -s   # summary
du --inodes -d 2 # depth 2 at most

Combine with | sort -nr to sort descending by number of containing inodes.

krlmlr
  • 902
1

When there is a large number of directories, tools like tree might take an eternity to finish or even hang, so you might want to use something more efficient.

The most efficient way to count the directories I can think of would be the following, since find will only print one . for each folder found instead of the complete path and file name and wc only needs to iterate over the number of characters:

find /mount/point -type d -printf '.' |wc -c

To exclude /mount/point itself from the calculation and only count the sub directories:

find /mount/point -mindepth 1 -type d -printf '.' |wc -c
David
  • 154
0

I like to use tree to pull directory count with:

tree -d -R -fi --noreport | wc -l

Or, I'll use find to show bulk of the folders are located with:

find . -type d -printf "%h\n" | cut -d/ -f-2 | sort | uniq -c | sort -rn
wasif
  • 9,176
-2

To find number of folders and directory in current directory, type the following command in your terminal:

echo */ | wc
wasif
  • 9,176