opscape

Disks and filesystems cheat sheet

The most useful disks and filesystems commands on one printable page, every line checked by running it.

dd Copy bytes between files or devices in fixed-size blocks, with optional offsets and conversions.

dd if=/dev/zero of=zeros.img bs=1M count=5 ls -l zeros.img
Create a file of a given size output
dd if=photos/beach.png bs=1 count=8 status=none | xxd
Read the first bytes of a file output
printf 'version=1.0\n' > app.ini printf '2' | dd of=app.ini bs=1 seek=8 conv=notrunc status=none cat app.ini
Overwrite bytes in place output
sudo dd if=debian.iso of=/dev/sdX bs=4M status=progress oflag=sync
Write an image to a USB stick output

df Report how much space is used and free on mounted filesystems.

df -h .
Free space where you are output
df -hT . /dev/shm
Show the filesystem type output
df -i .
Check free inodes output

du Show how much disk space files and directories use, totalled per directory.

du -sh data
Total size of a directory output
du -h --max-depth=1 src
Only the first level output
du -sh -- * | sort -h | tail -4
Find the biggest items output

findmnt Show mounted filesystems as a tree, or look up which filesystem a path is on and how it is mounted.

findmnt -T . -o TARGET,SOURCE,FSTYPE
Which filesystem is this directory on output
findmnt -t devpts,mqueue
All mounts of certain types output
if findmnt -rn /mnt/backup > /dev/null; then echo "backup disk mounted" else echo "backup disk NOT mounted, skipping" fi
Test whether something is mounted output
findmnt --verify --tab-file fstab
Check an fstab for mistakes output

lsblk List block devices (disks, partitions, LVM volumes, loop devices) as a tree, with sizes and mount points.

lsblk
List disks and partitions output
lsblk -f
Filesystems, labels and UUIDs output
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS,MODEL
Choose the columns output

mount Attach a filesystem (a disk partition, image, network share or tmpfs) to a directory, or list what is mounted.

mount --fstab fstab -a --fake -v
Dry-run an fstab with --fake output
sudo mkdir -p /mnt/data sudo mount /dev/sdb1 /mnt/data
Mount a disk partition output
sudo mount -o ro,noexec,nosuid /dev/sdb1 /mnt/data
Mount read-only and without executables output
sudo mount -o loop,ro debian.iso /mnt/iso
Mount a disk image or ISO file output

truncate Shrink or extend a file to an exact size, creating it if needed.

truncate -s 0 logs/app.log ls -l logs/app.log
Empty a file in place output
truncate -s 100M blank.img ls -lh blank.img du -h --apparent-size blank.img
Create a large file instantly output
wc -c < todo.txt truncate -s +10 todo.txt wc -c < todo.txt
Grow a file by a relative amount output