Re: number of files in directory?
On Tue, Nov 02, 2004 at 11:52:09PM +0000, Thomas Adam wrote:
> --- Lance Hoffmeyer <firstname.lastname@example.org> wrote:
> > How can I get a count of the number of files in a directory?
> > directory + subdirectoies?
> ls -1 | wc -l
> (note the "-1" option to 'ls' is hyphen-one, NOT lower-case L, which is
> what the option to 'wc' is).
You may want to count hidden files and directories as well with the
`-A' option. And I think the original poster may want to count the
total number of files in the directory and its subdirs. `-R' will help
you there, though you will count a bit too much.
`ls -1RA' will give you the list, but for every directory an extra
whiteline and a line with that directory name and a colon. The
following should ignore those lines and give the desired result -
unless you have real files ending in a colon...
ls -1RA | egrep -v "^$" | egrep -c -v ":$"
Come to think of it, you can also do this with `find':
find . -regex ".*" | wc -l
Find is actually a lot faster. I timed it on my system, as root in the
mauritsvanrees:/# time ls -1RA | egrep -v "^$" | egrep -c -v ":$"
mauritsvanrees:/# time find . -regex ".*" | wc -l
So `find' is roughly four times as fast. The difference in the number
of files is probably because some files were removed during the
operation, though I'm not completely sure of that. And `find' also
counts the current directory, so it finds one more than the other
Maurits van Rees | http://maurits.vanrees.org/ [Dutch/Nederlands]
"Let your advance worrying become advance thinking and planning."
- Winston Churchill