[Gllug] # files in a subdir

Alain Williams addw at phcomp.co.uk
Fri Sep 21 07:15:55 UTC 2001


On Thu, Sep 20, 2001 at 06:26:58PM +0100, Murray Walker wrote:
> hi,
> 
> blagging an answer to a question:
> 
> if I wanted to find the respective sizes of all the subdirectories of the
> cwd, I could do something like:
> 
> du -sk *
> 
> but if I wanted to find the *number of files* in each subdirectory, how
> would I do that?
How about:
	find . -type d |while read dir
			do	num=$(ls $dir | wc -l)
				echo "$dir $num"
			done
This counts the names in a directory which includes sub directories.
It does not count files who's name start with a '.', if you want that:
	find . -type d |while read dir
			do	num=$(( $(ls -a $dir | wc -l) - 2 ))
				echo "$dir $num"
			done

> I could use du -a | wc for each subdirectory, but I was wondering if there
> was a neater way than listing each file and wc'ing it?  Thinking about it,
> I'm not sure why, but I thought maybe the filesystem would keep a track of
> this.  That might have been a silly assumption on my part....
Why should it, it is so easy to find out.

-- 
Alain Williams

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list