linux ext4普通盤為什么目錄大小是4k?
Why does every directory have a size 4096 bytes (4 K)?
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 (I reckon most likely this is what you are using), 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.
要理解這個,你首先要懂:(注: 這里我用c7.4,默認xfs:/etc/fstab,我又掛了一個格式化成ext4做的實驗)
inode:
file
directory
symblic: 軟鏈接
參閱:
磁盤MBR分區機制- inode/Block深入實戰
linux的inode和block-軟硬鏈接

我猜目錄名字占了第一個inode-index.

inode-index和inodetable單獨開辟了一個block

目錄是特殊的文件,目錄內容包含: 文件名+inode號, 當目錄被創建后,文件系統會給這個目錄分配1個inode+特殊文件(即以目錄為名). inode指向一個block. 所有你才會看到目錄大小為4k.
這就能理解目錄里的文件,文件名字保存在了所在目錄的block里.
查看和修改ext4的inode和block
ext4 默認inode大小是256bytes, block大小是4096bytes
[root@moban ~]# dumpe2fs /dev/sdb |grep "Inode size"
Inode size: 256
[root@moban ~]# dumpe2fs /dev/sdb |grep "Block size"
Block size: 4096
- 格式化時候指定inode和block大小
[root@moban ~]# mkfs.ext4 -I 2048 -b 2048 /dev/sdb
查看目錄的inode內容


