#compsci 

## Hierarchy
/bin - essential ready-to-run programs
/boot - kernel boot loader files
/[[Devices in Linux|dev]] - device files
/etc - core system configuration directory
/lib - holds library files that binaries can use
/media - attachment point for removable media
/mnt - temporarily mounted filesystems
/opt - optional software
/proc - information about currently running [[Processes in Linux|processes]]
/run - information about the running system since the last boot
/sbin - essential system binaries, only for root
/srv - site-specific data served by the system
/tmp - storage for temporary files
/usr - user installed software and utilities
/var - variable directory (for stuff that frequently changes)

## Types
Most linux file systems are **journaling file systems**: they keep track of changes not yet committed to the file system by recording the goal of such changes in a so-called journal. In the event of a system crash, such file systems can be brought back online more quickly with a lower likelihood of becoming corrupted.

Common file systems:
ext4/3/2
btrfs
NTFS
FAT32
HFS+ (for macs)
XFS
squashfs (compressed read-only fs for Linux)

## Anatomy of a disk
Every disk has a **partition table**, which tells the system how the disk is partitioned (where partitions begin and end, which are bootable, etc).
2 main partition table schemes:
- MBR (Master Boot Record)
- GPT (GUID Partition Table)

MBR:
- Traditional
- Has a limit of 4 primary partitionals
- Disks up to 2 TB

GPT:
- New
- No primary, extended, and logical types. Only one type.
- Each partition has a globally unique iD (GUID)
- Used in conjunction with [[UEFI]]

Filesystem:
- Boot block (contains info used to boot the OS) (MBR/GPT)
- Super block (comes after the boot block, contains info about the filesystem)
- Inode table (the database that manages our files)
- Data blocks (the actual data)

## Disk partitioning
Tools:
- fdisk
- gparted
- parted
- gdisk

Basics of parted:
$ sudo parted
select %disk%
mkpart %type% %start point% %end point%
resize %number% %start point% %end point%

To create a filesystem:

$ sudo mkfs.%type% %partiion%
e.g
$ sudo mkfs.ext4 /dev/sdb2

## Mounting
1. Create a mountpoint
2. $ sudo mount -t %type% %partition% %mountpoint%
3. $ sudo umount %mountpoint% OR $ sudo umount %partition% to unmount

To view the universally unique ID (UUID) of a block device:
$ sudo blkid
Then:

$ sudo mount UUID=%uuid% %mountpoint%

Linux has a special file of partitions that are mounted during startup: /etc/fstab
Each line represents one filesystem and is structured as follows:
- UUID
- Mount point
- Filesystem type
- Options
- Dump (default 0, used by the dump utility when to make a backup)
- Pass - used by [[fsck]] to decide what order filyesystems should checked in. if the value is 0, it won't be checked.

## swap
Swap guide:
1. mkswap %partition%
2. swapon %partition%
3. Optional: add an entry of the "sw" type to /etc/fstab
4. swapoff %partition% to remove

## Other tools
Disk usage: $ df (-h flag for a readable format)
Disk usage of the current directory: $ du (-h flag for a readable format)
Filesystem check & repari - [[fsck]]. $ sudo fsck %partition%

## Inodes
An inode (index node) is an entry in the inode table. There's an inode for every file in the system. These store everything about the file, except its filename and the file itself.
When a filesystem is created, space for inodes is allocated as well. The inode table can run out of space.
To check how many inodes are left, use $ df -i .

To view the inode number of a file, use $ ls -i
You can also see detailed info about a file with $ stat .

The purpose of inodes is to point to the actual data blocks of files. In a typical FS each inode contains 15 pointers, the first 12 pointers point directly to the data blocks. The 13th points to a block containing pointers to more blocks, the 14th pointer points to another nested block of pointers, and the 15th pointers point yet again to another block of points. The reason this is done this way is to keep the inode structure the same for every inode, but be able to reference files of different sizes. If you had a small file, you could find it quicker with the first 12 direct pointers, larger files can be found with the nests of pointers. Either way the structure of the inode is the same.

The root directory always has the inode of **2**.
![[Pasted image 20230307164236.png]]
