Creating an arbitrarily large "fake" file

9,716

Solution 1

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

Solution 2

Beyond the portable dd/seek based solution already mentioned some Unixes have specialized commands:

At least on Solaris, MacOS/X and Irix:

mkfile -n 500m sparseFile

On HP-UX prealloc, on AIX lmktemp. and many Linux distributions have truncate

Share:
9,716

Related videos on Youtube

Mediocre Gopher
Author by

Mediocre Gopher

Updated on September 18, 2022

Comments

  • Mediocre Gopher
    Mediocre Gopher almost 2 years

    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.

  • Mediocre Gopher
    Mediocre Gopher about 12 years
    This works perfectly. Best case I was hoping for a solution where I could actually script out what the output is (like, for every seek position you look at the data there would be a hash of that position or something along those lines), but that was pretty out there. This definitely works for now, thanks!
  • cjm
    cjm about 12 years
    @MediocreGopher, you certainly could do something like that with FUSE.
  • Zoran
    Zoran over 3 years
    I'm looking for a similar solution, but I need it to be able to confuse du specifically.