Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting files in subdirectories? 1

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
I have used the following code and was delighted with it until today.

Code:
/usr/bin/find . -type f -iname  '*' -printf "%h\n" | uniq -c | grep content | gawk '{print substr($2,3),$1}'

The above counts files in a directory and displays

directoryname filecount

Someone pointed out to me today that it does not show the empty directories with a 0 count. I know I can do a for loop etc to get the result but would realy like it to be an efficient 1 liner if possible.

Maybe this is not right forum for this question but would appreciate any input from our learned friends on here

As always,thanks in advance,
Madasafish
 

Correction to be more more clearer,

"The above counts files in a directory and displays"

should be

The above counts files in sub-directories with the name "*content" (in my case) and displays"
 
The -iname '*' in that command was superfluous by the way.

Try this?

Code:
find . -type d -name '*content*' -printf "%f " -exec sh -c "ls \"{}\" | wc -l" \;

NB: This counts non-hidden entries in the directory in question, whether they be files or directories. You may want to use -printf "%-40f " or similar to get tidier output.

Hmm... not very awkey is it?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top