Character devices vs block devices
Character devices transfer data one character at a time. For example, a keyboard, a TTY device, /dev/urandom
Block devices transfer data one block at a time. Without getting too technical, a hard drive can only read or write data in blocks or sectors at time, never byte by byte. If you’re expecting data character by character, this can lead to a delay as you wait for a full block to be buffered in memory and then passed on to you. However, if you’re transferring files, sending over a full block at a time is much more efficient and ends up being faster. The default block size for NTFS and ext4 file systems is 4KB (4096 bytes).
Devices to know:
/dev/random or /dev/urandom generates pseudo-random data
Pseudo-random means it’s not truly random and therefore not suitable for sensitive cryptography operations but it’s close enough to be useful for everything else.
/dev/sd**
/dev is the folder where device files live, sd is the prefix for SATA disks, the letter after sd specifies the physical drive (a, b, c, d, etc) and the number specifies a partition on that physical drive (1, 2, 3, and so on).
/dev/nvme*n1p*
NVMe drives throw a wrench into a neat naming convention by adding something called namespaces. You’ll almost never actually use namespaces.
nvme is the prefix for NVMe storage, the number after nvme specifies the physical drive (0, 1, 2), the n and a number specifies a namespace (almost always n1), and the p + a number specifies a partition on that drive.
For example /dev/nvme3n1p6 would be the sixth partition on the fourth physical NVMe drive in a system.
Notice that partitions are counted starting from 1 on all types of drives, but NVMe physical drive numbers start from 0. This means that nvme3 is the fourth drive after nvme0, 1, and 2.
/dev/tty
A virtual device that simulates a terminal, used every time you open a terminal window or an SSH session
/dev/zero
Virtual device that returns empty characters when you read it.
/dev/null
Virtual device that discards anything you send to it. You can delete a file by moving it to /dev/null.
Figure 5-1 and 5-2
Commands to know:
| Command | Purpose | Usage |
|---|---|---|
| lsusb | List USB devices, including internal USB hubs built in to your motherboard | |
| lsblk | List block storage devices and their sizes | |
| mkfs | Write a new filesystem table to a partition | -t to specify a filesystem |
| df | (Disk Free) Show mounted disks and how much free space they have | |
| du | (Disk Usage) to calculate how much space a folder is taking up | -s to add all the output up and make it a single line, -h to use human-readable sizes like GB |
| mount | Mount a block device to a folder on your system | |
| umount | Unmount a drive | |
| fsck | Filesystem Check | |
| Process for setting up a new empty disk: |
- Write a partition table to the disk
- Create partitions on the disk
- Write a filesystem to each partition
- Mount each partition
Process for writing a filesystem to a disk: mkfs -t filesystemtype /dev/pathtopartiton For example, mkfs -t ntfs /dev/sdb1 The -t flag stands for filesystem type (NTFS, ext4, ExFAT, etc)
Process for mounting a disk: Create a folder under /mnt or /media mkdir /mnt/MovieDrive Mount the drive mount /dev/sdb1 /mnt/MovieDrive You have to specify a partition when using the mount command. Running mount /dev/sdb /mnt/MovieDrive won’t work.
Process to check and fix a filesystem:
fsck is the command used to check and fix filesystems.
For example fsck /dev/sdb2
You have to unmount a drive before you can check it.
To list all currently mounted filesystems, including virtual ones, run df -h. The -h stands for human-readable, meaning sizes will be converted into KB/MB/GB as needed.
To calculate how much space a given folder and its subfolders, run du -sh foldername.
-s stands for summary, which means it will add all the subfolder sizes together and only show one line of output.
-h stands for human-readable.
Most file system operations will require you to put sudo before the command, which means “run the following command with root permissions”. Sudo stands for “superuser do” or “substitute user do” depending on who you ask. You’ll be using sudo pretty much every day in Linux.
GParted is a GUI tool for editing partitions and filesystems in Linux, like Disk Management in Windows.
Each disk and each partition has its own unique UUID that can be used to identify it in commands. This is useful because Linux may mix up which drive is sda and which one is sdb after a reboot, and then mount /dev/sda2 where /dev/sdb2 should be.
/etc/fstab is a file where auto-mount information is written. We’ll work with it later. Any disk that’s mounted when you first boot up Mint has an entry in this file, including /boot/efi on sda1 and / on sda2.
Don’t worry about LVM or quotas.