53

I tried to cat /dev/zero, and it didn't seem to do anything. I googled /dev/zero, and it says it's basically a blank file with infinite size. Is cat printing an infinite number of non-existent characters? How does this work? How does it provide infinite data if it's 0 bytes? What are the uses of this file, if one can simply create a blank file?

4
  • 15
    Run cat -v /dev/zero to see what it is doing. See Clear unused space with zeros (ext3,ext4) for a common purpose.
    – manatwork
    Jan 31, 2013 at 13:09
  • 1
    There is also /dev/null which behaves like an empty file if you want to read it and as a black hole if you write to it.
    – jofel
    Jan 31, 2013 at 15:00
  • 1
    @jofel but you can't use /dev/null for infinite 0-bits, right? cat foo > /dev/null and cat foo > /dev/zero are the same, but cat /dev/null and cat /dev/zero aren't the same, correct?
    – tkbx
    Jan 31, 2013 at 15:19
  • 1
    @tkbx yes, correct. /dev/null returns immediately EOF if a program wants to read from it. /dev/zero returns infinitely 0-bytes.
    – jofel
    Feb 1, 2013 at 11:04

1 Answer 1

62

/dev/zero is a special file (in this case, a pseudo-device) that provides an endless stream of null characters (so hex 0x00)? That's why your cat is not outputting anything (but try running it through od (octal dump)).

'blank file with infinite size' is not 100% correct: it's not a regular file, but a special file (more like a 'stream' or a generator). You can read as much from it as you want, for example with dd (like dd if=/dev/zero of=yourfile count=1024 bs=1024).

It's not really a blank file, nor used to create blank files: it's used to create files or memory pages filled with only zeroes. You can also write to it, making it perform like a sinkhole (its more popular brother /dev/null is more commonly used for this though).

13
  • 1
    Why would someone want to fill up blank space, though?
    – tkbx
    Jan 31, 2013 at 13:16
  • 7
    For example, to erase what was there before. Or when transmitting.
    – Konerak
    Jan 31, 2013 at 13:18
  • 4
    I've not read the srm manual or source, but most 'secure erasers' prefer to write (pseudo-)random data instead of zero-ing a drive, since one pass of zero-ing is not secure enough (certain three-letter agencies can still deduct the original contents of a zeroed harddisk, since the individual bits are not 100% truly 1 or 0, but just "mostly 1" or "mostly 0". Compare it to a lever which you can put "all the way left" or "all the way right", but usually just a swing to the other side is enough.
    – Konerak
    Jan 31, 2013 at 13:32
  • 13
    @tkbx trying to dd a partition you may want to fill all empty space to 0 rather than random to get a smaller compressed file later.
    – neurino
    Jan 31, 2013 at 14:06
  • 1
    @Konerak I'm fairly suspicious of that. I've heard it said all over the place and I've never once seen evidence of it being possible on any remotely new harddrive (made in the past 10 years at least).
    – Rob
    Jan 31, 2013 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.