Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
12  
4096 bytes, not 4000. –  Mechanical snail Jan 22 '13 at 3:45
5  
@Mechanicalsnail 4095. You forgot the one byte of actual data –  Tobias Kienzler Jan 22 '13 at 8:19
2  
@Mechanicalsnail it's a leap year, isn't it? xkcd.com/394 :P –  tkbx Jan 22 '13 at 12:26

3 Answers 3

up vote 35 down vote accepted

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.

share|improve this answer
6  
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 '13 at 1:11

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."

share|improve this answer
4  
“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. –  Gilles Jan 21 '13 at 22:36
    
@Gilles Ah, thanks re: the inode. And yup, cluster/block. –  Christopher Jan 21 '13 at 22:59

An 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.
share|improve this answer
    
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 '13 at 4:24
2  
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. –  Jonathon Reinhart Jan 22 '13 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 '13 at 5:19
2  
I'm saying that none of the 4 KiB block size is used for metadata. I think my example proves that. –  Jonathon Reinhart Jan 22 '13 at 5:51
2  
@pvn: Jonathon is right. Metadata is stored in the inode for the file, which is separate from the block used to store file data. –  Mechanical snail Feb 7 '13 at 3:49

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.