Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am sure a directory file has much less information than 4096 bytes. I know the sector size is 4096 bytes. But normal files smaller than that do exist.

Why does Unix reserve 4096 bytes for each folder?

share|improve this question

4 Answers 4

up vote 13 down vote accepted

It's the initial size necessary to store the meta-data about files contained in that directory (including names). The initial allocation equals the size of one sector, but can grow above that if necessary. Once allocated, space is not freed if files are removed, to reduce fragmentation.

For example:

$ mkdir testdir
$ cd testdir
$ ls -ld .
drwxr-xr-x 2 matthew matthew 4096 2007-12-03 20:28 ./
$ for ((i=0; i<1000; i++)); do touch some_longish_file_name_$i; done
$ ls -ld .
drwxr-xr-x 2 matthew matthew 36864 2007-12-03 20:29 ./
$ rm some_longish_file_name_*
$ ls -ld .
drwxr-xr-x 2 matthew matthew 36864 2007-12-03 20:29 ./
$ cd ..
$ ls -ld testdir
drwxr-xr-x 2 matthew matthew 36864 2007-12-03 20:29 testdir/
$ rmdir testdir ; mkdir testdir
$ ls -ld testdir
drwxr-xr-x 2 matthew matthew 4096 2007-12-03 20:29 testdir/

source

share|improve this answer
1  
Thanks for the "space is not freed if files are removed" part. I restored a backup and wasn't sure why two otherwise identical directories had different sizes. –  Tomasz Zieliński May 14 '13 at 22:35

Sometimes 4096 bytes is the smallest allocation unit for some filesystems. That's why directory has 4096.

The same thing apply to files. Even though some files might report fewer than 4096, they are actually taking al least 4096 of storage from disk.

share|improve this answer
3  
Yup, as Pablo said. The size you are looking at with the file is the files content size, not the files on disk size. The on disk size of those small files is actually 4096, just like the directory as it can only allocate disk space in blocks of that size –  Dan McGrath May 19 '10 at 11:54
    
This has nothing to do with the question. –  harrymc May 20 '10 at 6:13
    
This is incorrect. See the accepted answer. –  VirtualDXS Oct 1 at 19:48

4096 is reserved to reduce fragmentation, because often the actual size of the metadata contained will fluctuate based on the directory contents. If it is constantly growing and shrinking (say it contained log files or dynamic content) over time it could hurt performance. This likely wouldn't happen with one folder, but across the whole file system it would add up quickly.

share|improve this answer

It depends on filesystem. On ext2/3/4 it "is" 4096. On reiserfs it can be 9608 (my $HOME) 1032 (/tmp) or 48 (some dir in /tmp).

By default on ext2/3/4 block is 4096 - and file cannot take less than that. If file is smaller it takes a whole block anyway. As it is pointless to ask about logical size of directory and this information is probably not on disk anyway and it have to report something it reports a size of block times the number of blocks taken i.e. the physical space that have been taken.

share|improve this answer
    
I am sure file size is also not stored with the files. It is computed when required. But for files, it reports the actual file size. –  Lazer May 19 '10 at 14:00
    
File size is stored with the inode. For sure ext2 inode contains the size in bytes inside it. Otherwise it would be impossible to find where the file ends as \0 is legal character in binary files. PS. It is possible to create FS that do not store it - but just not ext familly (and probably no other popular). –  Maciej Piechotka May 19 '10 at 21:53

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.