Disk and LVM commands cheat sheet

This cheat sheet brings together the commands for working with disks on Linux servers: checking free space, finding large files, partitions and file systems, growing the disk after an upgrade, permanent mounts in fstab, swap, and LVM management. You can search for a command, filter by topic, and copy it with one click.

Free space and disk usage

  • df -h

    Shows the size, used space, free space, and mount point of every mounted file system, in readable units such as G and M.

  • df -hT

    Works like df -h, and adds a column with the file system type, such as ext4 or xfs.

  • df -h /var

    Shows only the file system that the directory is on.

  • df -i

    Shows inode usage, meaning how many more files you can still create. A file system can run out of inodes even when it still has free space, for example because of a huge number of small cache files.

  • sudo du -sh /var/log

    Shows the total size of a directory.

  • sudo du -h -d 1 /var | sort -h

    Shows the size of each subdirectory one level down, sorted from smallest to largest. -d is short for --max-depth.

  • sudo du -xh -d 1 / | sort -h

    Scans from the root directory and stays on the same file system, so other disks mounted on the server aren't counted.

  • sudo ncdu -x /

    An interactive tool for reviewing disk usage, where you move between directories with the arrow keys. It isn't installed by default: install the ncdu package, which on AlmaLinux and Rocky Linux is in the EPEL repository.

  • journalctl --disk-usage

    Shows how much space the systemd logs take up.

  • sudo journalctl --vacuum-size=500M

    Caution: deletes old archived log files until their total size drops below 500MB. Deleted logs can't be recovered.

Large files and deleted files

  • sudo find / -xdev -type f -size +500M -exec ls -lh {} +

    Finds files larger than 500MB on the root file system, and shows the size of each one.

  • sudo find /var/log -type f -size +100M

    Finds log files larger than 100MB.

  • sudo lsof +L1

    Shows files that were deleted but are still open in a process. Their space is freed only when the process closes them, which is why df shows a full disk even when du can't find the files. Restarting the service that holds the file is usually enough.

  • sudo truncate -s 0 /var/log/example.log

    Caution: empties a log file without deleting it, so the space is freed right away and the process writing to it keeps working. The contents of the file are erased.

Disks, partitions, and file systems

  • lsblk

    Shows disks and partitions as a tree: name, size, type, and mount point. This command shows whether the disk has already grown after an upgrade.

  • lsblk -f

    Adds the file system type, label, UUID, and free space of each partition.

  • sudo blkid

    Shows the UUID, label, and file system type of every partition.

  • findmnt

    Shows all mounted file systems as a tree, with the source, type, and mount options.

  • findmnt -T /var/www

    Shows which file system and disk a directory is on.

  • sudo fdisk -l

    Shows the partition table of every disk: size, table type (GPT or DOS), and partitions. Caution: fdisk without -l opens a partition editor, and saving in it changes the disk.

  • sudo parted /dev/sda print

    Shows the partitions of one disk, with the file system and boundaries of each partition. Caution: other parted commands change the disk immediately, with no save step.

  • sudo gdisk -l /dev/sda

    Shows the partition table of a GPT disk. gdisk usually isn't installed by default.

  • xfs_info /

    Shows the details of a mounted XFS file system, such as the block size and the number of blocks.

Growing a partition and file system

  • sudo dnf install cloud-utils-growpart

    AlmaLinux, Rocky Linux, and RHEL: installs growpart.

  • sudo apt install cloud-guest-utils

    Debian and Ubuntu: installs growpart.

  • sudo growpart -N /dev/sda 1

    Simulates growing partition 1 on the /dev/sda disk and shows what would change, without changing anything.

  • sudo growpart /dev/sda 1

    Grows partition 1 to use all the free space after it. You give the disk and the partition number separately, with a space between them, also for a disk named like /dev/nvme0n1. The command grows only the partition, not the file system inside it.

  • sudo resize2fs /dev/sda1

    ext4: grows the file system to the size of the partition, even while it's mounted and in use. Shrinking ext4 is possible only when the file system is unmounted.

  • sudo xfs_growfs /

    XFS: grows the file system mounted at the given mount point to the size of the partition. XFS grows only while it's mounted, and in practice it can't be shrunk.

  • sudo pvresize /dev/sda3

    LVM: updates the PV to the size of the partition after growpart, and the new space is added to the free space in the VG.

  • sudo lvextend -r -l +100%FREE vg0/root

    LVM: grows the LV to use all the free space in the VG, and the -r option immediately grows the file system in it too. You can find the VG and LV names with vgs and lvs.

  • sudo lvextend -r -L +10G vg0/data

    LVM: grows an LV by 10GB, together with its file system.

New disk: creating a file system and mounting it

  • sudo mkfs.ext4 /dev/sdb1

    Caution: creates an ext4 file system on the partition, and all the data on it is erased permanently. Before you run it, check with lsblk -f that it's the right partition and that it's empty.

  • sudo mkfs.xfs /dev/sdb1

    Caution: creates an XFS file system on the partition, and all the data on it is erased. When the partition already has a file system, the command stops and requires the -f option.

  • sudo mkdir -p /mnt/data

    Creates the directory that serves as the mount point.

  • sudo mount /dev/sdb1 /mnt/data

    Mounts the partition on the directory until the server's next reboot. For a permanent mount, add a line to the /etc/fstab file.

  • sudo umount /mnt/data

    Unmounts the file system. The message target is busy means that a process is still using it.

  • sudo lsof /mnt/data

    Shows which processes hold open files on the file system mounted on the directory, for example when unmounting fails.

Permanent mounts in /etc/fstab

  • sudo cp /etc/fstab /etc/fstab.bak

    Saves a copy of the file before you edit it. A wrong line in fstab can delay or stop the server from booting.

  • sudo blkid /dev/sdb1

    Shows the UUID of the partition for the fstab line. Unlike names such as /dev/sdb1, the UUID doesn't change when the order of the disks changes.

  • UUID=<UUID> /mnt/data ext4 defaults,nofail 0 2

    A sample line in the /etc/fstab file: source, mount point, type, options, dump backup, and fsck check order. Replace <UUID> with the value that blkid shows, and for XFS, write xfs instead of ext4. The nofail option prevents a boot error when the disk is missing.

  • sudo findmnt --verify

    Checks that the /etc/fstab file is valid and usable. Run it after every edit.

  • sudo systemctl daemon-reload

    On servers with systemd: reloads the configuration after you edit fstab, so the change takes effect.

  • sudo mount -a

    Mounts all the file systems in fstab that aren't mounted yet. An error here points to a problem in the file, and it's better to find it before you reboot the server.

Swap

  • swapon --show

    Shows the active swap areas: file or partition, size, and usage. Empty output means the server has no active swap.

  • free -h

    Shows memory and swap: total, used, and free.

  • sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

    Creates a 2GB file to use as swap. This is the general method that works on most file systems. Don't create the file with cp or truncate, because swapon rejects a file with holes, and Btrfs requires extra settings.

  • sudo chmod 600 /swapfile

    Limits access to the file to root only. swapon warns when a swap file has more open permissions.

  • sudo mkswap /swapfile

    Initializes the file as a swap area.

  • sudo swapon /swapfile

    Turns on swap immediately, until the server's next reboot.

  • /swapfile none swap defaults 0 0

    A line in the /etc/fstab file that turns on the swap file every time the server boots.

  • sudo swapoff /swapfile

    Caution: turns off swap, and its contents move back into memory. When there isn't enough free memory, the command can fail or cause processes to be killed, so check with free -h first.

LVM

  • sudo pvs

    Shows the PVs, meaning the disks and partitions assigned to LVM, with their size and free space.

  • sudo vgs

    Shows the volume groups (VGs), and in the VFree column, the free space in each one.

  • sudo lvs

    Shows the logical volumes (LVs), their size, and the VG each one belongs to.

  • sudo lvdisplay vg0/root

    Shows full details about an LV, including its path, for example /dev/vg0/root.

  • sudo pvcreate /dev/sdb

    Caution: prepares a disk for use with LVM. Run it only on a new disk or on a disk whose data you no longer need.

  • sudo vgextend vg0 /dev/sdb

    Adds the disk to the VG, and its space is added to the free space.

  • sudo lvcreate -L 20G -n data vg0

    Creates a new 20GB LV named data. Then create a file system on it, for example sudo mkfs.ext4 /dev/vg0/data.

  • sudo lvcreate -l 100%FREE -n data vg0

    Creates an LV that uses all the free space in the VG.

  • sudo lvcreate -s -L 5G -n root_snap vg0/root

    Creates a snapshot of an LV, for example before a major update. The snapshot stores only the changes written after it, so its size depends on how many changes you expect, and it needs free space in the VG.

  • sudo lvremove vg0/root_snap

    Caution: permanently deletes an LV, including its data. Use it to remove a snapshot you no longer need, and double-check the name before you confirm.

  • sudo lvreduce -r -L 20G vg0/data

    Extreme caution: shrinks an LV to 20GB, and the -r option shrinks its file system first. Data in the area removed from the LV is lost. Of ext4 and XFS, only ext4 can be shrunk, and shrinking happens while the file system is unmounted, so -r may unmount it and mount it again. Do this only after a backup.

Checking and repair

  • sudo dmesg -T | grep -iE "i/o error|ext4-fs error|xfs"

    Searches the kernel log for I/O errors and file system messages.

  • sudo fsck -n /dev/sdb1

    Checks a file system and shows problems, without repairing them. On a mounted file system, the results aren't reliable, so unmount it first with umount. Caution: fsck without -n actually makes repairs, and you must never run it on a mounted file system, because that can corrupt the data.

  • sudo e2fsck -f /dev/sdb1

    ext4: checks and repairs a file system, even when it's marked as clean. Caution: run it only on an unmounted file system, preferably after a backup.

  • sudo xfs_repair -n /dev/sdb1

    XFS: checks an unmounted file system and reports problems, without repairing them. To actually repair, run xfs_repair without -n, also on an unmounted file system.

  • sudo smartctl -a /dev/sda

    Shows the health data (S.M.A.R.T.) of a physical disk. On a VPS, the disk is virtual, and this data usually isn't available inside it.

After a disk upgrade: growing the partition and file system

Upgrading the disk size of a VPS grows the virtual disk, but the partition and the file system inside it stay at their previous size until you grow them. Before you start, we recommend backing up your important data.

Checking the current state

Run lsblk -f and df -hT, and check that the disk already shows the new size, which partition holds the file system you want to grow, and what type it is: ext4, XFS, or LVM. An LVM partition appears in lsblk -f with the type LVM2_member, with the LV below it. If the disk still shows the previous size, reboot the server and check again.

Growing the partition

When the file system is on a partition, first grow the partition with growpart, giving the disk name and the partition number separately, for example sudo growpart /dev/sda 1. Running it with -N shows in advance what will change. growpart grows a partition up to the end of the disk or the start of the next partition, so in practice it suits the last partition on the disk. When the file system sits directly on the disk, with no partitions, this isn't needed.

ext4

After growing the partition, run sudo resize2fs with the partition name, for example sudo resize2fs /dev/sda1. The file system grows while it's mounted, with no interruption to the server.

XFS

Run sudo xfs_growfs with the mount point, for example sudo xfs_growfs / for the root file system. XFS grows only while it's mounted.

LVM

When the partition is an LVM PV, update it with sudo pvresize and the partition name, and then grow the LV together with its file system with sudo lvextend -r -l +100%FREE and the VG and LV names. You can add another disk attached to the server to the VG with pvcreate and vgextend, and then grow the LV the same way.

Final check

Run df -h again and make sure the new size appears.

Disk names on your server

The disk names on this page, such as /dev/sda and /dev/sdb1, are examples only. On virtual servers, the disk can also appear under names such as /dev/vda, /dev/xvda, or /dev/nvme0n1, and on a disk of the last type, partition names end with the letter p and a number, for example /dev/nvme0n1p1. vg0, root, and data are also sample names. Before any command that changes a disk, check the correct name with lsblk. General commands for working on a server are on the Linux commands page.

Did you know?

On a Linux VPS, you connect over SSH with full root access and manage the disks with every command on this page. When you need more space, you can add disk space at any time. The upgrade happens on your existing server, with no rebuild and no data transfer.

Linux VPS plans

Disk and LVM commands FAQ

How do I grow the file system after a disk upgrade on my server?

A disk upgrade grows the disk itself, and you grow the partition and the file system separately. Check the file system type with lsblk -f, grow the partition with growpart, and then run resize2fs for ext4 or xfs_growfs for XFS. When the server uses LVM, run pvresize and then lvextend -r. In every case, the file system grows while the server is running, and at the end you confirm with df -h that the new space is available.

What should I do when the disk on my server fills up?

Start with df -h to see which file system is full, then run sudo du -xh -d 1 on the directory, sorted with sort -h, and each time continue into the largest directory. The most common causes are logs, old backups kept on the server, cache files, and temporary files. When the space really is smaller than you need, grow the disk and the file system with the commands on this page.

Why do df and du show different disk usage?

df measures the whole file system, while du counts only the files it finds in directories. The gap usually comes from files that were deleted while a process still holds them open, such as a large log file. The sudo lsof +L1 command shows these files, and their space is freed after you restart the service that holds them. Another cause is a file system mounted on a directory that already contained files, which hides them from du.

Do I need swap on a VPS?

Swap is disk space the system uses when memory fills up. It doesn't replace memory, because the disk is much slower, but it gives you a safety margin during short load peaks and reduces the chance that processes are killed when memory runs out. Check with swapon --show whether the server has swap, and add a swap file with the commands on this page. When the server uses swap all the time, it's a sign that it's time to add memory.

What are the advantages of LVM on a server?

LVM adds a layer between the disks and the file systems: disks and partitions join a volume group (VG), and from it you create logical volumes (LVs) of the size you want. This lets you add another disk to an existing volume, grow a volume together with its file system in one command, and create a snapshot of a volume before a major change.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement