Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My friend works in a IT company where he is required to keep a count of XML files inside a directory (including subdirectories).

However the count using ls -LR | grep .xml or similar takes a lot of time as the count of files is in the millions.

I was thinking what can be a better approach?

Can some kind of background process take care of this, so that whenever there is a new file created or modification time change, it affects the counter (no. of files)?

share|improve this question

2 Answers 2

The daemon you describe could use inotify.

But perhaps using find and wc -l would already be fast enough?

I only ran one quick test but there is a significant difference between time ls -lR /mm/|grep -c jpg (real 0m2.168s) and time find /mm -type f -name \*jpg|wc -l (real 0m0.397s) on my system. Both return approx 42000 files so the difference on larger directories would probably be larger.

(I ran both commands several times to exclude disk caching effects.)

share|improve this answer
    
Thanks for your inputs. find and wc -l takes lots of time, also some kind of notifier is better as the number of files changes are very less, so repetitive find and wc -l doesnt suit mycase. –  Novice User May 12 '12 at 13:41
    
Counting lines might take a while. But you can use "-printf ." parameter of find and pipe it to "wc -c" to count the printed dots. That should be faster (see my answer). –  hluk May 12 '12 at 15:41

Following script will watch a directory indefinitely and print number of '*.xml' files every time the directory content changes.

DIR="a_path_to_directory_to_watch"
(echo; inotifywait -m -r -e create -e delete "$DIR" 2> /dev/null) |
    while read; do
        find "$DIR" -name '*.xml' -printf . | wc -c
    done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.