Unit 3: Linux File System and File Management
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.txtandfile.txtdiffer; 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:
/usrand/etcchange rarely (shareable/static);/varand/tmpchange constantly (variable) — a split that guides partitioning. - Reference by path: any file is named by its route from
/, e.g./home/asha/notes.txtwalks 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/binand/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.
- 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.
- Form:
- Relative path: begins from the current working directory and has no leading
/.- Form:
docs/report.txtwhen already inside/home/asha. - Special symbols:
.= current directory,..= parent,~= your home directory.
- Form:
Current dir: /home/asha
Absolute : /home/asha/docs/report.txt
Relative : docs/report.txt or ../asha/docs/report.txtIV. 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),cdorcd ~(home),cd -(previous directory).
C. ls
Lists directory contents.
- Common options:
ls -llong format (permissions, owner, size, date);ls -aincludes hidden dot-files;ls -lhhuman-readable sizes;ls -Rrecurse into subdirectories.
D. mkdir
Creates new directories.
- Usage:
mkdir project;mkdir -p a/b/ccreates 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, unlikerm -r.
F. touch
Creates an empty file or updates a file's timestamp.
- Usage:
touch new.txtmakes an empty file; running it on an existing file refreshes its modification time.
G. cat
Concatenates and displays file contents.
- Usage:
cat notes.txtprints a file;cat a.txt b.txt > c.txtjoins two files;cat > f.txtwrites typed input untilCtrl+D.
H. cp
Copies files and directories.
- Usage:
cp a.txt backup.txt(file copy);cp -r dir1 dir2copies a directory recursively;cp -iprompts before overwrite.
I. mv
Moves or renames files and directories.
- Usage:
mv old.txt new.txtrenames;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 dirremoves a directory and its contents;rm -fforces 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 namestores typed text in$name;read -p "Enter age: " ageshows a prompt. Used inside scripts to capture user input.
L. su
Switches to another user, defaulting to the superuser (root).
- Usage:
suprompts for root's password;su - ashastarts a login shell asasha, loading that user's environment;exitreturns to the original user.
M. clear
Clears the terminal screen.
- Usage:
clearscrolls output away for a clean prompt; equivalent to theCtrl+Lshortcut.
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;-inameignores case. - By type:
find . -type ffiles,-type ddirectories. - By size:
find / -size +100Mfiles larger than 100 MB. - By time:
find . -mtime -7modified within the last 7 days. - Acting on results:
find . -name "*.tmp" -delete, or-exec rm {} \;to run a command per match.
find /var/log -type f -name "*.log" -size +10M
# lists log files over 10 MB under /var/logVI. 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:
treefrom the current directory draws branches with├──and└──. - Depth limit:
tree -L 2shows 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:
historyprints numbered past commands;history 10shows the last ten. - Re-run:
!25executes command number 25;!!repeats the last command;!lsrepeats the most recentls. - Clear:
history -cempties 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 filehuman-readable size;du -sh dira single summarised total for a directory;du -h --max-depth=1per-subdirectory breakdown. - Focus: measures used space by path, from the bottom up.
B. df
df (disk free) reports space on mounted filesystems.
- Usage:
df -hshows each filesystem's size, used, available and use-% in human-readable units;df -h /homelimits output to the filesystem holding that path. - Contrast with du:
dfreports capacity per mounted device (top-down), whereasdusums the size of chosen files (bottom-up); the two answer "how full is the disk" versus "what is filling it".
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →