Allocate File

最後更新: 2017-11-20



Debian package


package: util-linux


package: coreutils



ls Opts







shrink or extend the size of a file

 For filesystems which support the fallocate system call (btrfs, ext4, ocfs2, and xfs)

 no IO to the data blocks (much faster than creating a file by filling it with zeros)

 * If a FILE is larger than the specified size, the extra data is lost.

 * If a FILE is shorter, it extended part (hole) reads as zero bytes


-s[+/-]N(K,M,G)            # size


truncate -s 1g iscsi.img

ls -slh iscsi.img

0 -rw-r--r--. 1 root root 1.0G Nov 19 23:33 iscsi.img




fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it.


Preallocate space to a file (ext4, btrfs ...)           # 須要 fs 支援 fallocate system call

preallocation is done quickly by allocating blocks and marking them as uninitialized

no IO to the data blocks =>  faster than creating a file by filling it with zeros.

fallocate 沒有 overcommit 效果 (因為 allocating blocks and marking them as uninitialized)

fallocate: test.bin: fallocate failed: No space left on device


-l, --length length           # Specifies the length of the range, in bytes.

  • KiB (=1024), MiB (=1024*1024)         # The "iB" is optional
  • KB (=1000), MB (=1000*1000)              


no overcommit

# fallocate 前

df -h /ssd

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc1       230G   96G  122G  45% /ssd

# fallocate 1G iscsi.img

fallocate -l 1G 1G.img

# df -h 會見到 "Used" 多了 1G

df -h /ssd

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc1       230G   97G  121G  45% /ssd


ls -sl --block-size=M *.img

1025M -rw-r--r-- 1 root root 1024M Sep 20 14:42 1G.img

-d, --dig-holes

Detect and dig holes. This makes the file sparse in-place, without using extra disk space.

You can think of this option as doing a "cp --sparse" and then renaming the destination file to the original

(without the  need  for extra disk space)

-p, --punch-hole

Deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for length bytes.

Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file.

 --keep-size is implied

-z, --zero-range

Zeroing is done within the filesystem preferably by converting the range into unwritten extents.

I/O is required only to update metadata.

Option --keep-size can be specified to prevent file length modification




# Create 1G file (fill with zero)

dd bs=1M count=1024 if=/dev/zero of=test.bin