Creating an arbitrarily large “fake” file

filessparse-files

I would like to create a special file similar to /dev/null or /dev/random, where the file doesn't actually exist but you can read from it all the same, except that I could actually set a cap on the apparent size of the file.

To put it another way, I want to create a special file where (assuming I set the cap at 500GB) when I "cat" the file it will output all 500GB of the file and then stop. It needs to act the same as an actual 500GB file, but without taking the space. The contents of this file don't matter, it could be all \0's like /dev/null, or just a small string being sent over and over, or whatever.

Is this something that's do-able? The only thing remotely close I've been able to find is man pages talking about mknod, but those weren't very helpful.

Best Answer

You can create a sparse file on certain filesystems, which will appear to be a certain size, but won't actually use that much space on disk.

$ dd if=/dev/null of=sparse bs=1024 count=1 seek=524288000
0+0 records in
0+0 records out
0 bytes (0 B) copied, 2.4444e-05 s, 0.0 kB/s
$ ls -l sparse 
-rw-rw-r--. 1 ignacio ignacio 536870912000 May  9 22:25 sparse
$ du -h sparse
0   sparse
Related Question