Operating System - HP-UX
1825748 Members
2448 Online
109687 Solutions
New Discussion

Can I estimate the file numbers in a directory by the size of the directory?

 
SOLVED
Go to solution
MA Qiang
Regular Advisor

Can I estimate the file numbers in a directory by the size of the directory?

For example:
# ll -ld a05
drwxr-xr-x 2 4621 4620 2212864 Dec 24 08:26 a05

Can I estimate the file numbers in a05?

Thanks.
14 REPLIES 14
RAC_1
Honored Contributor

Re: Can I estimate the file numbers in a directory by the size of the directory?

I do not think so unless you query te directory inode itself. The inode of directory is same as inode of a file except that it also keeps count of what files this dir holds.
There is no substitute to HARDWORK
MA Qiang
Regular Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

How to query the directory inode by UNIX system command?

Thanks.
Bill Hassell
Honored Contributor

Re: Can I estimate the file numbers in a directory by the size of the directory?

I'm not sure your question is clear. To see the first inode number for a specific file, just add -i to the ll command as in:

ls -ild a05

Now in your example, the first number (2) means that this file has two names, in other words, a hard link. The next two numbers (4610 4620) are the user ID (UID) and group ID (GID) and since they are shown as numbers, there is no matching number in the /etc/passwd and /etc/group file, respectively. When you use the ls -l or ll command, the username and groupname are shown only when the UID and GID match entries in these two files.

When these numbers show up instead, there are two possibilities:

1. The file was restored from some other system where these numbers have meaning,

or

2. Your system administrator has remove a user and group with these ID numbers. Files created by this user are not automatically removed.


Bill Hassell, sysadmin
MA Qiang
Regular Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

# ls -ild a05
641685227 drwxr-xr-x 2 4621 4620 2212864 Dec 24 08:26 a05

What is the relationship between the inode number(641685227) and file number?

Thanks.
RAC_1
Honored Contributor
Solution

Re: Can I estimate the file numbers in a directory by the size of the directory?

MA Qiang,

If I understood your question correctly (and I think i understood it), what you are trying to do is "you want to no. of files in a dir, just by looking at directoy.

It is not possible unless you query the inode directly. This would require some c code. Also you need to know inode structure. This becomes more diffifcult, because, if a file in dir is removed, that inode entry is marked as free. So just because inode entry is there does not mean a file is associated with it and is present.

man vxdb would give some insights into this.
There is no substitute to HARDWORK
Bill Hassell
Honored Contributor

Re: Can I estimate the file numbers in a directory by the size of the directory?

Files in Unix are stored in a structure called an inode and a directory is just another file. The number 641685227 is the number of the inode that has the directory name a05. The size of this directory is 2212864 (which is fairly large, indicating that there are a *LOT* of files in this directory. Each file name in the directory requires a space for the name, inode number and other details, but since the name can be variable in length, there is no way to determine the quantity of files in this directory by using its size.

To count the number of files in a directory, you can use a number of techniques. Here's a simple way:

cd a05
echo * | wc -l

but if there are thousands of files, this will fail with a "line too long" message. Another way is to use find:

cd a05
find a05 -type f | wc -l

but this will also count subdirectories. To see if there are subdirectories under a05:

cd a05
find a05 -type d | wc -l


Bill Hassell, sysadmin
Cem Tugrul
Esteemed Contributor

Re: Can I estimate the file numbers in a directory by the size of the directory?

Hi MA Qiang,
A link about inode as below;
http://docs.hp.com/en/B3929-90011/ch02s04.html#s2-4-7

Good Luck,
Our greatest duty in this life is to help others. And please, if you can't
Biswajit Tripathy
Honored Contributor

Re: Can I estimate the file numbers in a directory by the size of the directory?

You should be able to parse the octal dump of the
directory file (using command:
$ od -c /xyz/dir
'-c' option for display in ASCII ) to get the number
of files present in the directory. I have not tried it yet
and probably would not recomend as it would be
messy. If you can, use Bill Hassell's suggestions.

- Biswajit
:-)
MA Qiang
Regular Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

We should use 'wc -w' to get the number of files.

echo *|wc -w

Who can tell me how much files with 12 characters filename can be in a 2212864 bytes directory? Do not use 'find, echo * or ls'.

Thanks.

Jan Zalman
Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

As mentioned above, entries enumeration can be done using some c code. See attached example. Regards. Jan.
Time and loyalty cannot be bought.
MA Qiang
Regular Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

Thanks for Jan Zalman, the code is worked. But I still want to know how to get the number of files in a directory from the size of the directory, when I have known the length of the every filenames in the directory. Is it possible?
Michael Schulte zur Sur
Honored Contributor

Re: Can I estimate the file numbers in a directory by the size of the directory?

Hi,

the inode grows when more files enter the directory but I doubt it will grow smaller when files are deleted except through recreating the directory.

greetings,

Michael
MA Qiang
Regular Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

I don't want to know the exact number of files, just want to know the range of the number.

Thanks.
Jan Zalman
Advisor

Re: Can I estimate the file numbers in a directory by the size of the directory?

Internal organization of directory files is fs-type dependant and thus not standartized. Size of directory file should be somehow proportional with number of files inside this directory and lenght of their names, but this is not always true. For example (hp-ux 11.11v1 with JFS 3.3v4), directory file grows with 1kB steps (or whatever is jfs_fragment) and it does not shring back, when one deletes file inside. You can see a huge directory actually completely empty, which only means there WAS a time, it contained thousands of files. Considering this showcase I thing credible estimation is hard to achieve.
Time and loyalty cannot be bought.