tips
44 tips Disk
One command, setting or habit at a time about Disk: what it does, the line to copy and a caveat or two.
Disk usage 13
-
Includes dot folders like .cache in a du overview, where the usual glob leaves them out.
du -sh -- .[!.]* * 2>/dev/null | sort -h -
Gives one total for a set of matching files, like all logs or all ISO images.
du -ch /var/log/*.log | tail -n 1 -
Measures a folder without the parts you don't care about, like ISO files or node_modules.
du -sh --exclude=node_modules --exclude='*.iso' ~/projects -
Totals folder sizes without crossing into other mounted filesystems.
sudo du -xh -d1 / | sort -h -
Lists deleted files that a running program still holds open, which is why df stays full.
sudo lsof +L1 -
Shows disk usage as a navigable list, biggest first, with delete built in.
ncdu -x / -
Lists every folder in the current directory with its size, sorted so the biggest ones are at the bottom.
du -sh * 2>/dev/null | sort -h -
Lists directory sizes with the biggest last, so you see what fills the disk.
sudo du -h -d1 -x /var | sort -h -
Trims all mounted filesystems that support it, which keeps SSD write speed up.
sudo fstrim -av -
Measures how fast a disk reads sequentially, as a quick health and cabling check.
sudo hdparm -tT /dev/sda -
Lists the processes currently reading or writing the disk, with accumulated totals.
sudo iotop -oPa -
Prints per-disk utilisation and wait times every second to show whether storage is the bottleneck.
iostat -xz 1 -
Shows how many inodes are used on each filesystem, which explains a disk that is full with free space left.
df -i /var
Identify and inspect 9
-
Reports free space fragmentation on ext2, ext3 and ext4, with a histogram of free chunk sizes.
sudo e2freefrag /dev/sdb1 -
Prints filesystem details like type, block size and free inodes for any folder.
stat -f /srv -
Shows whether a hard disk is spinning or in standby, for checking spin down settings.
sudo hdparm -C /dev/sdX -
Watches kernel messages live, so you see the device name and any errors of a disk you connect.
sudo dmesg -wT -
Reads every block of a disk and lists the ones that cannot be read, without writing.
sudo badblocks -sv /dev/sdX -
Lists partitions together with the unallocated gaps between them, in readable units.
sudo parted /dev/sdb unit GiB print free -
Prints model and serial number of a drive from the udev database so you can identify it.
udevadm info --query=property --name=/dev/sda | grep -E 'ID_MODEL=|ID_SERIAL_SHORT=' -
Shows with one column whether each drive is a spinning disk or flash storage.
lsblk -d -o NAME,SIZE,ROTA,MODEL -
Prints the stable UUID and filesystem type of a partition so fstab does not depend on changing device names.
sudo blkid /dev/sdb1
Disks and names 8
-
Lists SCSI-layer devices such as disks with capacity, in one line each.
lsscsi -s -
Uses a name based on model and serial number instead of sda or sdb, which can swap after a reboot.
ls -l /dev/disk/by-id/ -
Sets a readable label on an ext4 filesystem and uses that label in fstab.
e2label /dev/sdX1 backup2 -
Shows live when disks appear, mount, unmount or eject on your Mac.
diskutil activity -
Adds an extra APFS volume with a size limit, without partitioning the disk.
diskutil apfs addVolume disk5 APFS Scratch -quota 50g -
Prints your APFS containers with their volumes, so you see which ones share space.
diskutil apfs list -
Unmounts every volume on a disk and takes it offline, ready to unplug.
diskutil eject disk4 -
Formats a USB drive as ExFAT so it works on both a Mac and Windows.
diskutil eraseDisk ExFAT SHARED MBR disk4
Mounts and writes 7
-
Flushes, detaches and powers down a USB disk from the terminal, like ejecting it in a file manager.
udisksctl power-off -b /dev/sdX -
Mounts an image file as a read-only folder, so you can copy files out of it safely.
mount -o loop,ro backup.img /mnt/img -
Lets a normal user mount and unmount one disk from fstab without root.
UUID=<your-uuid> /mnt/backup ext4 noauto,user 0 0 -
Mounts the partitions inside a full disk image file, such as an SD card backup.
sudo losetup -f -P -r --show sdcard.img -
Checks fstab for typos and missing devices without mounting anything.
sudo findmnt --verify --verbose -
Switches a mounted filesystem to read-only in place, so a failing disk stops receiving writes.
sudo mount -o remount,ro /mnt/data -
Lowers the share of an ext4 disk that is reserved for root, so you get the space back.
sudo tune2fs -m 1 /dev/sdb1
Risky operations 4
-
Creates a GPT partition table and one aligned partition on a new disk in a single command.
sudo parted -s -a optimal /dev/sdX mklabel gpt mkpart data ext4 1MiB 100% -
Saves the partition layout of a disk to a text file you can restore from later.
sudo sfdisk --dump /dev/sdX > sdX.dump -
Lists and removes old filesystem and RAID signatures from a disk, with a dry-run option.
sudo wipefs /dev/sdb -
Copies an image to a disk with live progress, and the target device decides what gets overwritten.
sudo dd if=debian.iso of=/dev/sdX bs=4M status=progress conv=fsync
Maintenance 2
-
Grows an ext4 filesystem to fill its enlarged disk or partition, often while it stays mounted.
resize2fs /dev/sdX1 -
Explains the sixth fstab column, which decides whether a disk gets a filesystem check at boot.
UUID=<root-uuid> / ext4 defaults 0 1
Quick checks 1
-
Prints SMART data as JSON, so scripts can check pass or fail, temperature and hours with jq.
sudo smartctl -a --json /dev/sda | jq '.smart_status.passed'
Nothing in this filter yet.