Counting files in a given directory


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Counting files in a given directory
# 1  
Old 11-15-2010
Counting files in a given directory

Hi all,

Need some help counting files... Smilie

I'm trying to count the number of files in a given directory (and subdirectories) which reportedly contains "thousands" of files.

I'm using this:

Code:
ls -R | wc -l

However it's been an hour and looks like it's still running; there is no output at all.

Is there a faster/better way to count the exact number of files or get an approximate value?

Thanks.
# 2  
Old 11-15-2010
Try:
Code:
find * -type f |wc -l

# 3  
Old 11-15-2010
Ok, I cancelled the "ls" and ran a "find"... now it's been almost four hours and the command is still running. Smilie

Guess I'm out of luck.
# 4  
Old 11-16-2010
I ran find command on my entire root (/) which finished in less then 2 minutes. In my case having more then 600K files
Code:
[root@aaa-build ~]# date
Tue Nov 16 10:01:22 IST 2010
[root@aaa-build ~]# time find / -type f | wc -l
662226

real    1m23.387s
user    0m1.149s
sys     0m6.536s


I'd adise you to run "find ${LOCATION} -type f | wc -l"

Last edited by Franklin52; 11-16-2010 at 09:02 AM.. Reason: Please use code tags, thank you
# 5  
Old 11-16-2010
Well, I tried with 'find' yesterday and left it running for about 7 hours and it never finished. Smilie

Apparently it's not just "thousands" of files, but several millions.

I'm still trying to figure out how to count those files. It would be enough for me if I could find out an approximate value, and not the exact number.

Any suggestions? Thanks.

---------- Post updated at 10:33 AM ---------- Previous update was at 10:12 AM ----------

Now I'm wondering, would the used inodes in the filesystem provide an approximate value of the total number of files? This is the output of df:

Code:
[root@atlas ~]# df -i
Filesystem            		Inodes   IUsed   IFree		IUse% 	Mounted on
/dev/mapper/volAvg-A1lv		15466496 4455023 11011473	30%	/export

# 6  
Old 11-16-2010
Anything that counts files will have to do so the same way: checking directory entries. So there's no special "faster ls". (If there was, why wouldn't we use it for everything?)

If you can compile on this machine, this program can provide a running total, updated once a second:

Code:
#include <stdio.h>
#include <time.h>

int lines=0;

int main(void)
{
	time_t timer=time(NULL);
	char buf[16384];
	while(fgets(buf, 16384, stdin) != NULL)
	{
		lines++;

		if((time(NULL)-timer) > 1)
		{
			fprintf(stderr, "\r%d", lines);
			timer=time(NULL);
		}
	}

	printf("%d\n", lines);

	return(0);
}

It only reads from stdin.

---------- Post updated at 10:30 AM ---------- Previous update was at 10:26 AM ----------

Quote:
Originally Posted by verdepollo
I'm still trying to figure out how to count those files. It would be enough for me if I could find out an approximate value, and not the exact number.
Very approximate since it includes directory entries as well, but since counting 4 million files is going to be hard, it might have to do.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 11-16-2010
I compiled your code; somehow the counter is increasing really slowly (~200 files/min).

I think I can assume there are around ~4 million files. Hopefully this approximation will work fine for my process.

Thanks all for your help. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting the number of files within a directory input by the user

So I have a loop that stated if a directory exists or not. If it does it prints the number of files within that directory. I use this code... result=`(ls -l . | egrep -c '^-')` However, no matter which directory I input, it outputs the number "2" What is wrong here? (4 Replies)
Discussion started by: itech4814
4 Replies

2. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

3. UNIX for Dummies Questions & Answers

Counting number of folders in a Directory

Help Needed ! Can we count number of folders of specific date in a directory, even if directory has folders of different dates. Please reply as soon as possible. (1 Reply)
Discussion started by: vishal_215
1 Replies

4. Shell Programming and Scripting

Counting the number of readable, writable, and executable items in a directory

Hello, I'm writing a script in sh in which the first command line argument is a directory. from that, i'm suppose to count the number of readable, writable, and executable items in the directory. I know using $1 represents the directory, and ls would display all the items in the directory, and that... (4 Replies)
Discussion started by: kratos22
4 Replies

5. Shell Programming and Scripting

Counting lines of code in a directory with awk

I've never toyed with awk, but it seems every time I present an elegant 2- to 8-line script, someone comes back with an awk 1-liner. I just came up with this to count all the lines of source code in a directory. How would I do it in awk? LINES=0 for n in $(wc -l *.cpp *.h | cut -b-7); do ... (2 Replies)
Discussion started by: KenJackson
2 Replies

6. Shell Programming and Scripting

Counting files in a directory that match a pattern

I have 20 files in a direcotry like BARE01_DLY_MKT_YYYYMMDD. The MKT differes for all these files but the remaining syntax remains the same for a particular day. If I am checking for today I need to make sure that there are 20 files that start with BARE01_DLY_MKT_20060720. How can I write a... (31 Replies)
Discussion started by: dsravan
31 Replies

7. UNIX for Dummies Questions & Answers

Line counting in Directory

I want to count the no of lines for files (.c and .h) present in a directory structure. My code is: #!/bin/bash # Usage: linecount.sh directory_name for file in $(find $1 -name ); do wc -l "$file" >> filecount.txt done Problem is that the directory structure is really big... (3 Replies)
Discussion started by: MobileUser
3 Replies

8. UNIX for Dummies Questions & Answers

Counting number of files in a directory

Some simple questions from a simple man. If i wanted to count the number of files contained within a directory, say /tmp would ls -l /tmp ¦ wc -l suffice and will it be accurate? second one: How would i check the number of files with a certain string in the filename, in the same directory. ... (2 Replies)
Discussion started by: iamalex
2 Replies

9. Shell Programming and Scripting

rm files in a directory, looping, counting, then exit

I am trying to write a script that will look for a file in a directory, then remove it. I need it to loop until it has removed a certain number of files. Is it better to do a repeat or to list each file in a pattern? Files will be numbered like RAF.01.*, RAF.02.*, etc. Thanks, James (6 Replies)
Discussion started by: JporterFDX
6 Replies
Login or Register to Ask a Question