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.

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.

share|improve this question
add comment

4 Answers 4

up vote 14 down vote accepted

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
share|improve this answer
    
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! –  Mediocre Gopher May 10 '12 at 3:15
1  
@MediocreGopher, you certainly could do something like that with FUSE. –  cjm May 10 '12 at 5:55
add comment

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|improve this answer
add comment

If you do not need to seek back in the file, you could use a named pipe:

mkfifo my_special_file
yes | head -n 100 > my_special_file&
cat my_special_file

Of course, you can use any other command, including a complex script, to generate data in the pipe.

share|improve this answer
add comment

Well, with dd, you can specify a blocksize and a count, and thereby limit the size like this:

dd if=/dev/urandom count=5 bs=1024 > nullfile

If you want to read from it, just pipe it:

dd if=/dev/urandom count=5 bs=1024 | yourReadingProcess 

In former times, every linux installation I made came with some networking tools, portmap, inet.d and such, and in inet.d, there where some services always specified, one of them was described/named chargen, if I remember correctly.

(... one minute later ...)

Yes. In /etc/services, you find

chargen         19/tcp          ttytst source
chargen         19/udp          ttytst source

So I guess it is a service to test something, and which sends chars - maybe random - over the network. I only find /etc/xinet.d/chargen on my PC. There are more infos:

# default: off
# description: An xinetd internal service which generate characters.  The
# xinetd internal service which continuously generates characters until the
# connection is dropped.  The characters look something like this:
# !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg
# This is the tcp version.

However, I don't know how to use it for your purpose.

share|improve this answer
add comment

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.