-1

Why every device in Linux is a file or folder ? what are the advantages ? and what is the necessity of /dev/sda1 , /dev/sda2 or /dev/sda3 file ? it's size is 0 bytes .and after mounting the hard disk the files are located in /media folder . why /dev/sda1 or such files necessary?

closed as too broad by heemayl, Kusalananda, Stephen Harris, jimmij, Jeff Schaller Mar 25 at 0:14

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to U&L! Folders (directories) are files as well. Apart from that, your question is comprised of multiple questions which makes this too broad to answer. Please ask a single question at a time. Good luck. – heemayl Mar 23 at 16:11
  • thanks for replaying @heemayl . questions were spinning around my head that's why I posted all the questions at once . if I won't get any good answer then I definitely post the question in parts. thanks again . BTW I am from Bangladesh too – sabbir Mar 23 at 16:18
  • Great! Nice to see you here :) – heemayl Mar 23 at 16:30
  • What would the alternative be? – Kusalananda Mar 23 at 16:34
  • This is rather broad, but see also e.g. A layman's explanation for “Everything is a file” — what differs from Windows? – ilkkachu Mar 23 at 19:07
4

I'll try to answer the question "Why every device in Linux is a file" specifically geared towards data storage (as the question takes them as an example). More in-depth answers regarding the POSIX philosophy may be interesting, too.

The base concept here is, that virtually every operation in a computer essentially comes down to moving data around. Data is read, transformed and written. Of course, depending on which hardware is origin of the data read or target of the data written, the real-world outcome is totally different.

Historically, popular devices for data storage (assume a hard-drive) were handled by the SCSI driver. That is why storage is commonly prefixed with sd. You then want a logical "software" representation of the individual physical "hardware" device. You just number them, starting with a. Concatenated, a hard-drive may be referred to as sda.

Now you have a "file" which you can read data from (writing is analogous). If you read the first byte from /dev/sda, you will actually read the byte which is actually sitting on the very beginning of that data storage device (block sizes ignored for the sake of simplicity). You can try this yourself by starting sudo hexdump -C /dev/sda | less. Of course, most of the content will be gibberish to the naked eye.
However if you interpreted that gibberish so you know where a partition starts and mount that partition, a program (the filesystem driver) interprets the data for you and presents a nice tree-like structure with folders and files you can then read data from in return.

Fun fact: This also holds (to varying degrees) for other kinds of devices (character devices) like keyboards and mice (they "produce" data you can read): sudo hexdump -C /dev/input/mouse0).
Or the main memory.
Or even the graphics memory (framebuffer): I occasionally enjoy switching to one of the virtual consoles (Ctrl+Alt+F1) and have my graphics card interpret and display random numbers via sudo dd if=/dev/urandom of=/dev/fb0 bs=1M count=1

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