Files and directories
Files
A file is a named list of bytes: a photo, a song, a text note. The OS does not care what the bytes mean. It stores them on the disk in fixed-size pieces called blocks, and finds them again when you ask.
Inodes
For each file the OS keeps a small record called an inode. It holds the file's size, owner, permissions, times (like last changed), and where its data blocks are on the disk. It does not hold the file's name. Each inode has a number, the inode number, and the OS uses that number to find the file.
Directories
A directory (folder) is a special file. Its content is a list of entries, each mapping a name to an inode number. A path like /home/ana/notes.txt is followed one step at a time: start at the root directory /, look up home, then ana, then notes.txt. Each directory also has . (itself) and .. (its parent).
Hard and soft links
Since names live in directories, one inode can have two names. A hard link is a second directory entry pointing to the same inode. The inode counts its names; deleting a name (unlink) lowers the count, and the data is freed only when it reaches zero. A symbolic link (soft link) is a tiny file that holds a path. If the target is deleted, the symbolic link points at nothing: it is broken.
File descriptors
To use a file, a process calls open. The OS gives back a small number, a file descriptor, and the process uses it for every later read and write. The OS remembers how far into the file the process has got, so each read carries on from there. Every process starts with 0, 1 and 2 already open (input, output, errors), so the first file opened usually gets 3. When done, the process calls close.
Remember
- A file is bytes; the OS stores it in blocks.
- The inode holds everything about a file except its name.
- A directory maps names to inode numbers.
- Hard link: another name, same inode. Symbolic link: a path that can break.
opengives a file descriptor; later calls use that number.