Unix and Shell Programming
Comprehensive exam revision notes for BCA-204N Unit 2 (Unix File System, Inodes & Storage Administration). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Unix and Shell Programming
Unix File System, Inodes & Storage Administration
Core Definitions & Key Concepts
What is an Inode (Index Node) in the Unix file system
An Inode is a fixed-size (typically 128 or 256 bytes) internal kernel data structure that stores all metadata about a file (permissions, owner, size, timestamps, block pointers) except its file name and file data.
Where is the file name stored if it is not inside the Inode
The file name is stored in a Directory file. A directory in Unix is simply a table mapping human-readable file names to their corresponding Inode numbers.
What are the three standard file timestamps tracked in an Inode
- atime (Access Time): Last time file contents were read.
- mtime (Modification Time): Last time file data contents were changed.
- ctime (Change Time): Last time file metadata or Inode was updated.
Hard Links and Soft (Symbolic) Links in Unix
| Feature | Hard Link (ln) | Symbolic Link (ln -s) |
|---|---|---|
| Inode Number | Shares the EXACT SAME Inode | Creates a NEW independent Inode |
| Across File Systems | Cannot cross file system boundaries | Can cross separate disks / file systems |
| Directories | Cannot link to directories (prevents loops) | Can link to directories |
| If Original Deleted | File data persists via remaining link | Becomes a broken (dangling) link |
Unix file permissions rwxr-xr-- in octal notation
Permissions: rwx (Owner = 4+2+1 = 7), r-x (Group = 4+0+1 = 5), r-- (Others = 4+0+0 = 4). Octal notation is 754.
What is the purpose of umask in Unix
umask (user mask) sets default permission restrictions on newly created files and directories. Default base is 666 (files) and 777 (dirs); actual permission = Base - umask (e.g., umask 022 gives 644 files and 755 dirs).
What do the SetUID (SUID) and SetGID (SGID) special permission bits do
- SUID (Octal 4000): Causes a program to execute with the permissions of the file owner (e.g.,
/bin/passwdruns as root to update/etc/shadow). - SGID (Octal 2000): Executes with permissions of the file group, or ensures new files in a directory inherit parent group ownership.
What is the Sticky Bit (Octal 1000) on directories like /tmp
When the sticky bit (t) is set on a shared directory (e.g., chmod 1777 /tmp), users can write files, but only the file owner or root can delete or rename their own files.
Conceptual & Architectural Analysis
The Unix File System Tree Hierarchy and Core Standard Directories
Standard Unix Directory Tree (FHS Standard)
- `/` (Root): The supreme top-level directory of the single hierarchical tree.
- `/bin`: Essential essential user command binaries needed in single-user mode (
ls,cp,sh). - `/sbin`: System administration binaries reserved for superuser root (
fdisk,reboot,init). - `/etc`: Host-specific static system configuration files (
passwd,fstab,network). - `/dev`: Special device files representing physical/virtual hardware (block and character devices).
- `/home`: User home directories storing personal documents and dotfile configs.
- `/var`: Variable dynamic runtime data that grows continuously (logs
/var/log, mail spools, print queues). - `/tmp`: Temporary scratch files accessible to all users, cleared on reboot.
- `/usr`: Secondary hierarchy containing user utilities, libraries (
/usr/lib), and headers (/usr/include).
The Inode Block Pointer Architecture (Direct, Single, Double, and Triple Indirect Pointers)
Inode Disk Block Addressing Mechanism
A standard Unix Inode contains 15 block pointers:
Inode Structure
+-----------------------+
| File Metadata |
+-----------------------+
| Direct Pointers (0-11)| ----> Direct Data Blocks (0 to 11)
+-----------------------+
| Single Indirect (12) | ----> Index Block of Direct Pointers ----> Data Blocks
+-----------------------+
| Double Indirect (13) | ----> Index Block ----> Index Blocks ----> Data Blocks
+-----------------------+
| Triple Indirect (14) | ----> Index Block ----> Index Blocks ----> Index Blocks ----> Data Blocks
+-----------------------+- 12 Direct Pointers: Point directly to the first 12 data blocks on disk (12 × 4 KB = 48 KB for small files instantly).
- 1 Single Indirect Pointer: Points to an index block containing block addresses (up to 1,024 pointers).
- 1 Double Indirect Pointer: Points to a block containing pointers to single indirect blocks (1,024 × 1,024 = 1,048,576 blocks).
- 1 Triple Indirect Pointer: Allows scaling to multi-terabyte file capacities.
File Types in Unix and How to Identify Them using ls -l
| Symbol | File Type | Description |
|---|---|---|
- | Regular / Ordinary file | Text documents, binary executables, media |
d | Directory file | Special file containing list of filenames and inode numbers |
l | Symbolic Link | Soft pointer file containing path string to target |
c | Character Special device | Character stream device without buffering (e.g., terminal /dev/tty, mouse) |
b | Block Special device | Block storage device with system caching (e.g., hard disk /dev/sda) |
p | Named Pipe (FIFO) | Inter-process communication pipe |
s | Socket | Network communication endpoint |
Storage Administration Commands: df, du, mount, and fdisk
df -h: Displays file system disk space usage (total, used, available, mount points) in human-readable gigabytes.du -sh /var: Computes recursive disk space used by a specific directory tree.mount /dev/sdb1 /mnt/usb: Attaches external storage partition to mount point/mnt/usb.umount /mnt/usb: Unmounts safely before physical removal.fdisk -l: Inspects physical disk partition tables (MBR/GPT).
In-Depth Analysis & Implementation
Demonstrate File Permission Administration using Octal and Symbolic Modes with Complete Terminal Examples
1# 1. Understanding Permission Representation2# -rwxr-xr-- 1 rohan students 1024 Oct 18 10:00 project.sh3# User (Owner) = rwx = 4+2+1 = 74# Group = r-x = 4+0+1 = 55# Others = r-- = 4+0+0 = 46# Octal: 7547
8# 2. Applying Permissions using chmod (Numeric / Octal Mode)9chmod 755 project.sh # rwxr-xr-x (Owner all, others read+exec)10chmod 600 private_key.pem # rw------- (Owner read+write only)11chmod 644 document.txt # rw-r--r-- (Owner read+write, others read)12
13# 3. Applying Permissions using Symbolic Mode14chmod u+x script.sh # Add execute for User15chmod g-w script.sh # Remove write for Group16chmod o=r script.sh # Set Others to read-only17chmod a+rx binary_app # Add read+exec for All (User, Group, Others)18
19# 4. Modifying Ownership with chown and chgrp20sudo chown rohan:developers script.sh # Sets owner to rohan, group to developers21sudo chown -R rohan /var/www/html # Recursive ownership transfer22
23# 5. Advanced Special Permissions (SUID, SGID, Sticky)24chmod 4755 /usr/local/bin/custom_admin # Set SUID (runs as owner)25chmod 2775 /shared/team_folder # Set SGID (new files inherit group)26chmod 1777 /tmp/shared_scratch # Set Sticky bit (users delete only their files)Write a C Program to Read and Display File Metadata using the stat() System Call
1#include <stdio.h>2#include <stdlib.h>3#include <sys/stat.h>4#include <time.h>5
6void print_file_stats(const char *filepath) {7 struct stat file_info;8
9 if (stat(filepath, &file_info) < 0) {10 perror("stat error");11 return;12 }13
14 printf("=========================================\n");15 printf(" FILE METADATA INSPECTOR: %s\n", filepath);16 printf("=========================================\n");17 printf("Inode Number: %lu\n", file_info.st_ino);18 printf("Total Size: %ld bytes\n", file_info.st_size);19 printf("Block Size: %ld bytes\n", file_info.st_blksize);20 printf("Blocks Allocated: %ld\n", file_info.st_blocks);21 printf("Hard Link Count: %lu\n", file_info.st_nlink);22 printf("Owner UID: %u\n", file_info.st_uid);23 printf("Group GID: %u\n", file_info.st_gid);24 printf("Permissions Mode: %o (Octal)\n", file_info.st_mode & 0777);25 printf("Last Modified: %s", ctime(&file_info.st_mtime));26 printf("Last Accessed: %s", ctime(&file_info.st_atime));27}28
29int main(int argc, char *argv[]) {30 if (argc < 2) {31 printf("Usage: %s <filename>\n", argv[0]);32 return 1;33 }34 print_file_stats(argv[1]);35 return 0;36}Inode Exhaustion vs Disk Space Exhaustion and How to Diagnose and Resolve Both
Inode Exhaustion vs Disk Space Exhaustion
| Condition | Cause | Diagnostic Command | Symptom |
|---|---|---|---|
| Disk Space Full | Large data files (video, core dumps, huge DBs) consume all bytes | df -h shows 100% used on partition | 'No space left on device' |
| Inode Table Full | Millions of tiny 0-byte or small files consume all Inode entries | df -i shows 100% Inodes used, but df -h shows free space | 'No space left on device' even with free gigabytes! |
Diagnosis and Recovery Commands
# 1. Check disk capacity vs Inode capacity
df -h # Check physical bytes
df -i # Check inode exhaustion
# 2. Find directories with millions of small files (e.g., session files)
find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n | tail -n 10
# 3. Clean millions of stale files safely via xargs
find /var/spool/clientmqueue -type f -mtime +30 -print0 | xargs -0 rm -fImportant Exam Points & Quick Revision
Draw the Inode pointer tree (direct, single, double, triple) and clearly differentiate hard links vs soft links for high exam scores.
100% left on this page — finishing it closes the gap for Unix File System, Inodes & Storage Administration.
Unix File System, Inodes & Storage Administration still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.