0

The C fsize function returns 4096 for special/device files, but is there an integrated function to know the real size of a device file if it is finite ?
I would also like to know if there is a way to know whether a file is a regular file or a device file ?

Thank you !

  • See How can I get the size of a file in a bash script? which has a section on that. It's very system dependant. If you have read access to the file, lseek(SEEK_END) may be an option. – Stéphane Chazelas Jan 16 '18 at 15:01
  • 1
    What's that fsize() function you're referring to? It doesn't seem to be a standard Unix/POSIX/GNU API – Stéphane Chazelas Jan 16 '18 at 15:11
  • The value you get is, in fact, the "real" size of the device file, that it occupies in the file system. What you probably want is the size of the content this entry is referring to, right? Perhaps you read xyproblem.info first, then rephrase your question, telling us what you really want to achieve... – Murphy Jan 16 '18 at 15:33
  • @Murphy Yes I would like the size of the content ! – Trevör Jan 17 '18 at 20:07
0

Your question is still rather unspecific and broad, and you'll get unspecific answers to unspecific questions, which may be helpful or not. Still here's a try to point you into the right direction.

I would also like to know if there is a way to know whether a file is a regular file or a device file ?

Of course. The most common is the output of ls -l:

$ ls -l /dev
total 0
crw-rw----+ 1 root   audio      14,   4 Jan  5 00:44 audio
crw-------  1 root   root       10, 235 Jan  5 00:44 autofs
drwxr-xr-x  2 root   root           480 Jan 17 01:20 block
drwxr-xr-x  2 root   root           140 Jan 17 01:20 bsg
drwxr-xr-x  3 root   root            60 Jan  5 00:44 bus
lrwxrwxrwx  1 root   root             3 Jan 17 22:14 cdrom -> sr0
[...]

The first character of each entry line reveals the file type; you can find a list of valid types in the ls manual.

As you're asking about the entries in /dev, the most common types there, besides directories and links, are character devices and block devices.

is there an integrated function to know the real size of a device file

As I already pointed out, what you mean with "real size" is the size of the content that's available "in" a device, of which the special files in /dev are just a representation to make access to them available to the system.

The answer to "how to get the size" is "depends on the content". E. g. for a disk partition, which is a block device, you usually mount it and can get the sizes (total, occupied, free space) using df. On the other hand not every block device can be mounted.

And without knowing what exactly you want to achieve that's all you'll get so far. It's now up to you to think about it, search the web (it's all there) and create a new question if you can't figure it out yourself.

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.