6

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

2 Answers 2

5

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.

1
  • Nice, i found it this time by chance. lol anyway just 8 minuets and mark as answer
    – Maythux
    May 21, 2015 at 8:24
1

You can do it like below:

d=$(find . -type d | wc -l)
f=$(find . -type f | wc -l)
echo "There are $d directories and $f files"

In my case, the output is There are 459 directories and 2352 files

HTH

4
  • This would be more than one command. anyway +1 for your effort
    – Maythux
    May 21, 2015 at 8:26
  • Oh, it marks . and .. as well, which is wrong. I take it back. May 21, 2015 at 8:31
  • This was useful. I'm using it after using scp -r to copy a folder (recursively) of files from Mac to PC on the same network. Unfortunately, although the number of directories is a match, I see a mismatch in number of files and in file sizes. I think that often happens between Mac and PC, but I don't know why (or how otherwise to double-check that my copying of files from Mac to PC was successful).
    – Ryan
    Jul 22, 2019 at 21:28
  • New question: superuser.com/questions/1462861/…
    – Ryan
    Jul 22, 2019 at 22:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .