Operating System - HP-UX
1754903 Members
3736 Online
108827 Solutions
New Discussion юеВ

Best way to find out number of files

 
SOLVED
Go to solution
KapilRaj
Honored Contributor

Best way to find out number of files

Gents,

Can anybody suggest the best method to count number files under any given directory other than "ls" ?.

This is a JFS filesystem and logically a directory file should have a list of inodes (?) and I would like to count them.

Regds,

Kaps
Nothing is impossible
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Best way to find out number of files

KNT=$(ls | wc -l)
is pretty easy

but if you want another way:

#!/usr/bin/perl

opendir DH1,$ARGV[1];
@arry = readdir(DH1);
closedir DH1;
print $#arry + 1,"\n";

You should add some error checking and maybe some stat commands to look for certain types of files but that should get you started.
If it ain't broke, I can fix that.
Joseph Loo
Honored Contributor

Re: Best way to find out number of files

hi,

try this at the directory you are interested in:

# ls|wc -l
wc counts the number of lines which is equivalent to the number of files in that directory.

regards.
what you do not see does not mean you should not believe
V.Tamilvanan
Honored Contributor

Re: Best way to find out number of files

Hi,

find /DIRNAME -type f |wc -l
KapilRaj
Honored Contributor

Re: Best way to find out number of files

Hi Clay,

When I invoke it with directory as an argument, why is it printing "0" when there are multiple files in it ?.

./script1 /u/hcsg41/OLDLOGS
0

Am I wrong in syntax ?. I am not good at perl

Joseph, I clearly mentioned other than "ls"

Tamil , I have already tried find but it is cpu intensive as I have 9,00,000 files in a filesystem on which I want to count nofiles for duirectories! (some bloody developer screwed it up)

Regds,

Kaps
Nothing is impossible
Nicolas Dumeige
Esteemed Contributor

Re: Best way to find out number of files

Kapil,

strings $DIR | wc -l

1) It doesn't use ls ;)
2) You'll be able to see strange thing : file that were deleted

More seriously, I supose the fastest result should be obtenaid with C using some file stat API.

Cheers

Nicolas
All different, all Unix
Francisco J. Soler
Honored Contributor

Re: Best way to find out number of files

Hi Kaps,

If you want to count the files recursively you can try:

find directory -print | wc -l

if you want count only regular files or directories do:

find directory -type f -print |wc -l
or
find directory -type d -print |wc -l

Frank
Linux?. Yes, of course.
Juergen Tappe
Valued Contributor

Re: Best way to find out number of files

Try

find /directory/* -prune | wc -l

"-prune" does not look into subdirs.

if you wanna only files:
find /directory/* -type f -prune | wc -l

if you wanna count "dot-files" as well
find /directory/* /directory/.* | wc -l

"find" is better performing if you have lots and lots of entries in that directory, but normally "ls" is as good as this and easier to use.
Working together