Why does every directory have a size 4096 bytes (4 K)?
- Without getting too technical, think of a directory entry as simply a "link" to a list of the files the directory "contains."
- Then, as with everything,
ls
shows you the size of that link, not the total space occupied by the contents of the directory. - The minimum size a file or directory entry/link must occupy is one block, which is usually 4096 bytes/4K on most ext3/4 filesystems.
To understand this, you'd better have some basic knowledge of the following (file system):
- inode (contains file attributes, metadata of file, pointer structure)
- file (can be considered a table with 2 columns, filename and its inode, inode points to the raw data blocks on the block device)
- directory (just a special file, container for other filenames. It contains an array of filenames and inode numbers for each filename. Also it describes the relationship between parent and children.)
- symbolic link VS hard link
- dentry (directory entries)
- ...
On typical ext4
file system (what most people use), the default inode
size is 256 bytes, block size is 4096 bytes.
A directory is just a special file which contains an array of filenames and inode numbers. When the directory was created, the file system allocated 1 inode to the directory with a "filename" (dir name in fact). The inode points to a single data block (minimum overhead), which is 4096 bytes. That's why you see 4096 / 4.0K when using ls
.
You can get the details by using tune2fs
& dumpe2fs
.
Example
root@ubuntu:~# tune2fs -l /dev/ubuntu/root
tune2fs 1.42 (29-Nov-2011)
Filesystem volume name: <none>
Last mounted on: /
Filesystem UUID: 2fca4cbb-22f1-4328-ab13-cacedb360930
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 967680
Block count: 3931136
Reserved block count: 0
Free blocks: 2537341
Free inodes: 517736
First block: 0
Block size: 4096
Fragment size: 4096
Reserved GDT blocks: 416
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8064
Inode blocks per group: 504
RAID stride: 35637
Flex block group size: 16
Filesystem created: Thu Mar 15 14:31:04 2012
Last mount time: Sat Oct 20 20:28:04 2012
Last write time: Sat Oct 20 20:23:32 2012
Mount count: 1
Maximum mount count: -1
Last checked: Sat Oct 20 20:22:57 2012
Check interval: 0 (<none>)
Lifetime writes: 54 GB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
First orphan inode: 272350
Default directory hash: half_md4
Directory Hash Seed: d582ad79-75a0-4964-9a48-33ddba04df5c
Journal backup: inode blocks