为什么Linux中的空目录大小为4KB? [英] Why the size of an empty directory in Linux is 4KB?

查看:118
本文介绍了为什么Linux中的空目录大小为4KB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ubuntu 14.04,并使用mkdir命令在/tmp上创建了一个空目录:

I'm using Ubuntu 14.04 and I made an empty directory on /tmp with the mkdir command:

cd /tmp
mkdir foo

然后我用ls检查了它的大小:

and then I checked it's size using ls:

ls -ldh foo

结果显示目录的大小为4KB,尽管其中没有任何内容!

and the result shows that the size of the directory is 4KB, although it has nothing inside!

然后我触摸创建了一个空文件:

then I created an empty file with touch:

touch empty

然后我检查了它的大小:

and then I checked its size:

ls -l empty

结果显示空文件的大小为0B,与空目录不同.

the result shows that the empty file is of 0B, which differs from the empty directory.

我已经读到一些有关Q& A的说法,即4KB是目录的元数据.但是,如果是元数据,那么内部将存储什么类型的信息,为什么存储的信息如此之大,以及为什么一个空文件没有这种元数据?如果不是元数据,那么4KB是什么意思?

I've read about some Q&A's saying that the 4KB is the metadata of the directory. But if it is the metadata, what kind of information is stored inside and why it is so huge, and why an empty file don't have such kind of metadata? If it is not the metadata, what does the 4KB mean?

推荐答案

我将把这个问题分为3个部分,其中2个我可以回答...

I'm going to break this question down into 3 parts, 2 of which I can answer...

第1部分:为什么空目录大小不为0?

Part 1: why isn't an empty directory size 0?

因为它包含. .. ,所以它并不是真正的空.

Because it contains . and .. so it's not really empty.

第2部分:为什么最低4K?

Part 2: Why is 4K the minimum?

因为这是文件系统的块大小.创建文件系统时,可以将其设置为较小,但是会产生开销.文件系统必须记住每个块的空闲或使用中标志,因此,较小的块=更多的块=更多的开销.(在ext2的早期,默认块大小为1K.磁盘足够小,通过不为每个文件分配4K的倍数节省的空间比用于免费块图的空间更为重要.)

Because that's the filesystem's block size. You can set it smaller when you create the filesystem, but there is overhead. The filesystem must remember a free-or-in-use flag for every block, so smaller blocks = more blocks = more overhead. (In the early days of ext2, the default block size was 1K. Disks were small enough that the space saved by not allocating a multiple of 4K for every file was more important than the space used for the free block map.)

无法实现超过4K的块大小,因为4K是大多数处理器上的页面大小(虚拟内存的最小单位),并且Linux并非旨在处理大于内存的文件系统块页面.

Block sizes over 4K aren't possible because 4K is the page size (the smallest unit of virtual memory) on most processors, and Linux wasn't designed to deal with filesystem blocks bigger than memory pages.

第3部分:当您在 ls -l <​​/code>常规文件中时,您会获得实际使用的字节数,而当您在 ls -ld 目录中时,您会得到该数字分配的字节数.为什么?

Part 3: When you ls -l a regular file, you get the actual number of bytes used but when you ls -ld a directory, you get the number of bytes allocated. Why?

这部分我不知道.对于常规文件,您可以使用 ls -s 查看分配大小,这两个大小实际上告诉您不同的内容.但是在目录上, -l <​​/code>大小类似于 -s 大小的冗余副本.大概内核可以报告一个大小,该大小指示实际使用了4K块的多少,但没有使用.我不知道为什么.

This part I don't know. For regular files, there is an allocation size you can view with ls -s, and the two sizes actually tell you different things. But on directories, the -l size is like a redundant copy of the -s size. Presumably the kernel could report a size that indicates how much of the 4K block is actually used, but it doesn't. I don't know why.

这篇关于为什么Linux中的空目录大小为4KB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆