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.

I want to know how many files I have on my filesystem. I know I can do something like this:

find / -type f | wc -l

This seems highly inefficient. What I'd really like is to do is find the total number of unique inodes that are considered a 'file'.

Is there a better way?

Note:

I would like to do this because I am developing a file synchronization program, and I would like to do some statistical analysis (like how many files the average user has total vs how many files are on the system). I don't, however, need to know anything about those files, just that they exist (paths don't matter at all). I would especially like to know this info for each mounted filesystem (and it's associated mount point).

share|improve this question

2 Answers 2

up vote 20 down vote accepted

The --inodes option to df will tell you how many inodes are reserved for use. For example:

$ df --inodes / /home
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/sda1            3981312  641704 3339608   17% /
/dev/sda8            30588928  332207 30256721    2% /home
$ sudo find / -xdev -print | wc -l
642070
$ sudo find /home -print | wc -l
332158
$ sudo find /home -type f -print | wc -l
284204

Notice that the number of entries returned from find is greater than IUsed for the root (/) filesystem, but is less for /home. But both are within 0.0005%. The reason for the discrepancies is because of hard links and similar situations.

Remember that directories, symlinks, UNIX domain sockets and named pipes are all 'files' as it relates to the filesystem. So using find -type f flag is wildly inaccurate, from a statistical viewpoint.

share|improve this answer
    
I think this is what I'm looking for. Is there any way to find out how many of each type of file there is? This isn't directly needed by my application, but could be useful knowledge. –  tjameson Sep 18 '11 at 2:08
1  
Not without inspecting the inodes of each file in the filesystem, which would be about the same as doing the find. –  Arcege Sep 18 '11 at 2:20
    
Does df understand most filesystems (ext2-4, ntfs, fat, reiserfs, etc)? –  tjameson Sep 18 '11 at 2:21
    
It would understand any filesystem type that could be mounted. Some filesystem types do not have inodes (for example, fat); df would display the total number of inodes as 0 on those filesystems. Other types should show the underlying filesystem's inode stats. –  Arcege Sep 18 '11 at 2:35
    
Would you have to redirect errors to /dev/null seeing as you would get lines warning you that files in /proc are "virtual"? –  MaxMackie Nov 26 '11 at 14:02

Use statvfs(), and calculate f_files - f_ffree.

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.