Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have just found out that /dev/null is a file and not a directory.

I'm just wondering if it has an actual file size.

share|improve this question

2 Answers 2

/dev/null is not really a file. It's a character device!

$ ls -l /dev/null
crw-rw-rw- 1 root root 1, 3 Apr 10 09:53 /dev/null

The first letter c of the permissions string (crw-rw-rw-) indicates this. For files, it would be a - instead.

So in easy words: /dev/null is not a file but a virtual device mapped to this path in the file system which has the only purpose to swallow and vanish data - like a black hole.
It can also be used as input though, then it acts like an empty file (size 0) and immediately returns an EOF (EndOfFile).

Therefore it does not have a specified file size, as it's not a file.

share|improve this answer
    
Input? What does it supply an endless list of NUL characters? –  Jonathon Wisnoski 20 hours ago
    
@JonathonWisnoski I don't know exactly and I never used it like this, so I can just give you this link to a question on StackOverflow where it's explained. –  ByteCommander 20 hours ago
4  
@JonathonWisnoski, no - it behaves like a zero-sized file on input, so that the first read from it reports that the file is at EOF. –  godlygeek 20 hours ago
    
Yep, that's correct. –  godlygeek 20 hours ago
    
@JonathonWisnoski look at /dev/zero for that. –  Paŭlo Ebermann 19 hours ago

/dev/null is a special kind of file called "device file".

Device files act as a interface to some kernel functions. They just occupy the space that is needed for a directory entry ("inode") but don't have any real content and don't have an actual file size.

Other device files are e.g. /dev/sda (generally a HDD or SSD), /dev/zero (a file that generates zero's when read), or /dev/random (a file that generates random data when read). Actually about all files in /dev/ are either device files or links pointing to device files.

share|improve this answer
    
While I find most of this answer correct (+1), I think that it's a little misleading to say that they occupy the size of an inode entry. They either don't occupy any space, if referring to disk space, or they take more, if referring to their whole implementation as character devices. –  kos 23 hours ago
    
Just like any other file a device file needs an inode that stores the owner, permissions, major/minor number and other metadata. It depends on the file system how and where this inodes are stored (you can use mknod to create device files anywhere you want). For virtual file systems like /dev/ the inode doesn't occupy disc space but some memory. –  Florian Diesch 22 hours ago
    
Exactly. I think I didn't express myself well, so let me rephrase. Perhaps what I'm pointing out it's a little picky, so forgive me for that. What I meant is that if you state that each character device occupies some space in memory due to its inode entry, perhaps you want to point out that functions to be called upon read/write on such device, which are loaded along with them, do so also. –  kos 22 hours ago
    
Is it really correct to say a file occupies the space occupied by its inode table entry? I don't think this is usually included in what people--and software people have written--mean when they talk about file size. In particular, asking ls and du to report the size of a character special device like /dev/null or /dev/random yields 0. –  Eliah Kagan 17 hours ago

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.