Instead of using find
, use a for
loop. I am assuming that you are using bash
or similar since that is the most common shell on most of the modern Linux distros:
for i in treedir_*; do ls "$i" | wc -l; done
Given the following structure:
treedir_001
|__ a
|__ b
|__ c
treedir_002
|__ d
|__ e
treedir_003
|__ f
The result is:
3
2
1
You can get fancy and print whatever you want around the numbers:
for i in treedir_*; do echo $i: $(ls "$i" | wc -l); done
gives
treedir_001: 3
treedir_002: 2
treedir_003: 1
This uses $(...)
to get the output of a command as a string and pass it to echo
, which can then print everything on one line.
for i in treedir_*; do echo $i; ls "$i" | wc -l; done
gives
treedir_001
3
treedir_002
2
treedir_003
1
This one illustrates the use of multiple commands in a single loop.
for
can be redirected to a file or piped just like any other command, so you can do
for i in treedir_*; do ls "$i" | wc -l; done > list.txt
or better yet
for i in treedir_*; do ls "$i" | wc -l; done | tee list.txt
The second version sends the output to the program tee
, which prints it to standard output and also redirects it to a file. This is sometimes nicer for debugging than a simple redirect with >
.
find
is a powerful hammer, but not everything is a nail...