Simple file count question

Jonathan Arnold jdarnold at
Tue Mar 16 07:42:13 PST 2004

Dave Carrera wrote:
> I know this is going to be simple but I cant find a suitable answer by
> trawling the web so I ask how do I count the number of certain files in a
> directory?
> So I am in my dir and want to count how many files begin with db and show me
> the number.
> I hope you can help me and thank you in advance for any help you may give

As you can see, the key command is 'wc'. It's a nice, simple little command
that does one thing and one thing well - it counts "words". If you don't
give it any flags, it tells you the number of lines, words, and bytes. Using
-l will count the number of lines. So you pipe the output of a command to
it and it will count stuff for you. Thus:

$ ls db* |wc -l

will give you a single number telling you the number of lines in its input;
in this case, the input is the output of 'ls db*', which is a simple listing
of all the files in the current directory beginning with 'db'. Thus, you
get a count of the number of files in the directory that begin with db.
A very typical Un*x way of doing things - string together building block
commands to get your output. Flexbile if arcane.

Jonathan Arnold     (mailto:jdarnold at
Daemon Dancing in the Dark, a FreeBSD weblog:

More information about the freebsd-questions mailing list