Unix Programming - count of files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2006 > count of files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author count of files
satishmullapudi84@gmail.com

2006-08-10, 7:36 am

Hi all,
I am using RHEL v 4.1 . Is there any command to get the count of files
in a particular directory. If I am using 'find' I get the list of all
files recursively in the directory. Now I want to find the count of
these files. please help me out..
thank you.
http://www.google.co.in/search?hl=e...
&spell=1


Rainer Temme

2006-08-10, 7:36 am

satishmullapudi84@gmail.com wrote:
> I am using RHEL v 4.1 . Is there any command to get the count of files
> in a particular directory. If I am using 'find' I get the list of all
> files recursively in the directory. Now I want to find the count of
> these files. please help me out..


'wc' is your friend ...
Nils O. Selåsdal

2006-08-10, 7:36 am

satishmullapudi84@gmail.com wrote:
> Hi all,
> I am using RHEL v 4.1 . Is there any command to get the count of files
> in a particular directory. If I am using 'find' I get the list of all
> files recursively in the directory. Now I want to find the count of
> these files. please help me out..
> thank you.
> http://www.google.co.in/search?hl=e...
&spell=1

Since find outputs names, one per line, we can just count the lines.
find . |wc -l

If you're only interested in counting files and not directories,
we can find files only,
find . -type f |wc -l
That'll only find regular files, find -not -type d for everything
not a directory.

Perhaps you meant something else by "count of files" like
what du -h, or du -bs gives you
James Antill

2006-08-11, 7:31 pm

On Thu, 10 Aug 2006 03:34:53 -0700, satishmullapudi84 wrote:

> Hi all,
> I am using RHEL v 4.1 . Is there any command to get the count of files
> in a particular directory. If I am using 'find' I get the list of all
> files recursively in the directory. Now I want to find the count of
> these files. please help me out..


To do it correctly isn't trivial, the almost works method is:

% find . -maxdepth 1 | wc -l

....one solution for the correct method would be:

% find . -maxdepth 1 -print0 | \
perl -nle 'BEGIN { $/ = "\x0"; } ++$num; END { print $num }'

--
James Antill -- james@and.org
http://www.and.org/and-httpd

Chris F.A. Johnson

2006-08-11, 7:31 pm

On 2006-08-10, satishmullapudi84@gmail.com wrote:
> Hi all,
> I am using RHEL v 4.1 . Is there any command to get the count of files
> in a particular directory. If I am using 'find' I get the list of all
> files recursively in the directory. Now I want to find the count of
> these files. please help me out..
> thank you.
> http://www.google.co.in/search?hl=e...
&spell=1


Do you want all subdirectories recursively? If so:

find . -type f | wc -l

That will be inaccurate if you have files with newlines in the name.

If you just want the files in the current directory:

set -- *
num=$#
set -- */
numdirs=$#
numfiles=$(( $num - $numdirs ))

That will not include dotfiles unless your shell has dotglobbing
turned on.

Basically, the best way to do it will depend on your exact
requirements and the type of files.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com