Unit 3: Linux File System and File Management

CSE105 — Creative Engineering Workshop 7 min read

Linux organises all storage into a single inverted tree rooted at /, governed by the Filesystem Hierarchy Standard (FHS, first published 1994). There are no drive letters; every device, partition and pseudo-file is mounted into this one tree, and every command in this unit either navigates it, inspects it, or changes it.

  • Everything is a file: ordinary data, directories, devices (/dev/sda) and kernel state (/proc) are all accessed through the same path-based interface.
  • Single rooted tree: one top node /; additional disks appear as subdirectories via mount points, not as separate roots.
  • Case-sensitive, /-separated: File.txt and file.txt differ; the forward slash separates path components and, alone, names the root.
  • Purpose-partitioned: each top-level directory has a defined role (binaries, configuration, logs), so the same file type lives in a predictable place across distributions.

II. Root Directory Structure and Important Directories

The fixed layout beneath / and what each branch holds.

A. Root Directory Structure

The root directory / is the single ancestor of every file; the FHS defines its immediate children so software and users can rely on stable locations.

  • Top-level layout: / contains standard directories (/bin, /etc, /home, ...); the root user's home is /root, distinct from /.
  • Static vs variable: /usr and /etc change rarely (shareable/static); /var and /tmp change constantly (variable) — a split that guides partitioning.
  • Reference by path: any file is named by its route from /, e.g. /home/asha/notes.txt walks root → home → asha → file.

B. Important Linux Directories (/bin, /sbin, /home, /etc, /usr, /var, /tmp, /opt, /lib)

Each standard directory stores one category of files.

  • /bin: essential user command binaries needed even in single-user mode — ls, cp, cat, mv.
  • /sbin: system-administration binaries, usually root-only — fdisk, ifconfig, reboot, mkfs.
  • /home: per-user home directories, e.g. /home/asha, holding personal files and dot-config.
  • /etc: system-wide text configuration files — /etc/passwd, /etc/fstab, /etc/hosts; contains no binaries.
  • /usr: secondary hierarchy for user programs installed after the base system — /usr/bin, /usr/lib, /usr/share.
  • /var: variable data that grows during operation — logs (/var/log), mail spools, print queues, caches.
  • /tmp: temporary scratch files, world-writable and typically cleared on reboot.
  • /opt: optional third-party or self-contained application packages, e.g. /opt/google/chrome.
  • /lib: shared libraries and kernel modules required by binaries in /bin and /sbin — libc.so, /lib/modules.

III. Paths

Two ways to name a file's location.

A. Absolute and Relative Paths

A path is the address of a file; it is written from the root or from where you currently stand.

  1. Absolute path: begins at root with a leading / and is unambiguous from anywhere.
    • Form: /home/asha/docs/report.txt — full route from /.
    • Use: scripts and configuration, where the current directory is unknown.
  2. Relative path: begins from the current working directory and has no leading /.
    • Form: docs/report.txt when already inside /home/asha.
    • Special symbols: . = current directory, .. = parent, ~ = your home directory.
TEXT
Current dir: /home/asha
Absolute : /home/asha/docs/report.txt
Relative : docs/report.txt   or   ../asha/docs/report.txt

IV. Navigation and File Management Commands

The core shell commands for moving through and altering the tree.

Each command below is invoked at the shell prompt; options follow a -.

A. pwd

Prints the absolute path of the current working directory.

  • Behaviour: pwd → /home/asha; confirms location before running relative commands.

B. cd

Changes the current working directory.

  • Forms: cd /etc (absolute), cd docs (relative), cd .. (up one), cd or cd ~ (home), cd - (previous directory).

C. ls

Lists directory contents.

  • Common options: ls -l long format (permissions, owner, size, date); ls -a includes hidden dot-files; ls -lh human-readable sizes; ls -R recurse into subdirectories.

D. mkdir

Creates new directories.

  • Usage: mkdir project; mkdir -p a/b/c creates the whole parent chain in one step.

E. rmdir

Removes empty directories only.

  • Usage: rmdir olddir — fails with "Directory not empty" if it contains files, unlike rm -r.

F. touch

Creates an empty file or updates a file's timestamp.

  • Usage: touch new.txt makes an empty file; running it on an existing file refreshes its modification time.

G. cat

Concatenates and displays file contents.

  • Usage: cat notes.txt prints a file; cat a.txt b.txt > c.txt joins two files; cat > f.txt writes typed input until Ctrl+D.

H. cp

Copies files and directories.

  • Usage: cp a.txt backup.txt (file copy); cp -r dir1 dir2 copies a directory recursively; cp -i prompts before overwrite.

I. mv

Moves or renames files and directories.

  • Usage: mv old.txt new.txt renames; mv file.txt /home/asha/docs/ moves; no separate rename command exists in Linux.

J. rm

Removes files and directories permanently.

  • Usage: rm file.txt; rm -r dir removes a directory and its contents; rm -f forces without prompt. Caution: there is no recycle bin — deletion is irreversible.

K. read

Reads a line of input from the terminal into a shell variable.

  • Usage: read name stores typed text in $name; read -p "Enter age: " age shows a prompt. Used inside scripts to capture user input.

L. su

Switches to another user, defaulting to the superuser (root).

  • Usage: su prompts for root's password; su - asha starts a login shell as asha, loading that user's environment; exit returns to the original user.

M. clear

Clears the terminal screen.

  • Usage: clear scrolls output away for a clean prompt; equivalent to the Ctrl+L shortcut.

V. File Searching using find

Locating files by attribute anywhere in the tree.

A. find

find searches a directory tree recursively and matches files by name, type, size, time or permission, then optionally acts on the results.

  • Syntax: find <start-path> <criteria> <action>.
  • By name: find /home -name "report.txt" — exact match; -iname ignores case.
  • By type: find . -type f files, -type d directories.
  • By size: find / -size +100M files larger than 100 MB.
  • By time: find . -mtime -7 modified within the last 7 days.
  • Acting on results: find . -name "*.tmp" -delete, or -exec rm {} \; to run a command per match.
TEXT
find /var/log -type f -name "*.log" -size +10M
# lists log files over 10 MB under /var/log

VI. Directory Visualization using tree

Displaying the hierarchy as an indented diagram.

A. tree

tree prints a directory and its subdirectories as an ASCII tree, making nesting visible at a glance (may require installation, e.g. sudo apt install tree).

  • Basic use: tree from the current directory draws branches with ├── and └──.
  • Depth limit: tree -L 2 shows only two levels down.
  • Include hidden: tree -a; directories only: tree -d.
  • Output: ends with a count, e.g. "3 directories, 5 files".

VII. history command

Recalling previously typed commands.

A. history

history lists commands the shell has recorded (stored in ~/.bash_history), each with an index number for quick reuse.

  • List: history prints numbered past commands; history 10 shows the last ten.
  • Re-run: !25 executes command number 25; !! repeats the last command; !ls repeats the most recent ls.
  • Clear: history -c empties the current session's list.

VIII. Disk Management Commands (du, df)

Measuring space used by files versus space available on filesystems.

A. du

du (disk usage) reports how much space files and directories consume.

  • Usage: du -h file human-readable size; du -sh dir a single summarised total for a directory; du -h --max-depth=1 per-subdirectory breakdown.
  • Focus: measures used space by path, from the bottom up.

B. df

df (disk free) reports space on mounted filesystems.

  • Usage: df -h shows each filesystem's size, used, available and use-% in human-readable units; df -h /home limits output to the filesystem holding that path.
  • Contrast with du: df reports capacity per mounted device (top-down), whereas du sums the size of chosen files (bottom-up); the two answer "how full is the disk" versus "what is filling it".