Ubuntu – How to count number of files and directories recursively in a given directory

command line

I can find number of files recursively in a given directory

find . -type f | wc -l

I can find number of directories recursively in a given directory

find . -type d | wc -l

But what I want a single command that shows number of files and directories recursively in a given directory in the same time.

The output would be something like

6 directories, 14 files

Best Answer

Use tree:

$ tree foo
foo
├── bar
│   ├── baz
│   └── foo3
└── foo2

2 directories, 2 files

Or you can combine find and awk:

find foo -type d -printf "d\n" -o -type f -printf "f\n" | awk '/f/{f++}/d/{d++}END{printf "%d directories, %d files\n", d, f}'

Or, generalizing:

find foo -printf "%y\n" | awk '/f/{f++}/d/{d++}END{printf "%d directories, %d files\n", d, f}'

If you don't mind concise output:

find foo -printf "%y\n" | sort | uniq -c

This will count one more directory than tree - the starting directory is not counted in tree's output.