I have a directory with lots of files.

How can I find out by the size of meta data reported by ls

drwxr-x--- 2 www-data www-data 1017M Aug  8 10:44 cookies_

How many files are in the directory.

I do not want to count them (even in Determining number of files in a directory without counting them there are counting them)

  • P.S. If I’m not mistaken, ls -ald is equivalent to ls -ld — the d option makes the a option irrelevant. – Scott Aug 8 '17 at 18:52
  • @Scott I think you're mistaken – jrw32982 supports Monica Aug 9 '17 at 19:35
  • @jrw32982: Do you have any evidence to support your thought? – Scott Aug 9 '17 at 23:05
  • @Scott man ls -a, --all: do not ignore entries starting with . -d, --directory: list directory entries instead of contents, and do not dereference symbolic links – jrw32982 supports Monica Aug 10 '17 at 2:32
  • @jrw32982: That supports my point.  -d means list directory entries instead of contents, or, somewhat more clearly, “list directories themselves, not their contents” (see ls(1) here) — in short, don’t list directory contents, further discussed here and here.  -a and -A affect which entries are listed (and which are not) when listing a directory’s contents — which doesn’t happen when you specify -d, so -a and -A are moot. – Scott Aug 10 '17 at 2:53

You can’t, for a number of reasons.

The first is that a directory’s size grows, but it doesn’t shrink (on most file systems anyway). Try this:

mkdir testdir && cd testdir
touch {1..100000}
rm {1..100000}
ls -ld ../testdir

This will produce a fairly large directory (nothing like yours admittedly, but that’s irrelevant here) containing no files...

The second is that in most cases, file records inside a directory entry are variable in length, depending on the file’s name. See for example the ext4 disk layout.

The third is that the directory might not even be linear, which complicates matters further.

The fourth is that a directory’s size is a multiple of the block size, so a directory with one file and a directory with twenty will usually have the same size.

| improve this answer | |
  • Can the second and third reason be mitigated if you know approximately the size of the filename, and the structure of the directory? (Not sure what you mean by linear here) – pipe Aug 8 '17 at 11:27
  • @pipe if all the files have names with similar lengths, you can use that to get an approximate upper bound to the number of files in the directory — if your file names occupy 12 bytes on average, an ext4 directory will use 20 bytes on average per entry (plus checksum blocks). By non-linear, I mean that there might be holes (in some file systems), or even tree structures. – Stephen Kitt Aug 8 '17 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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