9 Essential Bash Commands for Linux Storage: Easy Guide
Storage problems show up at the worst moment: a full disk, a partition that will not unmount, a file system you formatted by mistake. These nine essential Bash commands cover the daily storage work on Linux: df, du, lsblk, fdisk, mkfs, mount, umount, lsof and losetup. Strictly speaking these Bash commands are Linux programs you run from Bash, not Bash built-ins, and the original post only listed them. This rewrite runs them for real in a debian:13 container (util-linux 2.41.5) so every output below is genuine.
Lab note: the container is rootless Podman, so it has no free loop devices. I ran fdisk and mkfs on image files and mount on a tmpfs, and I could not run losetup successfully; that section says so. Details of each option come from the manual pages in man7.org.
Contents
- 1. df: the first of the Bash commands to run on a full disk
- 2. du: find which directory uses the space
- 3. lsblk: see disks and partitions before using other Bash commands
- 4. fdisk: create a partition table
- 5. mkfs: put a file system on it
- 6. mount: attach it to a directory
- 7. umount and 8. lsof: fix "target is busy"
- 9. losetup: treat a file as a disk
- Quick reference for these Bash commands
- Frequently asked questions about storage Bash commands
- Which Bash commands are safe to run on a production server?
- What happens if I use fdisk wrongly?
- Why does umount say the target is busy?
- Why does df -h show free space but writes fail?
1. df: the first of the Bash commands to run on a full disk
df reports space per mounted file system. Use -h for human-readable sizes and -T to add the file system type:
$ df -h /
Filesystem Size Used Avail Use% Mounted on
overlay 225G 166G 56G 75% /
$ df -T /
Filesystem Type 1K-blocks Used Available Use% Mounted on
overlay overlay 235250688 173158500 58444604 75% /
The -i option shows inodes instead of blocks. On this overlay file system it prints zeros and a dash, because overlayfs does not report inode counts. On ext4 or XFS you get real numbers, and a full inode table is a classic reason for "No space left on device" while df -h shows free space:
$ df -i /
Filesystem Inodes IUsed IFree IUse% Mounted on
overlay 0 0 0 - /
2. du: find which directory uses the space
Where df tells you a disk is full, du tells you which directory did it. I created data/logs with a 3 MB file and data/cache with a 1 MB file:
$ du -sh data
4.1M data
$ du -h --max-depth=1 data
3.0M data/logs
1.0M data/cache
4.1M data
Without -h, du -a lists every file with sizes in 1 KiB units, which is easy to feed into sort -n:
$ du -a data
3072 data/logs/app.log
3072 data/logs
1024 data/cache/blob.bin
1024 data/cache
4 data/readme.txt
4100 data
3. lsblk: see disks and partitions before using other Bash commands
The lsblk command prints block devices as a tree and is one of the safest Bash commands to start with. The useful form is lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT, and lsblk -f adds labels and UUIDs. It only works on real block devices: pointing it at an image file fails, which is a handy reminder to use losetup first.
$ lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT -n disk.img
lsblk: disk.img: not a block device
I do not show the container's device list, since it only mirrors the host, so run it on your own machine to see your disks.
4. fdisk: create a partition table
Of all these Bash commands, fdisk is the one that can destroy data. It edits partition tables interactively, and fdisk -l lists them. It works on a plain file, so you can practise safely. I created a 64 MiB image, a GPT table and two partitions (32 MiB and the rest), then listed it:
$ fdisk -l disk.img
Disk disk.img: 64 MiB, 67108864 bytes, 131072 sectors
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: 06D94BBC-8FF8-43EF-9D61-0D8444D47078
Device Start End Sectors Size Type
disk.img1 2048 67583 65536 32M Linux filesystem
disk.img2 67584 129023 61440 30M Linux filesystem
Nothing is written until you press w, which makes fdisk one of the Bash commands you can rehearse on an image file. On a real disk, double-check the device name before you do, and back up first.
5. mkfs: put a file system on it
mkfs.ext4 formats a partition or, as here, a whole image file. blkid and dumpe2fs -h confirm what was written:
$ mkfs.ext4 -q -F -L backup disk.img; blkid disk.img
disk.img: LABEL="backup" UUID="c0625cea-5821-48fb-aee2-32bc049d7955" BLOCK_SIZE="1024" TYPE="ext4"
$ dumpe2fs -h disk.img | grep -E "Block count|Block size"
Block count: 65536
Block size: 1024
XFS has a minimum size. A 64 MiB image is rejected, and a 320 MiB image works:
$ mkfs.xfs -q -f -L xfsdisk disk.img
Filesystem must be larger than 300MB.
$ mkfs.xfs -q -f -L xfsdisk xfs.img; blkid xfs.img
xfs.img: LABEL="xfsdisk" UUID="e5bf8166-78b5-411d-9af2-3d2004522e62" BLOCK_SIZE="512" TYPE="xfs"
Choosing between the two file systems: ext4 is the safe general default, and XFS suits large file systems. XFS cannot be shrunk, which matters when you plan an LVM layout, so read the XFS documentation before you commit. For ext4 versus other types on an existing disk, see how to list the file system type of a partition, and for combining disks see how to create LVM in CentOS 6.
6. mount: attach it to a directory
Among the daily Bash commands for storage, mount DEVICE DIR makes a file system visible. Because the container has no block devices, I mounted a 16 MiB tmpfs, which behaves the same way for the rest of the workflow. findmnt shows the options in effect:
$ mount -t tmpfs -o size=16m tmpfs /mnt/mypartition
$ df -h /mnt/mypartition
Filesystem Size Used Avail Use% Mounted on
tmpfs 16M 0 16M 0% /mnt/mypartition
$ findmnt /mnt/mypartition
TARGET SOURCE FSTYPE OPTIONS
/mnt/mypartition tmpfs tmpfs rw,relatime,seclabel,size=16384k,uid=1000,gid=1000,inode64
For a real partition the command is mount /dev/sdb1 /mnt/mypartition. Create the directory first and add an entry to /etc/fstab if the mount must survive a reboot.
7. umount and 8. lsof: fix "target is busy"
These two Bash commands work as a pair. The most common umount failure is a process that still uses the mount. I started a sleep whose working directory is inside the mount, and umount refused. lsof +D names the culprit:
$ umount /mnt/mypartition
umount: /mnt/mypartition: target is busy.
$ lsof +D /mnt/mypartition
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sleep 3579 root cwd DIR 0,94 60 1 /mnt/mypartition
The cwd in the FD column means the process simply sits in that directory. After stopping it, the unmount worked and findmnt found nothing (exit code 1):
$ umount /mnt/mypartition && echo unmounted; findmnt /mnt/mypartition; echo "findmnt rc=$?"
unmounted
findmnt rc=1
Whichever of these Bash commands you use next, always unmount before you repartition or format a device. Avoid umount -l (lazy) as a first reaction; it hides the problem until the process exits.

9. losetup: treat a file as a disk
losetup -f --show disk.img attaches an image file to the first free loop device, so fdisk, mkfs and mount can use it like a real disk. It needs access to /dev/loop-control. In my rootless container it failed:
$ losetup -f --show disk.img
losetup: disk.img: failed to set up loop device: No such file or directory
On a normal host as root the same command prints the device name (for example /dev/loop3) and losetup -d /dev/loop3 detaches it. I did not test that path here, so confirm it with man losetup on your system.
Quick reference for these Bash commands
Keep this list next to your terminal. These Bash commands cover most storage tasks:
df -h/df -i: free space and free inodes.du -h --max-depth=1 DIR: biggest subdirectories.lsblk -f: devices with file systems and labels.fdisk -l DEVICE: show the partition table before changing anything.mkfs.ext4 -L LABEL DEVICE: format.mount,findmnt,umount: attach, inspect, detach.lsof +D DIR: who blocks the unmount.losetup -f --show FILE: loop device for an image.
Frequently asked questions about storage Bash commands
Which Bash commands are safe to run on a production server?
df, du, lsblk, findmnt and lsof only read information. fdisk, mkfs and umount change things, so check the device name twice.
What happens if I use fdisk wrongly?
Changes stay in memory until you write them with w, so you can quit with q. After writing, the old partition table is gone, so back up first.
Why does umount say the target is busy?
A process has a file open or its working directory inside the mount. Find it with lsof +D, stop it, then unmount.
Why does df -h show free space but writes fail?
Of the storage Bash commands, df -i answers this. The file system may have run out of inodes. Check with df -i.
Comments
Post a Comment