SyntaxPilot
Courses/CS Fundamentals for Placements/Lesson 35
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

CS Fundamentals for Placements · Lesson 35 of 90

File systems 1: files, directories and access

filefile attributesfile operationsfile descriptoropen file tablefile access methods (sequential, direct, indexed)directory structures (single-level, two-level, tree, acyclic graph, general graph)absolute and relative pathmountinghard link vs soft (symbolic) linkfile protectionrwx permissionsaccess control list

These are the notes for CS Fundamentals for Placements, episode 35. Until now we lived in RAM. RAM forgets everything when the power goes, so lasting data lives on the disk, and the disk is only a long row of numbered blocks. Today we invent the file and everything you use to reach it: file attributes and file operations, the file descriptor and the open file table, access methods (sequential, direct, indexed), directory structures (single-level, two-level, tree, acyclic graph, general graph), absolute and relative paths, mounting, hard links vs soft (symbolic) links, file protection with rwx permissions, and access control lists. Every term is explained in plain words the first time it appears, and there is a full glossary at the end.

All program outputs below are real: every C program was compiled with gcc 13.3 -O2 (the SyntaxPilot compiler's flags: gcc -O2 -Wno-unused-result main.c -o main -lm) and run on Linux x86-64 on 6 November 2026, each in a fresh, empty directory (as the online compiler does).

The problem: RAM forgets, and the disk has no names

The story: the school's record room

A school writes the day's marks on a whiteboard; a power cut wipes it. RAM is like that board: it is volatile (it loses its contents without power). Lasting data goes to secondary storage (a hard disk or SSD), which is non-volatile.

But a disk is a huge row of numbered blocks (fixed-size pieces of storage, typically 4 KB), like numbered slots in a record room. "Asha's marks are in slot 81,927, 81,928 and 90,004" is impossible to remember. So the school uses a labelled folder, Class 10 marks, and a clerk who knows which slots it uses. The folder is a file; the clerk is the file system.

Files

A file is a named, lasting collection of related bytes stored on secondary storage. It survives after the program ends and after the power goes. The file system is the part of the OS that organises files and maps their names to disk blocks.

To the OS, a file is just a sequence of bytes; what the bytes mean (text, an image, a program) is up to the programs that use it. The file type is often hinted by the extension (.txt, .c, .jpg), and executable files also carry a magic number (fixed first bytes, such as \x7fELF for Linux programs).

File attributes (metadata)

Besides its contents, every file has attributes, also called metadata (data about the data):

Attribute Meaning
Name the human-readable name, e.g. marks.txt
Identifier a unique number inside the file system (on Unix, the inode number)
Type regular file, directory, link, device, ...
Size current size in bytes (and blocks used)
Location which disk blocks hold the data
Owner (and group) the user (and group) the file belongs to
Protection who may read, write or execute it
Times created, last modified, last accessed

On Unix-like systems most of these live in the file's inode (index node): the on-disk record of a file. The name lives in the directory, not in the inode. stat() (and the shell command ls -l) reads the inode.

File operations

Operation What it does Unix system call
Create make a new, empty file open(..., O_CREAT), creat
Open prepare the file for use, get a file descriptor open
Read copy bytes from the file into memory read
Write copy bytes from memory into the file write
Seek (reposition) move the current position lseek
Close finish using it; free the descriptor close
Delete remove the name (the data is freed when no names and no opens are left) unlink
Truncate keep the file and its attributes, drop the contents truncate, O_TRUNC

Each is a system call: a request from a program to the operating system (episode 16). C's fopen, fprintf, fgets are library functions that call these underneath.

The file descriptor

The story: a cloakroom token

Searching the directories by name for every read would be slow. So you open a file once: like handing in your bag at a cloakroom and getting token 3. The counter's register says what token 3 means. Return the token and number 3 goes to the next person.

A file descriptor (fd) is that token: a small non-negative integer returned by open, used for every read, write, lseek and close after that.

Live: file descriptors are small integers, and the lowest free one is reused

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(void) {
    int a = open("marks.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    int b = open("notes.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    printf("marks.txt got fd %d, notes.txt got fd %d\n", a, b);
    close(a);                                   // fd 3 is free again
    int c = open("menu.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    printf("after close(%d), menu.txt got fd %d\n", a, c);
    fflush(stdout);
    write(1, "written straight to fd 1 (the screen)\n", 38);
    write(b, "OS notes\n", 9);                  // into notes.txt
    close(b); close(c);
}
marks.txt got fd 3, notes.txt got fd 4
after close(3), menu.txt got fd 3
written straight to fd 1 (the screen)
  • The first file got 3, not 0. Every process starts with three descriptors already open: 0 = standard input (stdin, normally the keyboard), 1 = standard output (stdout, the screen), 2 = standard error (stderr, also the screen, for error messages).
  • After close(3), the next open got 3 again. The POSIX standard (the common Unix interface) says open returns "the lowest file descriptor not currently open".
  • write(1, ...) printed on the screen: to the OS, the terminal is just another file. fflush(stdout) first empties C's printf buffer so the lines appear in order.
  • The flags: O_WRONLY open for writing only (O_RDONLY reading only, O_RDWR both), O_CREAT create if missing, O_TRUNC empty it if it exists; 0644 is the permission for a newly created file (see Permissions).

The open file table

There are three levels between a file descriptor and the data:

process A's fd table        system-wide open file table          the file
fd 3 ───────────────────▶  entry: offset 4, mode r, count 2 ──▶  inode: size, owner,
fd 5 (dup of 3) ────────┘                                         permissions, blocks
fd 4 ───────────────────▶  entry: offset 2, mode r, count 1 ──┘
  1. Per-process file descriptor table: one per process; fd number → pointer to an open-file entry.
  2. System-wide open file table: one entry per open() call. It holds the file offset, the access mode (read/write), and a reference count (how many descriptors point here).
  3. The in-memory inode (one per open file): size, permissions, block locations.

The file offset (current position) is where the next read or write happens; each read or write moves it forward. POSIX says each open creates a new open file description with its offset at the beginning of the file. dup(fd) creates a second descriptor that points at the same entry, so they share the offset. fork() also gives the child copies of the parent's descriptors that point at the same entries.

Live: two opens vs dup

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(void) {
    FILE *f = fopen("abc.txt", "w"); fputs("ABCDEFGH", f); fclose(f);
    int a = open("abc.txt", O_RDONLY);          // open #1
    int b = open("abc.txt", O_RDONLY);          // open #2, same file
    int d = dup(a);                             // a copy of fd a
    char x[3] = {0};
    read(a, x, 2); printf("read fd %d: %s\n", a, x);
    read(a, x, 2); printf("read fd %d: %s\n", a, x);
    read(b, x, 2); printf("read fd %d: %s   <- its own offset\n", b, x);
    read(d, x, 2); printf("read fd %d: %s   <- shares fd %d's offset\n", d, x, a);
}
read fd 3: AB
read fd 3: CD
read fd 4: AB   <- its own offset
read fd 5: EF   <- shares fd 3's offset
  • fd 3 reads AB, then CD: its offset moves.
  • fd 4 (a separate open) reads AB: its own offset, still at the start.
  • fd 5 (dup(3)) reads EF: it shares fd 3's entry and continues where fd 3 stopped.

Access methods

The story: tape, phone, book index

On a cassette tape you play past songs 1-3 to reach song 4. On a phone you tap song 4 directly. In a book you look up the index, get a page number, then go there.

Method How Good for Operations
Sequential access records one after another, in order text, logs, compilers reading source read next, write next, rewind
Direct (random / relative) access jump straight to record k (fixed-size records) databases, reservation systems read k, write k, seek
Indexed access a separate index maps a key to the record's position large files searched by key (roll number, account) look up key → seek → read

For direct access with fixed-size records: offset of record k = k × record size (records numbered from 0).

Live: sequential vs direct

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define REC 16                     // every record is exactly 16 bytes
int main(void) {
    const char *names[5] = {"Asha", "Ravi", "Priya", "Kabir", "Neha"};
    int fd = open("class.dat", O_RDWR | O_CREAT | O_TRUNC, 0644);
    char rec[REC + 1], line[REC];
    for (int i = 0; i < 5; i++) {              // write 5 records
        snprintf(line, REC, "%d %s %d", i, names[i], 60 + 7 * i);
        snprintf(rec, REC + 1, "%-15s\n", line);   // pad to 16 bytes
        write(fd, rec, REC);
    }
    rec[REC] = 0;
    lseek(fd, 0, SEEK_SET);                    // sequential: from the start
    for (int i = 0; i < 2; i++) { read(fd, rec, REC); printf("next     : %s", rec); }
    lseek(fd, 4 * REC, SEEK_SET);              // direct: jump to record 4
    read(fd, rec, REC); printf("record 4 : %s", rec);
    lseek(fd, 1 * REC, SEEK_SET);              // and back to record 1
    read(fd, rec, REC); printf("record 1 : %s", rec);
    close(fd);
}
next     : 0 Asha 60      
next     : 1 Ravi 67      
record 4 : 4 Neha 88      
record 1 : 1 Ravi 67      
  • snprintf(rec, REC + 1, "%-15s\n", line) pads every record to exactly 16 bytes (15 characters + newline), so record k starts at byte k × 16.
  • The first two reads are sequential (each read moves the offset 16 bytes on). Then lseek(fd, 4 * REC, SEEK_SET) jumps straight to record 4 (Neha) and then back to record 1 (Ravi): direct access, nothing read in between.
  • Indexed access adds an index file, for example roll 4 → record 4, possibly sorted so it can be binary-searched; very large files use a multi-level index (an index of the index). This is the idea behind database indexes (the DBMS episodes).

Directory structures

A directory (folder) is a special file that lists names and, for each name, where that file's record (inode) is. Operations: search for a file, create, delete, list, rename, traverse.

Structure Idea Problem it solves Problem it leaves
Single-level one directory for everyone simplest names must be unique system-wide; two users can't both have notes.txt
Two-level a master file directory (MFD) with one user file directory (UFD) per user no clash between users no folders inside; hard to share
Tree-structured directories inside directories, one root grouping, any depth (what you use today) a file can be in only one place
Acyclic graph a file or directory can appear in two places (shared), but no cycles (loops) sharing (a group project folder) a file now has several names; deleting needs care (reference counts)
General graph any links allowed, cycles possible most flexible searches may never end; a cycle can keep unreachable files "in use", needing garbage collection

Acyclic means "without cycles". Unix gets acyclic-graph sharing with links but forbids hard links to directories, so the graph stays loop-free (soft links can loop, but the kernel gives up after a fixed number of steps with "Too many levels of symbolic links").

/
├── home
│   ├── asha
│   │   └── college
│   │       └── cse
│   │           └── os.txt
│   └── ravi
└── etc

Paths

A path is the list of directories to walk through to reach a file, separated by / (Windows uses \).

  • Absolute path: starts at the root /; works from anywhere. /home/asha/college/cse/os.txt.
  • Current (working) directory: where the process is right now (chdir changes it; the shell commands are cd and pwd).
  • Relative path: starts from the current directory. From /home/asha/college: cse/os.txt.
  • . means this directory; .. means the parent directory (one level up).

Live: relative and absolute paths

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
void try(const char *p) {
    FILE *f = fopen(p, "r");
    printf("%-24s %s\n", p, f ? "found" : strerror(errno));
}
int main(void) {
    mkdir("college", 0755); mkdir("college/cse", 0755);
    FILE *f = fopen("college/cse/os.txt", "w"); fputs("paging\n", f); fclose(f);
    chdir("college");                  // now we are inside college
    try("cse/os.txt");                 // relative: starts from here
    try("os.txt");                     // not in college itself
    try("../college/cse/os.txt");      // .. = the parent directory
    try("/etc/passwd");                // absolute: starts from /
}
cse/os.txt               found
os.txt                   No such file or directory
../college/cse/os.txt    found
/etc/passwd              found
  • After chdir("college"), cse/os.txt is found relative to college.
  • os.txt alone fails with No such file or directory: it is one level further down, in cse. The most common file bug: a relative path from the wrong current directory.
  • ../college/cse/os.txt goes up one level and back down; /etc/passwd is absolute and works from anywhere.
  • strerror(errno) turns the error number from the failed call into its message (perror prints the same message to stderr).

Mounting

A pen drive or second disk has its own file system. Mounting attaches that whole file system at a directory of the existing tree, called the mount point. After mount /dev/sdb1 /media/pendrive, the drive's photos folder is /media/pendrive/photos. Whatever was inside the mount point is hidden until unmount (umount). Unmount (eject) before removing a drive, so data the OS is still holding in memory is written first.

The story: two contact names vs a sticky note

You saved one phone number as Asha and as Class rep. Delete Asha and Class rep still works: both hold the number. A sticky note saying "see the contact Asha" is useless once Asha is deleted.

  • Hard link: a second directory entry (name) for the same inode. All names are equal; there is no "original". The inode's link count says how many names it has; the data is freed only when the count reaches 0 (and no process has the file open). Shell: ln song.txt hard.txt.
  • Soft link (symbolic link, symlink): a small separate file, with its own inode, whose contents are a path to another name. Opening it follows the path. If the target is deleted it becomes a dangling link. Shell: ln -s song.txt soft.txt.

Live: delete the original

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
void show(const char *name) {
    struct stat s; char buf[32] = {0};
    if (stat(name, &s) != 0) { printf("%s: %s\n", name, strerror(errno)); return; }
    FILE *f = fopen(name, "r"); fgets(buf, sizeof buf, f); fclose(f);
    printf("%s  links %ld  says: %s", name, (long)s.st_nlink, buf);
}
int main(void) {
    FILE *f = fopen("song.txt", "w"); fputs("la la la\n", f); fclose(f);
    link("song.txt", "hard.txt");          // a second NAME for the same file
    symlink("song.txt", "soft.txt");       // a small file that holds a PATH
    struct stat a, b; char path[32] = {0};
    stat("song.txt", &a); stat("hard.txt", &b); readlink("soft.txt", path, 31);
    printf("song and hard: same inode? %s\n", a.st_ino == b.st_ino ? "yes" : "no");
    printf("soft.txt holds the path: %s\n", path);
    show("song.txt"); show("hard.txt"); show("soft.txt");
    unlink("song.txt");                    // delete the original name
    printf("-- deleted song.txt --\n");
    show("hard.txt"); show("soft.txt");
}
song and hard: same inode? yes
soft.txt holds the path: song.txt
song.txt  links 2  says: la la la
hard.txt  links 2  says: la la la
soft.txt  links 2  says: la la la
-- deleted song.txt --
hard.txt  links 1  says: la la la
soft.txt: No such file or directory
  • song.txt and hard.txt share one inode; the link count is 2.
  • readlink shows that soft.txt holds just the path song.txt. (stat follows the link, so it reports the target's link count, 2; lstat would describe the link itself.)
  • After unlink("song.txt"), hard.txt still reads la la la with 1 link left, while soft.txt fails: No such file or directory.
Hard link Soft (symbolic) link
What it is another name for the same inode a file holding a path
Own inode? no yes
Link count goes up unchanged
Original deleted still works dangling
Across file systems no (inode numbers are per file system) yes
To a directory not allowed (loops) allowed
Shell ln a b ln -s a b

File protection and permissions

File protection controls who may do what with a file. Unix-like systems use:

  • three kinds of access: r = read, w = write, x = execute (run it as a program; for a directory, x means "may enter / search it");
  • three classes of users: owner (u), group (g), others (o).

That's 9 permission bits, written as three octal digits with r = 4, w = 2, x = 1 added per class (octal = base 8, episode 2):

  owner  group  others
  r w -  r - -  r - -
  4 2 0  4 0 0  4 0 0   →  6 4 4   (chmod 644)

Common modes: 644 (rw-r--r--, documents), 600 (rw-------, private), 755 (rwxr-xr-x, programs and directories), 700 (rwx------, private scripts).

Live: octal ↔ rwx

#include <stdio.h>
#include <sys/stat.h>
void rwx(const char *name) {
    struct stat s; stat(name, &s);
    const char *c = "rwxrwxrwx"; char out[10] = {0};
    for (int i = 0; i < 9; i++)                       // 9 permission bits
        out[i] = (s.st_mode & (0400 >> i)) ? c[i] : '-';
    printf("%-10s %04o  %s\n", name, s.st_mode & 0777, out);
}
int main(void) {
    FILE *f = fopen("result.txt", "w"); fclose(f);
    chmod("result.txt", 0644); rwx("result.txt");     // rw- r-- r--
    chmod("result.txt", 0640); rwx("result.txt");     // rw- r-- ---
    chmod("result.txt", 0754); rwx("result.txt");     // rwx r-x r--
    chmod("result.txt", 0600); rwx("result.txt");     // rw- --- ---
}
result.txt 0644  rw-r--r--
result.txt 0640  rw-r-----
result.txt 0754  rwxr-xr--
result.txt 0600  rw-------
  • s.st_mode & (0400 >> i) tests the 9 permission bits from owner-read (0400) down to others-execute (0001).
  • In C, a leading 0 makes a number octal: 0644.
  • The superuser (root) is not limited by these bits for reading and writing; that's why permission demos must run as a normal user.

Access control lists (ACLs)

Owner/group/others is coarse. "Only the class teacher and one TA may edit marks.txt, students may read it" is hard to express. An access control list (ACL) attaches to each file a list of (user or group → rights) entries:

marks.txt
  teacher : read, write
  ta_ravi : read, write
  students: read

On Linux, setfacl sets ACL entries and getfacl shows them (for example setfacl -m u:ta_ravi:rw marks.txt). ACLs give fine control at the cost of longer lists to store and check. Other protection ideas: passwords per file (hard to manage), and capabilities (a token listing what a holder may do).

The whole chain

Problem Invention
RAM forgets; disk blocks have no names the file, file system, attributes, operations
Don't search by name on every read open once → file descriptor
Two opens of one file open file table, file offset, dup
Read in order, or jump sequential, direct, indexed access
Too many files in one list single-level → two-level → tree → acyclic graph → general graph
Naming the way to a file absolute and relative paths, . and ..
A second disk mounting
One file, two names hard links, soft links
Who may use it rwx permissions, ACLs

Common mistakes

Mistake What you see Why Fix
chmod("f", 644); mode becomes 1204 (ls -l shows --w----r-T) 644 without a leading 0 is decimal; 644 = octal 1204 chmod("f", 0644);
Opening a relative path from the wrong directory No such file or directory relative paths start at the current directory check getcwd, or use an absolute path
Reading through a soft link whose target was deleted No such file or directory the link only holds a name recreate the target, or use a hard link
link() on a directory Operation not permitted hard links to directories could create loops use symlink()
Not checking open's result later read fails with Bad file descriptor open returns -1 on failure if (fd < 0) { perror("open"); return 1; }
Never calling close eventually Too many open files each process has a limit on open descriptors close every fd you open
Thinking fd 0 is your first file wrong fd answers 0, 1, 2 are already open first open returns 3

The first and fourth rows were run in the video:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
int main(void) {
    FILE *f = fopen("result.txt", "w"); fclose(f);
    chmod("result.txt", 644);               // oops: no leading 0
    struct stat s; stat("result.txt", &s);
    printf("asked for 644, got %04o\n", s.st_mode & 07777);
    mkdir("photos", 0755);
    if (link("photos", "photos2") != 0)     // hard link to a directory?
        printf("link photos: %s\n", strerror(errno));
}
asked for 644, got 1204
link photos: Operation not permitted

Mini project: a tiny ls -l

Write a C program that takes a directory name, lists every entry with opendir/readdir, and for each one uses lstat to print: the type (- file, d directory, l link), the rwx string, the link count, the size, and the name; for symbolic links, also print -> target using readlink. Then add: -R to walk subdirectories recursively (skip . and ..), and a check that detects a symbolic-link loop (stop after, say, 40 levels).

Practice

1. What does chmod 640 allow? Write it as rwx.

Solution

6 = 4 + 2 = rw- (owner), 4 = r-- (group), 0 = --- (others): rw-r-----. The owner can read and write, the group can read, everyone else gets nothing.

2. A process has fds 0, 1 and 2 open. It calls close(1) and then open("log.txt", O_WRONLY | O_CREAT, 0644). Which fd does open return, and what happens to later printf output?

Solution

open returns the lowest free descriptor: 1. So log.txt is now standard output, and later printf output (after fflush) goes into log.txt instead of the screen. This is exactly how the shell implements program > log.txt (redirection).

3. Which link survives deleting the original, and why?

Solution

The hard link. It is another name for the same inode; deleting one name only lowers the link count (2 → 1), and the data is freed only at 0. A soft link stores the name song.txt; once that name is gone, following it fails with No such file or directory (live in the video).

4. Fixed-size records of 32 bytes, numbered from 0. At what byte offset does record 10 start? Which call jumps there?

Solution

10 × 32 = 320. lseek(fd, 10 * 32, SEEK_SET); then read(fd, buf, 32);

5. From /home/asha/college, write the relative path to /home/ravi/notes.txt.

Solution

Up two levels to /home, then down: ../../ravi/notes.txt.

6. Name one advantage and one problem of each directory structure: single-level, two-level, tree, acyclic graph.

Solution

Single-level: simple / name clashes. Two-level: per-user names / no grouping, hard sharing. Tree: unlimited grouping, efficient search / no sharing in two places. Acyclic graph: sharing / several names per file, deletion needs reference counts (and dangling links for soft links).

Quick quiz

chmod 751 run.sh

// what can 'others' do with run.sh?

A. read and execute B. only execute C. read, write and execute D. nothing

Answer

B. only execute. 7 = rwx (owner), 5 = r-x (group), 1 = --x (others).

Interview questions

  1. What is a file? What are file attributes? A named, lasting collection of related bytes on storage; attributes (metadata) are name, identifier, type, size, location, owner, protection and times.
  2. What is a file descriptor? A small integer returned by open that indexes the process's descriptor table; used for read/write/close. 0, 1, 2 are stdin, stdout, stderr.
  3. Explain the per-process and system-wide open file tables. The per-process table maps fds to entries in the system-wide table, which holds the offset, mode and reference count and points to the in-memory inode.
  4. What does dup do? How is it different from opening the file again? It makes a new fd for the same open-file entry, sharing the offset; a second open creates a new entry with its own offset.
  5. Sequential vs direct vs indexed access? In order / jump to record k (offset = k × size) / look up the key in an index, then jump.
  6. Compare the directory structures. Single-level, two-level (MFD + UFD), tree, acyclic graph (sharing, no loops), general graph (loops; needs garbage collection).
  7. Absolute vs relative path? From the root / vs from the current working directory; . = here, .. = parent.
  8. What is mounting? Attaching a file system at a directory (mount point) of the existing tree.
  9. Hard link vs soft link? Same inode and link count vs a separate file holding a path; hard links survive deletion of the other name, can't cross file systems or point to directories; soft links can dangle but can cross file systems and point to directories.
  10. Explain rwx permissions and chmod 755. r=4, w=2, x=1 for owner, group, others; 755 = rwxr-xr-x.
  11. What is an ACL and why use it? A per-file list of users/groups and their rights; finer than owner/group/others.
  12. What happens to a file's data when you delete it while a program still has it open? The name is removed, but the inode and data stay until the last descriptor is closed (the link count and open count both reach 0).

Glossary

Term Meaning in plain words Example
Volatile Loses its contents without power RAM
Secondary storage Lasting (non-volatile) storage HDD, SSD
Block A fixed-size piece of the disk 4 KB block 81,927
File A named, lasting collection of related bytes marks.txt
File system The OS part that organises files and maps names to blocks ext4
Extension The part of a name after the dot, hinting the type .c
Magic number Fixed first bytes that identify a file format \x7fELF
File attributes / metadata Data about a file: name, size, owner, times, ... ls -l output
Inode (index node) The on-disk record of a Unix file (everything but the name) inode with links 2
Inode number The file's unique number in its file system same for song.txt and hard.txt
System call A request from a program to the OS open, read
Create / open / read / write / close The basic file operations open(..., O_CREAT)
Seek (reposition) Move the current position in a file lseek(fd, 64, SEEK_SET)
Truncate Keep the file, drop its contents O_TRUNC
Delete (unlink) Remove a name; data freed when no names/opens remain unlink("song.txt")
File descriptor (fd) Small integer returned by open 3
Standard input/output/error fds 0, 1, 2, open in every process keyboard, screen
POSIX The standard Unix programming interface "lowest fd not open"
Per-process fd table A process's table: fd → open-file entry fd 3 → entry
Open file table System-wide table of open files: offset, mode, count one entry per open
File offset Where the next read/write happens 4 after reading ABCD
Reference count How many fds point to an entry 2 after dup
dup Make a second fd sharing the same entry fd 5 = dup(3)
Access method How records of a file are read sequential, direct, indexed
Sequential access Records in order reading a log
Direct (random) access Jump to record k; offset = k × record size record 4 at byte 64
Indexed access Look up a key in an index, then jump roll 4 → record 4
Record One fixed unit of data in a file 4 Neha 88 (16 bytes)
Directory (folder) A special file listing names → file records college
Single-level directory One directory for everyone name clashes
Two-level directory Master file directory + one directory per user MFD, UFD
Tree-structured directory Directories within directories, one root /home/asha
Acyclic graph directory Sharing allowed, no cycles a shared project folder
General graph directory Any links, cycles possible folder inside itself
Root directory The top of the tree, / /
Path The directories to walk through to a file cse/os.txt
Absolute path A path from the root /etc/passwd
Relative path A path from the current directory ../college
Current (working) directory Where a process is right now after chdir("college")
. and .. This directory and the parent directory ../college
Mounting Attaching a file system at a directory pen drive at /media/pendrive
Mount point The directory a file system is attached at /media/pendrive
Hard link Another name for the same inode ln song.txt hard.txt
Link count How many names an inode has 2, then 1
Soft (symbolic) link A small file holding a path ln -s song.txt soft.txt
Dangling link A soft link whose target is gone soft.txt after unlink
File protection Controlling who may do what with a file permissions
r / w / x Read, write, execute (enter, for directories) rwx
Owner / group / others The three user classes u, g, o
Octal Base 8; one digit per 3 bits 0644
chmod Change a file's permission bits chmod 755 run.sh
Superuser (root) The administrator account, not limited by rwx uid 0
Access control list (ACL) Per-file list of users/groups and their rights setfacl -m u:ta_ravi:rw

Summary

  • A file is a named, lasting collection of bytes with attributes (stored in the inode) and a fixed set of operations (system calls).
  • open returns a file descriptor, the lowest free one (live: 3, 4, then 3 again; 0, 1, 2 are stdin, stdout, stderr). Each open creates a new open file table entry with its own offset; dup shares one (live: AB, CD, AB, EF).
  • Files are read by sequential, direct (live: jump to record 4 at byte 64) or indexed access.
  • Directories grew from single-level and two-level into trees, acyclic graphs and general graphs; files are named by absolute or relative paths (live: os.txt not found from college), and other disks join the tree by mounting.
  • A hard link is another name for the same inode; a soft link holds a path and dangles when the target is deleted (live).
  • rwx permissions (r 4, w 2, x 1, for owner, group, others; live: 0644 = rw-r--r--) and ACLs control access; write modes in C with a leading 0.

Next episode: File systems 2: contiguous, linked and indexed allocation, FAT, Unix inodes, maximum file size numericals, free-space management and journaling.