New batchZero to DevOps starts 29 Nov · limited seatsReserve your seat
Cheat sheetsLinuxLinux
FreeSheet 01 of 7

Disks & Filesystems: Complete Cheat Sheet

A comprehensive developer cheatsheet covering Linux storage and filesystem management, featuring essential CLI commands and operational workflows for disk partitioning (fdisk, gdisk, parted), filesystem creation (mkfs), monitoring space utilization (df, du, lsblk), managing mounts (mount, /etc/fstab), executing filesystem repairs (fsck), and handling swap space and file archiving.

Understanding Linux Storage

Filesystem Types

TypeDescriptionUse Case
ext4Fourth Extended FilesystemDefault for most Linux distros
xfsHigh-performance 64-bit journalingEnterprise, large filesystems
btrfsB-tree FilesystemAdvanced features (snapshots, checksums)
vfatFAT32USB drives, compatibility with Windows
ntfsNew Technology FilesystemWindows drives (read/write with ntfs-3g)
tmpfsTemporary filesystem in RAM/tmp, /run
swapSwap spaceVirtual memory

Device Naming Conventions

Device TypeName FormatExample
SATA/SAS/SCSI/dev/sdX/dev/sda, /dev/sdb
NVMe/dev/nvmeXnY/dev/nvme0n1, /dev/nvme1n2
IDE/dev/hdX/dev/hda, /dev/hdb
Virtual (KVM)/dev/vdX/dev/vda, /dev/vdb
CD/DVD/dev/srX/dev/sr0, /dev/sr1
Loop Device/dev/loopX/dev/loop0, /dev/loop1

Partition Table Types

TypeDescriptionMax SizeMax Partitions
MBRMaster Boot Record2 TB4 primary (or 3 primary + 1 extended)
GPTGUID Partition Table9.4 ZB128 (or more)

Disk Space Monitoring

df — Display Disk Space Usage

Flags: -h (human-readable), -H (SI units), -T (show filesystem type), -i (inode usage), -x (exclude), --total (show total)

bash
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   25G   23G  52% /
/dev/sdb1       500G  200G  300G  40% /home
tmpfs           7.8G  1.2M  7.8G   1% /tmp

Shows disk space usage for all mounted filesystems. -h makes it human-readable.

bash
$ df -T
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda1      ext4      50G   25G   23G  52% /
/dev/sdb1      xfs      500G  200G  300G  40% /home
tmpfs          tmpfs    7.8G  1.2M  7.8G   1% /tmp

-T shows the filesystem type for each mounted device.

bash
$ df -i
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/sda1      3276800 234567 3042233   8% /
/dev/sdb1     26843545 123456 26720089   1% /home
tmpfs          2048000    123 2047877   1% /tmp

-i shows inode usage — important when you have many small files.

Inode Usage Alert: If inodes are 100% used, you can't create new files even if you have free space.


du — Estimate File/Directory Space Usage

Flags: -h (human), -s (summary), -a (all files), -c (grand total), -d N (max depth N), --exclude=PATTERN

bash
$ du -sh /home/bob
456M    /home/bob

Shows total size of a directory. -s = summary, -h = human-readable.

bash
$ du -sh /var/* | sort -hr | head -5
2.3G    /var/log
1.2G    /var/cache
456M    /var/lib
123M    /var/www
45M     /var/tmp

Find the largest directories in /var sorted by size.

bash
$ du -h --max-depth=1 /home
4.5G    /home/alice
2.3G    /home/bob
1.2G    /home/charlie
8.0G    /home

--max-depth=1 shows sizes of immediate subdirectories only.

bash
$ du -ah /home/bob | sort -hr | head -10
456M    /home/bob
123M    /home/bob/Videos
89M     /home/bob/Downloads
45M     /home/bob/Documents

-a includes files, not just directories. Great for finding large files.


lsblk — List Block Devices

Flags: -f (show filesystems), -m (human-readable), -t (topology), -p (full path), -o (custom output)

bash
$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0   100G  0 disk
├─sda1        8:1    0    50G  0 part /
├─sda2        8:2    0     2G  0 part [SWAP]
└─sda3        8:3    0    48G  0 part /home
sdb           8:16   0   500G  0 disk
└─sdb1        8:17   0   500G  0 part /backup
nvme0n1     259:0    0   250G  0 disk
└─nvme0n1p1 259:1    0   250G  0 part /data

Shows all block devices in a tree structure. Easy to understand disk layout.

bash
$ lsblk -f
NAME        FSTYPE  LABEL      UUID                                 MOUNTPOINT
sda
├─sda1      ext4    rootfs     12345678-1234-1234-1234-123456789012 /
├─sda2      swap    swap       abcd1234-abcd-1234-abcd-123456789012 [SWAP]
└─sda3      ext4    home       98765432-9876-9876-9876-987654321098 /home
sdb
└─sdb1      xfs     backup     56789012-5678-5678-5678-567890123456 /backup

-f shows filesystem type, label, and UUID for each partition.

bash
$ lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,LABEL
NAME        SIZE TYPE MOUNTPOINT LABEL
sda         100G disk
├─sda1       50G part /          rootfs
├─sda2        2G part [SWAP]     swap
└─sda3       48G part /home      home

Custom output format — show only the columns you need.


blkid — Locate/Print Block Device Attributes

Flags: -s (show specific attribute), -o (output format), -p (probe)

bash
$ blkid
/dev/sda1: UUID="12345678-1234-1234-1234-123456789012" TYPE="ext4" PARTUUID="abcd-1234"
/dev/sda2: UUID="abcd1234-abcd-1234-abcd-123456789012" TYPE="swap" PARTUUID="efgh-5678"
/dev/sda3: UUID="98765432-9876-9876-9876-987654321098" TYPE="ext4" PARTUUID="ijkl-9012"
/dev/sdb1: UUID="56789012-5678-5678-5678-567890123456" TYPE="xfs" PARTUUID="mnop-3456"

Shows UUIDs and filesystem types for all block devices.

bash
$ blkid -s UUID -o list
device     uuid                                   type
/dev/sda1  12345678-1234-1234-1234-123456789012  ext4
/dev/sda2  abcd1234-abcd-1234-abcd-123456789012  swap
/dev/sda3  98765432-9876-9876-9876-987654321098  ext4
/dev/sdb1  56789012-5678-5678-5678-567890123456  xfs

-s UUID -o list shows just UUIDs in a clean table.


Disk Partitioning

fdisk — Partition Table Manipulator (MBR & GPT)

Flags: -l (list partitions), /dev/sda (target disk)

Interactive Commands:

KeyAction
nNew partition
dDelete partition
pPrint partition table
wWrite changes and exit
qQuit without saving
tChange partition type
bash
$ fdisk -l /dev/sda
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 12345678-1234-1234-1234-123456789012

Device         Start       End   Sectors  Size Type
/dev/sda1       2048  104857600 104855552   50G Linux filesystem
/dev/sda2  104857600  109051904   4194304    2G Linux swap
/dev/sda3  109051904  209715200 100663296   48G Linux filesystem

Lists all partitions on /dev/sda.

Interactive Partitioning Session:

bash
$ fdisk /dev/sdb

Command (m for help): p
Disk /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 sectors
...

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-1048575999, default 2048):
Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): +100G

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Creates a new 100GB partition on /dev/sdb.


gdisk — GPT Partition Table Manipulator

Flags: -l (list partitions), /dev/sda (target disk)

Interactive Commands:

KeyAction
nNew partition
dDelete partition
pPrint partition table
wWrite changes and exit
qQuit without saving
tChange partition type
bash
$ gdisk -l /dev/nvme0n1
GPT fdisk (gdisk) version 1.0.9

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Disk /dev/nvme0n1: 488397168 sectors, 232.9 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 12345678-1234-1234-1234-123456789012
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 488397134

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       488397134   232.9 GiB   8300  Linux filesystem

gdisk is the modern tool for GPT disks (supports >2TB drives).

bash
$ gdisk /dev/nvme0n1
Command (? for help): n
Partition number (1-128, default 1): 2
First sector (34-488397134, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-488397134, default = 488397134) or {+-}size{KMGTP}: +50G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8300
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/nvme0n1.
The operation has completed successfully.

Creates a new 50GB partition on an NVMe SSD.


parted — Command-line Partition Editor

Flags: mklabel (create partition table), mkpart (create partition), print, resizepart, rm

bash
$ parted /dev/sdb print
Model: VBOX HARDDISK (scsi)
Disk /dev/sdb: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  107GB   107GB   ext4         primary
 2      107GB   109GB   2147MB               primary  swap

Shows partition layout with sizes in human-readable format.

bash
$ parted /dev/sdb mklabel gpt
(No output — creates a new GPT partition table)

Creates a new GPT label (erases all existing partitions).

bash
$ parted /dev/sdb mkpart primary ext4 1MiB 100GiB
(No output — creates a 100GB ext4 partition)

Creates a new partition of type primary with ext4 filesystem.

bash
$ parted /dev/sdb resizepart 1 200GiB
(Resizes partition 1 to 200GB — requires that the partition is not mounted)

Resizes an existing partition (must be unmounted).

bash
$ parted /dev/sdb rm 2
(Removes partition 2)

Filesystem Creation

mkfs — Build a Filesystem

Common Filesystem Creation:

bash
$ mkfs.ext4 /dev/sda1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: 12345678-1234-1234-1234-123456789012
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Formats /dev/sda1 as ext4 filesystem.

bash
$ mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=6553600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0
data     =                       bsize=4096   blocks=26214400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=12800, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Formats /dev/sdb1 as XFS filesystem.

bash
$ mkfs.vfat /dev/sdc1
mkfs.fat 4.2 (2021-01-31)

Formats /dev/sdc1 as FAT32 (vfat) — good for USB drives.

bash
$ mkfs -t ext4 -L "DATA" /dev/sda3

-t specifies filesystem type, -L sets a label.


mkswap — Set Up a Swap Space

bash
$ mkswap /dev/sda2
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=abcd1234-abcd-1234-abcd-123456789012

Prepares a partition to be used as swap space.

bash
$ mkswap -L SWAP /dev/sda2

-L sets a label for the swap partition.


Mounting & Unmounting

mount — Mount a Filesystem

Flags: -t (type), -o (options), -a (mount all in /etc/fstab), -r (read-only)

bash
$ mount /dev/sdb1 /mnt/backup
(No output — mounts /dev/sdb1 to /mnt/backup)

Mounts a filesystem to a directory.

bash
$ mount -t ext4 /dev/sda1 /mnt/data

-t specifies the filesystem type (auto-detected if omitted).

bash
$ mount -o ro /dev/sdb1 /mnt/readonly
(Mounts as read-only — prevents accidental writes)

-o ro mounts read-only. Useful for recovery or security.

Common Mount Options:

OptionMeaning
rwRead-write (default)
roRead-only
noexecPrevent execution of binaries
nosuidIgnore setuid bits
nodevIgnore device files
remountRemount with new options
bash
$ mount -o remount,rw /mnt/data
(Remounts /mnt/data as read-write without unmounting)

remount changes mount options for an already mounted filesystem.

bash
$ mount -a
(No output — mounts all filesystems defined in /etc/fstab)

-a mounts all filesystems listed in /etc/fstab.

bash
$ mount -o rw,noexec /dev/sdb1 /mnt/data
(Mounts with no execution allowed — good for /tmp or data directories)

umount — Unmount a Filesystem

Flags: -l (lazy), -f (force), -a (unmount all)

bash
$ umount /mnt/backup
(No output — unmounts /mnt/backup)

Unmounts a mounted filesystem.

bash
$ umount /dev/sdb1
(Can unmount by device name as well)
bash
$ umount -l /mnt/backup
(No output — lazy unmount: detaches now, cleans up later)

-l lazy unmount — useful when a filesystem is busy.

bash
$ umount -f /mnt/backup
(Force unmount — use when normal unmount fails)

mountpoint — Check if a Directory is a Mount Point

bash
$ mountpoint /mnt/data
/mnt/data is a mountpoint
bash
$ mountpoint /home
/home is not a mountpoint

/etc/fstab — Filesystem Table

Format: <device> <mountpoint> <type> <options> <dump> <pass>

bash
$ cat /etc/fstab
# <device>        <mountpoint>  <type>  <options>                  <dump> <pass>
UUID=123456...    /             ext4    defaults,noatime           0      1
UUID=abcd12...    /home         ext4    defaults,noatime           0      2
UUID=efgh34...    /boot         ext4    defaults,noatime           0      1
UUID=ijkl56...    swap          swap    defaults                   0      0
/dev/sdb1         /backup       xfs     defaults,noatime           0      2
tmpfs             /tmp          tmpfs   defaults,noexec,nosuid     0      0

/etc/fstab defines how filesystems are mounted at boot.

Understanding fstab Fields:

FieldMeaning
DeviceUUID, LABEL, or device path
MountpointDirectory where it's mounted
TypeFilesystem type (ext4, xfs, swap, etc.)
OptionsMount options (defaults, noatime, etc.)
Dump0 = no backup, 1 = backup (for dump command)
Pass0 = skip check, 1 = root check, 2 = other check

Filesystem Repair

fsck — Filesystem Consistency Check

Flags: -a (auto repair), -y (yes to all), -n (no to all), -f (force even if clean), -t (type)

** IMPORTANT:** The filesystem MUST BE UNMOUNTED before running fsck.

bash
$ umount /dev/sda1
$ fsck /dev/sda1
fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
/dev/sda1: clean, 123456/3276800 files, 2345678/13107200 blocks

Checks and repairs a filesystem. Must be unmounted.

bash
$ fsck -y /dev/sda1
e2fsck 1.46.5 (30-Dec-2021)
/dev/sda1 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda1: 123456/3276800 files (0.2% non-contiguous), 2345678/13107200 blocks

-y automatically answers "yes" to all repair questions.

bash
$ fsck -f /dev/sda1
(Forces check even if filesystem appears clean)
bash
$ fsck -n /dev/sda1
(Dry run — shows what would be fixed without making changes)

tune2fs — Adjust Filesystem Parameters (ext2/ext3/ext4)

Flags: -l (list settings), -c (max mount count), -i (check interval), -m (reserved blocks)

bash
$ tune2fs -l /dev/sda1 | head -15
tune2fs 1.46.5 (30-Dec-2021)
Filesystem volume name:   rootfs
Last mounted on:          /
Filesystem UUID:          12345678-1234-1234-1234-123456789012
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              3276800
Block count:              13107200
Reserved block count:     655360
Free blocks:              10754000
Free inodes:              3153344
First block:              0
Block size:               4096

Shows detailed filesystem parameters.

bash
$ tune2fs -c 30 /dev/sda1
tune2fs 1.46.5 (30-Dec-2021)
Setting maximal mount count to 30

Forces a filesystem check every 30 mounts.

bash
$ tune2fs -i 30d /dev/sda1
tune2fs 1.46.5 (30-Dec-2021)
Setting interval between checks to 30 days

Forces a filesystem check every 30 days.

bash
$ tune2fs -m 2 /dev/sda1
tune2fs 1.46.5 (30-Dec-2021)
Setting reserved blocks percentage to 2% (262144 blocks)

Reserves 2% of blocks for root. Default is 5%.


xfs_repair — Repair XFS Filesystem

bash
$ umount /dev/sdb1
$ xfs_repair /dev/sdb1
Phase 1 - find and verify superblock...
Phase 2 - using internal log
Phase 3 - for each AG...
Phase 4 - check for duplicate blocks...
Phase 5 - rebuild AG headers and trees...
Phase 6 - check inode connectivity...
Phase 7 - verify link counts...
No modifications to filesystem

Repairs XFS filesystems. Must be unmounted.

bash
$ xfs_repair -n /dev/sdb1
(Dry run — checks without making changes)

Swap Management

swapon — Enable Swap

Flags: -s (show swap status), -a (enable all in /etc/fstab), --show (modern list)

bash
$ swapon -s
Filename                Type        Size        Used    Priority
/dev/sda2               partition   2097148     234567  -2

Shows current swap usage. -s is deprecated — use --show instead.

bash
$ swapon --show
NAME      TYPE      SIZE   USED  PRIO
/dev/sda2 partition 2G     234M  -2

Modern way to show swap information.

bash
$ swapon /dev/sda2
(No output — enables swap on /dev/sda2)
bash
$ swapon -a
(Enables all swap devices listed in /etc/fstab)

swapoff — Disable Swap

bash
$ swapoff /dev/sda2
(No output — disables swap on /dev/sda2)
bash
$ swapoff -a
(Disables all swap devices)

Archiving & Compression

tar — Tape Archive (The Swiss Army Knife)

Common Flags:

FlagMeaning
cCreate archive
xExtract archive
tList contents
zgzip compression
jbzip2 compression
Jxz compression
vVerbose output
fFile (must be last)
pPreserve permissions
--excludeExclude patterns

Create Archives:

bash
$ tar -czvf archive.tar.gz /home/bob/Documents/
tar: Removing leading `/' from member names
/home/bob/Documents/
/home/bob/Documents/file1.txt
/home/bob/Documents/file2.pdf
/home/bob/Documents/subdir/

Creates a compressed tarball with gzip (-z).

bash
$ tar -cjvf archive.tar.bz2 /home/bob/Documents/
(Similar but with bzip2 compression — slower but smaller)
bash
$ tar -cJvf archive.tar.xz /home/bob/Documents/
(xz compression — highest compression, slower)

Extract Archives:

bash
$ tar -xzvf archive.tar.gz
/home/bob/Documents/
/home/bob/Documents/file1.txt
/home/bob/Documents/file2.pdf

Extracts a gzip-compressed tarball.

bash
$ tar -xjvf archive.tar.bz2

Extracts a bzip2-compressed tarball.

bash
$ tar -xJvf archive.tar.xz

List Archive Contents:

bash
$ tar -tvf archive.tar.gz
drwxr-xr-x bob/staff    0 2026-01-15 10:30 home/bob/Documents/
-rw-r--r-- bob/staff 1234 2026-01-15 10:30 home/bob/Documents/file1.txt
-rw-r--r-- bob/staff 4567 2026-01-15 10:30 home/bob/Documents/file2.pdf

Extract to Different Directory:

bash
$ tar -xzvf archive.tar.gz -C /tmp/
(Extracts to /tmp/)

Exclude Files:

bash
$ tar -czvf backup.tar.gz /home/bob --exclude="*.tmp" --exclude=".cache"

gzip — Compress Files

Flags: -d (decompress), -k (keep original), -v (verbose), -1 to -9 (compression level)

bash
$ gzip -v file.txt
file.txt:  67.2% -- replaced with file.txt.gz

Compresses a file (replaces original with .gz).

bash
$ gzip -k file.txt
(Compresses but keeps the original file)
bash
$ gzip -d file.txt.gz
(Decompresses .gz file)

Compression Levels:

bash
$ gzip -1 file.txt   # Fastest, least compression
$ gzip -6 file.txt   # Default
$ gzip -9 file.txt   # Slowest, best compression

gunzip — Decompress Files

bash
$ gunzip -v file.txt.gz
file.txt.gz:  67.2% -- replaced with file.txt

Decompresses .gz files.

bash
$ gunzip -k file.txt.gz
(Decompresses but keeps the .gz file)

zip — Create ZIP Archives (Windows Compatible)

Flags: -r (recursive), -d (delete from zip), -l (show contents), -u (update), -P (password)

bash
$ zip -r archive.zip /home/bob/Documents/
  adding: home/bob/Documents/ (stored 0%)
  adding: home/bob/Documents/file1.txt (deflated 67%)
  adding: home/bob/Documents/file2.pdf (deflated 45%)

Creates a ZIP archive (compatible with Windows).

bash
$ zip -r -P secret archive.zip /home/bob/secret/
(Adds password protection — use with caution!)
bash
$ zip -l archive.zip
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2026-01-15 10:30   home/bob/Documents/
     1234  2026-01-15 10:30   home/bob/Documents/file1.txt
     4567  2026-01-15 10:30   home/bob/Documents/file2.pdf
---------                     -------
     5801                     3 files

-l lists contents without extracting.


unzip — Extract ZIP Archives

Flags: -l (list contents), -o (overwrite), -d (extract to directory), -n (never overwrite)

bash
$ unzip -l archive.zip
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2026-01-15 10:30   home/bob/Documents/
     1234  2026-01-15 10:30   home/bob/Documents/file1.txt
     4567  2026-01-15 10:30   home/bob/Documents/file2.pdf
---------                     -------
     5801                     3 files

Lists contents without extracting.

bash
$ unzip archive.zip
(Extracts all files to current directory)
bash
$ unzip -d /tmp/ archive.zip
(Extracts to /tmp/)
bash
$ unzip -o archive.zip
(Overwrites existing files without prompting)

Quick Reference — Most Used Commands

CommandDescription
df -hShow disk space usage (human-readable)
df -iShow inode usage
du -sh /pathShow total size of a directory
du -h --max-depth=1Show sizes of immediate subdirectories
lsblk -fList block devices with filesystem info
blkidShow UUIDs of all block devices
fdisk -l /dev/sdaList partitions on /dev/sda
parted /dev/sda printShow partition layout (human-readable)
mkfs.ext4 /dev/sda1Format partition as ext4
mount /dev/sda1 /mntMount a filesystem
umount /mntUnmount a filesystem
fsck /dev/sda1Check and repair filesystem (must be unmounted)
swapon --showShow swap usage
tar -czvf archive.tar.gz /pathCreate compressed tarball
tar -xzvf archive.tar.gzExtract tarball
gzip fileCompress file (replaces with .gz)
gunzip file.gzDecompress .gz file
zip -r archive.zip /pathCreate ZIP archive
unzip archive.zipExtract ZIP archive

The Nuclear Commands — DO NOT TYPE

These commands will permanently destroy your data or system.

CommandWhat it does
dd if=/dev/zero of=/dev/sdaWipes the entire disk (no recovery)
dd if=/dev/urandom of=/dev/sdaOverwrites disk with random data (secure erase)
mkfs.ext4 /dev/sdaFormats the whole disk (all partitions destroyed)
rm -rf /Deletes everything on the system
fsck -y /dev/sdaRunning fsck on a mounted filesystem (causes corruption)
echo 1 > /proc/sys/kernel/panicForces kernel panic
shred -f /dev/sdaOverwrites and deletes data irreversibly

Keyboard Shortcuts

ShortcutAction
TabAuto-complete file names and commands
Ctrl+CCancel/kill the current running command
Ctrl+DLog out of the current terminal or exit a shell
Ctrl+ZSuspend the current command (put it in the background)
!!Re-run the last command (e.g., sudo !! re-runs with sudo)
Ctrl+RReverse search through command history

Additional Tips

  1. The Man Pages: If you forget how a command works, type man command (e.g., man df). Press q to exit the manual.

  2. Always Unmount Before fsck: Running fsck on a mounted filesystem can cause corruption. Always unmount first.

  3. Use UUIDs in /etc/fstab: Device names (/dev/sda1) can change on reboot. UUIDs are persistent. Use blkid to find UUIDs.

  4. Inode Usage: df -i shows inode usage. If inodes are 100% full, you cannot create new files even with free space.

  5. The 5% Reserved Space: By default, ext4 reserves 5% of space for root. For data drives, reduce this to 1-2% with tune2fs -m 1.

  6. Mount Options for Security:

  • noexec on /tmp prevents executing malicious binaries
  • nosuid on /home prevents setuid exploits
  • nodev on /tmp prevents device file attacks
  1. Check Disk Health: Use smartctl -a /dev/sda (requires smartmontools) to check disk health and predict failures.

  2. Extend Filesystem: To extend a partition and filesystem:

  • Use parted or gdisk to resize the partition
  • Use resize2fs /dev/sda1 (ext4) or xfs_growfs /mountpoint (XFS)
  1. Find Large Files:

    bash
    find / -type f -size +1G -exec ls -lh {} \; 2>/dev/null
  2. Create a Swap File (instead of a partition):

    bash
    dd if=/dev/zero of=/swapfile bs=1G count=4
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    echo "/swapfile none swap sw 0 0" >> /etc/fstab

Next in LinuxFile Navigation & Manipulation
Linux cheat sheet: Disks & Filesystems: Complete Cheat Sheet | InfraThrone DevOps Park