56

For some reason, when I make a text file on OS X, it's always at least 4kB, unless it's blank. Why is this? Could there be 4,000 bytes of metadata about 1 byte of plain text?

enter image description here

3
  • 18
    4096 bytes, not 4000. Jan 22, 2013 at 3:45
  • 11
    @Mechanicalsnail 4095. You forgot the one byte of actual data Jan 22, 2013 at 8:19
  • 6
    @Mechanicalsnail it's a leap year, isn't it? xkcd.com/394 :P
    – tkbx
    Jan 22, 2013 at 12:26

3 Answers 3

61

The block size of the file system must be 4 kB. When data is written to a file that is contained in a file system the operating system must allocate blocks of storage to contain the data that will be written to the file.

Typically, when a file system is created the storage contained in that file system is segmented into blocks of a fixed size. This Wikipedia article briefly explains this process.

The underlying block size of the file system for this file must have a 4K byte block size. This file is using 1 4K block and only one byte within that block contains actual data.

3
  • 10
    A comment: In Windows, the actual file size is displayed by default, and the size on disk is displayed in the Options pane.
    – Joe Z.
    Jan 22, 2013 at 1:11
  • so can a block accommodate different files? Feb 27, 2018 at 5:56
  • @sudeepdino008 no, one block (at least) for each file (Linux’ ext file system has/had(?) an option for putting multiple files in one block, but that is an exception to the rule)
    – Ro-ee
    Oct 7, 2018 at 0:48
13

All file systems have a cluster or block size, or the smallest amount of disk space that can be allocated to hold a file. Even if the actual file size is smaller than the cluster/block size, it will still consume one cluster, or 4K on your file system. The cluster size depends on the file system, and the file system options.

If it contains zero bytes, as Gilles pointed out, it uses zero blocks/clusters but one inode on typical *nix file systems, which better answers the caveat, "unless it's blank."

1
  • 6
    “Even if a file size is zero bytes, it will still consume one cluster.” Actually, no: on typical unix filesystems, an empty file consumes one inode and zero blocks, and there is no notion of cluster that differs from blocks. Jan 21, 2013 at 22:36
12

A little experiment to help illustrate this:

First, let's see what the actual block size of my root ext4 (LVM) partition is:

[root@fedora17 blocksize]# dumpe2fs /dev/mapper/vg_fedora17-lv_root | grep -i "block size"
dumpe2fs 1.42.3 (14-May-2012)
Block size:               4096

It is 4096 (4 KiB), as expected. Now, let's create three files: The first is zero bytes, the second is just one byte, and the third is 4 KiB (the block size):

[root@fedora17 blocksize]# touch 0_bytes.bin
[root@fedora17 blocksize]# dd if=/dev/zero of=1_byte.bin bs=1 count=1
[root@fedora17 blocksize]# dd if=/dev/zero of=4096_bytes.bin bs=1 count=4096


Now, we ls the directory. We use the -s option to see the allocated size (the left-most column), in number of 1024-byte "blocks."
(ls doesn't know the real block size is 4096 -- we could specify --block-size but that scales everything by that value, and we want to see the actual file size in bytes, too).

[root@fedora17 blocksize]# ls -ls
total 8
0 -rw-r--r--. 1 root root    0 Jan 21 23:56 0_bytes.bin
4 -rw-r--r--. 1 root root    1 Jan 21 23:38 1_byte.bin
4 -rw-r--r--. 1 root root 4096 Jan 21 23:38 4096_bytes.bin

Two things can be noted here:

  • The zero byte file takes up zero blocks in the filesystem, confirming what Giles stated.
  • Even though the other two files have different file sizes, they both take up 4*1024 = one 4KiB ext4 block.

Sparse Files

Sparse files are files with large blocks of zeros. Because the data is known to be all zero, there's no point in storing it on the disk. In this way, a file's apparent size can actually be larger than the on-disk size.

Inline Data

Note that some filesystems allow the contents very small files to be stored in the inode itself. See Is it possible to store data directly inside an inode on a Unix / Linux filesystem?.

5
  • Yes you are quite correct the 4k is the size the file system uses to store information regarding the storage od the file inside the file system. Things such as the index of the file from the beginning of a block, index of the block and size of memory utilized by the file are stored which eat up 4k. This information is used to reference the text file from the file system.
    – pvn
    Jan 22, 2013 at 4:24
  • 4
    This is incorrect. File metadata like you mention do not "eat up" any of the 4KiB. Those structures are part of the filesystem formatting overhead. See my answer above for proof. If what you said was true, then my 4096-byte file would need more than one block. Jan 22, 2013 at 4:55
  • Pointers to the file (segment no, blk no) in the file system are the things that have to be stored and require one block to be assigned. If the text file is has very less content that can fit in the first block already assigned to it, then it won't require second block allocation. I agree that the whole of 4k is not used for the metadata and some internal fragmentation arises.
    – pvn
    Jan 22, 2013 at 5:19
  • 3
    I'm saying that none of the 4 KiB block size is used for metadata. I think my example proves that. Jan 22, 2013 at 5:51
  • 3
    @pvn: Jonathon is right. Metadata is stored in the inode for the file, which is separate from the block used to store file data. Feb 7, 2013 at 3:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .