|

Linux CLI 29 🐧 fsck, mkfs and dd commands

sudo fsck -N /dev/sda
sudo fsck /dev/sda
sudo fsck -n /dev/sda
sudo fsck -f /dev/sda
sudo mkfs -h
sudo mkfs <tab>

These three commands form the backbone of filesystem management on Linux. fsck checks and repairs filesystems, mkfs creates them, and dd copies raw data between devices and files. Together they let you build, verify, and clone storage at a low level.

Key point: These tools operate on filesystems and raw devices — not on individual files. A mistake with dd can permanently destroy data, so always double-check your device names.


a – fsck command

fsck stands for File System Consistency Check. It is used to verify the integrity of a filesystem. It checks for errors — corrupted inodes, bad sectors, or broken filesystem structures — and can attempt to fix those errors.

Before you check a filesystem, you have to unmount it with umount. Running fsck on a mounted filesystem can cause serious corruption because the kernel may still be writing to it while fsck is trying to repair it.

CommandDescription
sudo fsck -N /dev/sdaDisplays what it will do without doing anything
sudo fsck /dev/sdaPerforms the check
sudo fsck -n /dev/sdaPerforms a check but does not repair
sudo fsck -f /dev/sdaForces a check even if the filesystem looks clean

Additional options:

  • -a → repairs errors automatically
  • -v → verbose mode (more output)
  • -t → test for bad sectors

Examples:

# Unmount first — always!
$ sudo umount /dev/sdb1

# Dry run — shows what fsck would do
$ sudo fsck -N /dev/sdb1
fsck from util-linux 2.37.2
[/sbin/fsck.ext4 (1) -- /dev/sdb1] fsck.ext4 /dev/sdb1

# Read-only check — no repairs
$ sudo fsck -n /dev/sdb1
fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
Warning: skipping journal recovery because doing a read-only filesystem check.
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/sdb1: clean, 11/6553600 files, 456789/26214400 blocks

# Force a check
$ sudo fsck -f /dev/sdb1
fsck from util-linux 2.37.2
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
...
/dev/sdb1: 11/6553600 files (0.0% non-contiguous), 456789/26214400 blocks

# Automatic repair
$ sudo fsck -a /dev/sdb1

# Verbose + force
$ sudo fsck -fv /dev/sdb1

Important: Never run fsck on a mounted root filesystem while the system is running. If you need to check /, boot from a live USB or use touch /forcefsck and reboot.


b – mkfs command

mkfs stands for Make File System. It is used to create and format filesystems on a partition or drive. You can think of it as the step that turns a raw partition into something the operating system can actually store files on.

To see the supported filesystem types, type sudo mkfs and press the TAB key — the shell will list all available mkfs.* variants such as mkfs.ext4, mkfs.xfs, mkfs.vfat, and so on.

Create a filesystem on a partition:

sudo mkfs -t ext4 /dev/sda1
# or equivalently
sudo mkfs.ext4 /dev/sda1

Examples:

# List supported filesystem types (press TAB after mkfs.)
$ sudo mkfs.<TAB>
mkfs.cramfs   mkfs.ext3   mkfs.ext4   mkfs.fat    mkfs.minix
mkfs.msdos    mkfs.ntfs   mkfs.vfat   mkfs.xfs    mkfs.btrfs

# Show help
$ sudo mkfs -h
Usage: mkfs [options] [-t <type>] [fs-options] <device> [<size>]
...

# Create ext4 filesystem
$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: 12345678-abcd-ef00-1234-56789abcdef0
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, ...
Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done

# Create XFS filesystem
$ sudo mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2              isize=512    agcount=4, agsize=6553600 blks
...

# Create FAT32 filesystem (for USB drives)
$ sudo mkfs.vfat -F 32 /dev/sdc1
mkfs.fat 4.2 (2021-01-31)

# Create with a label
$ sudo mkfs.ext4 -L "MyData" /dev/sdb1

Note: mkfs creates a new filesystem on an existing partition or drive. It does not resize or delete the partition itself. For partitioning tasks, use fdisk (covered in the previous chapter). When you run mkfs, everything on that partition is erased.


c – dd command

dd stands for Data Description (or sometimes “disk dump” or “convert and copy”). It is used to copy and convert data from one place to another at the block level. Unlike cp, which works on files, dd works on raw data — it can copy entire disks, partitions, or create image files.

Syntax:

dd if=inputfile [bs=blocksize] [count=numcopies] of=outputfile
OptionMeaning
ifInput file — where to read data from
bsSets the block size (default is 512 bytes) — optional
countSpecifies how many blocks will be transferred (all is default) — optional
ofOutput file — where the data will be transferred to

Why block size matters: A larger bs (like 4M or 64M) makes dd much faster for large operations. The default 512 bytes is slow because it does many small reads and writes.

Examples:

# Copy a file
$ dd if=input.txt of=backup.txt
0+1 records in
0+1 records out
123 bytes copied, 0.0003 s, 410 kB/s

# Copy with a larger block size (faster)
$ dd if=input.txt of=backup.txt bs=4M

# Show progress (GNU dd)
$ dd if=/dev/sda of=image.img bs=4M status=progress

d – dd command examples

Here are the most common real-world uses of dd. Read each one carefully — a typo in of= can overwrite the wrong disk.

1. Copy a file:

$ dd if=input.txt of=backup.txt

2. Create an image file from a disk:

$ dd if=/dev/sda bs=4M count=10240 of=image.img

This reads the first 10240 blocks of 4 MB each (about 40 GB) from /dev/sda and writes them to image.img. Omitting count copies the entire disk.

3. Clone a disk to another disk:

$ dd if=/dev/sda of=/dev/sdc bs=64M status=progress

This makes /dev/sdc an exact byte-for-byte copy of /dev/sda. The destination disk must be at least as large as the source. This is often used to migrate a system to a new drive.

4. Backup a partition:

$ dd if=/dev/sda1 of=~/sda1partition.img

This writes the entire partition to a file in your home directory. The resulting .img can be stored, compressed, or moved to another machine.

5. Restore a partition from a backup:

$ dd if=sda1partition.img of=/dev/sda1

This writes the saved image back onto the original partition. Be absolutely certain the target is correct — dd will not warn you.

6. Create a CD-ROM ISO:

$ dd if=/dev/cdrom of=tgsservice.iso bs=2048

The bs=2048 matches the standard CD sector size, which makes the read more efficient for optical media.

7. Wipe a disk with zeros (secure erase):

$ dd if=/dev/zero of=/dev/sdc bs=4M status=progress

This overwrites every byte of the target disk with zeros. Useful before disposing of a drive or preparing it for a fresh install.

8. Wipe a disk with random data:

$ dd if=/dev/urandom of=/dev/sdc bs=4M status=progress

Slower than zeros, but harder to recover data from.

9. Test disk write speed:

$ dd if=/dev/zero of=./testfile bs=1G count=1 oflag=dsync

10. Create a bootable USB from an ISO:

$ sudo dd if=ubuntu.iso of=/dev/sdc bs=4M status=progress && sync

The sync at the end flushes all cached writes to the device before you unplug it.


Complete Example Session

# ============================================
# PART 1: CHECK A FILESYSTEM WITH FSCK
# ============================================

# Unmount first
$ sudo umount /dev/sdb1

# Dry run
$ sudo fsck -N /dev/sdb1
fsck from util-linux 2.37.2
[/sbin/fsck.ext4 (1) -- /dev/sdb1] fsck.ext4 /dev/sdb1

# Read-only check
$ sudo fsck -n /dev/sdb1
...
/dev/sdb1: clean, 11/6553600 files, 456789/26214400 blocks

# Force check with verbose output
$ sudo fsck -fv /dev/sdb1
...

# ============================================
# PART 2: CREATE A FILESYSTEM WITH MKFS
# ============================================

# Create ext4 on a new partition
$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: 12345678-abcd-ef00-1234-56789abcdef0
...

# Add a label
$ sudo mkfs.ext4 -L "Backup" /dev/sdb1

# Create FAT32 for a USB stick
$ sudo mkfs.vfat -F 32 /dev/sdc1

# Mount the new filesystem
$ sudo mkdir /mnt/backup
$ sudo mount /dev/sdb1 /mnt/backup

# ============================================
# PART 3: COPY DATA WITH DD
# ============================================

# Back up a partition
$ sudo dd if=/dev/sdb1 of=~/sdb1.img bs=4M status=progress
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 42 s, 102 MB/s

# Restore it
$ sudo dd if=~/sdb1.img of=/dev/sdb1 bs=4M status=progress

# Clone a whole disk
$ sudo dd if=/dev/sda of=/dev/sdc bs=64M status=progress

# Create a bootable USB
$ sudo dd if=ubuntu.iso of=/dev/sdc bs=4M status=progress && sync

# Wipe a USB with zeros
$ sudo dd if=/dev/zero of=/dev/sdc bs=4M status=progress

# ============================================
# PART 4: VERIFY
# ============================================

$ sudo fsck -n /dev/sdb1
/dev/sdb1: clean, 12/6553600 files, 500123/26214400 blocks

$ lsblk -f
NAME   FSTYPE LABEL   UUID                                 MOUNTPOINT
sda
├─sda1 vfat           1234-ABCD                            /boot/efi
└─sda2 ext4           5678-EFGH                            /
sdb
└─sdb1 ext4   Backup  90AB-CDEF                            /mnt/backup
sdc
└─sdc1 vfat           3456-GHIJ

Quick Reference

fsck

CommandPurpose
fsck -N /dev/sdXDry run — show what would happen
fsck /dev/sdXCheck the filesystem
fsck -n /dev/sdXCheck without repairing
fsck -f /dev/sdXForce a check
fsck -a /dev/sdXAuto-repair
fsck -v /dev/sdXVerbose output
fsck -t /dev/sdXTest for bad sectors

mkfs

CommandPurpose
mkfs -hShow help
mkfs.<TAB>List supported types
mkfs -t ext4 /dev/sdXCreate ext4
mkfs.ext4 /dev/sdXSame, shorthand
mkfs.xfs /dev/sdXCreate XFS
mkfs.vfat -F 32 /dev/sdXCreate FAT32
mkfs.ext4 -L LABEL /dev/sdXCreate with label

dd

OptionMeaning
if=Input file / device
of=Output file / device
bs=Block size (default 512)
count=Number of blocks
status=progressShow progress
oflag=dsyncSync writes

dd Examples

CommandPurpose
dd if=in.txt of=out.txtCopy a file
dd if=/dev/sda of=image.imgImage a disk
dd if=/dev/sda of=/dev/sdcClone a disk
dd if=/dev/sda1 of=part.imgBackup a partition
dd if=part.img of=/dev/sda1Restore a partition
dd if=/dev/cdrom of=cd.iso bs=2048Create ISO
dd if=/dev/zero of=/dev/sdcWipe with zeros
dd if=/dev/urandom of=/dev/sdcWipe with random
dd if=ubuntu.iso of=/dev/sdc bs=4MBootable USB

Best Practices

Do This:

# Unmount before fsck
sudo umount /dev/sdb1
sudo fsck /dev/sdb1                    # ✅

# Use a larger bs for dd speed
sudo dd if=/dev/sda of=/dev/sdc bs=64M # ✅

# Use status=progress on long dd runs
sudo dd if=/dev/sda of=x.img bs=4M status=progress  # ✅

# Sync after dd to a USB
sudo dd if=img.iso of=/dev/sdc bs=4M && sync        # ✅

# Double-check device names before dd
lsblk                                  # ✅

# Use fsck -n first to inspect
sudo fsck -n /dev/sdb1                 # ✅

# Label your filesystems
sudo mkfs.ext4 -L "Backup" /dev/sdb1   # ✅

Don’t Do This:

# Don't fsck a mounted filesystem
sudo fsck /dev/sda1                    # ❌ if mounted

# Don't type of= carelessly
sudo dd if=/dev/sda of=/dev/sda        # ❌ wipes source!

# Don't use dd to copy files as a habit
dd if=file1 of=file2                   # ❌ just use cp

# Don't run mkfs on a partition with data
sudo mkfs.ext4 /dev/sdb1               # ❌ erases everything

# Don't unplug a USB before sync finishes
sudo dd if=img.iso of=/dev/sdc         # ❌ then immediately pull

Common Pitfalls

PitfallProblemSolution
fsck on mounted FSCorruptionUnmount first
Wrong of= in ddData lossVerify with lsblk
Small bsSlow copyUse bs=4M or higher
No sync after dd to USBIncomplete writeAdd && sync
mkfs on wrong partitionData erasedDouble-check device
dd without statusNo feedbackAdd status=progress
Running fsck on / liveDangerousBoot from live USB
Confusing /dev/sda with /dev/sda1Wrong targetDisk vs partition

Real-World Examples

1. Check a USB Drive

$ sudo umount /dev/sdc1
$ sudo fsck -fv /dev/sdc1
fsck from util-linux 2.37.2
fsck.fat 4.2 (2021-01-31)
Checking we can access the last sector of the filesystem
Boot sector contents:
...
/dev/sdc1: 11 files, 456/32123 clusters

2. Format a New Partition

$ sudo mkfs.ext4 -L "Data" /dev/sdb1
$ sudo mkdir /mnt/data
$ sudo mount /dev/sdb1 /mnt/data
$ df -h /mnt/data
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       916G   24K  870G   1% /mnt/data

3. Back Up an Entire Disk

$ sudo dd if=/dev/sda of=/backup/sda.img bs=64M status=progress
500118192 bytes (500 GB) copied, 4832 s, 103 MB/s

4. Clone a Disk to a New SSD

$ sudo dd if=/dev/sda of=/dev/sdc bs=64M status=progress
$ sync

5. Restore a Partition

$ sudo umount /dev/sda1
$ sudo dd if=~/sda1partition.img of=/dev/sda1 bs=4M status=progress
$ sudo fsck -f /dev/sda1
$ sudo mount /dev/sda1 /mnt/restore

6. Create a Bootable USB

$ sudo dd if=ubuntu-22.04.iso of=/dev/sdc bs=4M status=progress && sync
4294967296 bytes (4.3 GB) copied, 320 s, 13.4 MB/s

7. Wipe a Disk Before Disposal

$ sudo dd if=/dev/urandom of=/dev/sdc bs=4M status=progress
$ sudo dd if=/dev/zero of=/dev/sdc bs=4M status=progress

8. Test Disk Write Speed

$ dd if=/dev/zero of=./speedtest bs=1G count=1 oflag=dsync
1073741824 bytes (1.1 GB) copied, 4.2 s, 256 MB/s
$ rm ./speedtest

Visual: The Storage Workflow

┌──────────────────────────────────────────────┐
│              Raw Disk /dev/sdb               │
│                                              │
│  (no partitions, no filesystem)              │
└─────────────────┬────────────────────────────┘
                  │
                  │  sudo fdisk /dev/sdb
                  │  → n → p → 1 → w
                  ▼
┌──────────────────────────────────────────────┐
│           Partition /dev/sdb1                │
│                                              │
│  (partition exists, but no filesystem)       │
└─────────────────┬────────────────────────────┘
                  │
                  │  sudo mkfs.ext4 /dev/sdb1
                  ▼
┌──────────────────────────────────────────────┐
│        Filesystem on /dev/sdb1               │
│                                              │
│  (ready to store files)                      │
└─────────────────┬────────────────────────────┘
                  │
                  │  sudo mount /dev/sdb1 /mnt
                  ▼
┌──────────────────────────────────────────────┐
│              Mounted at /mnt                 │
│                                              │
│  $ cp file.txt /mnt/     ← usable!           │
└─────────────────┬────────────────────────────┘
                  │
                  │  sudo umount /mnt
                  │  sudo fsck /dev/sdb1
                  ▼
┌──────────────────────────────────────────────┐
│              Verified Clean ✅                │
│                                              │
│  (fsck confirms integrity)                   │
└──────────────────────────────────────────────┘

Summary

CommandPurposeExample
fsck -N /dev/sdXDry runsudo fsck -N /dev/sdb1
fsck /dev/sdXCheck filesystemsudo fsck /dev/sdb1
fsck -n /dev/sdXCheck without repairsudo fsck -n /dev/sdb1
fsck -f /dev/sdXForce checksudo fsck -f /dev/sdb1
fsck -a /dev/sdXAuto-repairsudo fsck -a /dev/sdb1
mkfs -t ext4 /dev/sdXCreate ext4sudo mkfs -t ext4 /dev/sdb1
mkfs.ext4 /dev/sdXSame, shorthandsudo mkfs.ext4 /dev/sdb1
mkfs.vfat -F 32 /dev/sdXCreate FAT32sudo mkfs.vfat -F 32 /dev/sdc1
dd if=IN of=OUTCopy datadd if=a.txt of=b.txt
dd if=/dev/sda of=/dev/sdcClone disksudo dd if=/dev/sda of=/dev/sdc bs=64M
dd if=/dev/sda1 of=part.imgBackup partitiondd if=/dev/sda1 of=~/part.img
dd if=part.img of=/dev/sda1Restore partitiondd if=~/part.img of=/dev/sda1
dd if=/dev/zero of=/dev/sdXWipe disksudo dd if=/dev/zero of=/dev/sdc bs=4M

Key takeaways:

  • fsck verifies and repairs filesystem integrity — always unmount first
  • Use fsck -N for a dry run, -n for read-only, -f to force, -a to auto-repair
  • mkfs creates a filesystem — it erases everything on the target partition
  • Press TAB after mkfs. to list supported filesystem types
  • dd copies raw data between files and devices — it works at the block level
  • Syntax: dd if=input of=output [ bs=blocksize ] [ count=n ]
  • Always use a large bs (4M, 64M) for speed on large operations
  • Add status=progress for long dd operations, and sync after writing to USB
  • Double-check device names — a typo with dd or mkfs can destroy data instantly

Remember: fsck checks, mkfs creates, dd copies. Each one works on raw devices and can wipe a disk in seconds if misused. Unmount before fsck, back up before mkfs, and verify twice before dd. Use lsblk to confirm your device names, use status=progress so you know something is happening, and use sync before unplugging anything. Master these three tools, and you can build, verify, back up, and clone any storage on your system.


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!