0
0
Linux CLIscripting~5 mins

fdisk and lsblk in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to see and manage the disks and partitions on your Linux computer. fdisk helps you create or change partitions, and lsblk shows you a clear list of all disks and their partitions.
When you want to check what disks and partitions are available on your system before installing software.
When you need to create a new partition on a disk to store files separately.
When you want to see the size and layout of your disks and partitions quickly.
When you want to delete or modify partitions on a disk safely.
When preparing a new hard drive for use by creating partitions.
Commands
This command lists all block devices (disks and partitions) in a tree format so you can see their names, sizes, and mount points.
Terminal
lsblk
Expected OutputExpected
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 232.9G 0 disk sda1 8:1 0 100G 0 part / sda2 8:2 0 132.9G 0 part /home sdb 8:16 0 931.5G 0 disk sdb1 8:17 0 931.5G 0 part /mnt/data
This opens fdisk to manage partitions on the disk /dev/sdb. You need sudo because it changes disk settings.
Terminal
sudo fdisk /dev/sdb
Expected OutputExpected
Welcome to fdisk (util-linux 2.36.1). Changes will remain in memory only, until you decide to write them. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0x12345678. Command (m for help):
Inside fdisk, this command prints the current partition table to see existing partitions.
Terminal
p
Expected OutputExpected
Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Disklabel type: dos Disk identifier: 0x12345678 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 1953523711 1953521664 931.5G 83 Linux Command (m for help):
This quits fdisk without saving any changes, useful if you only wanted to view partitions.
Terminal
q
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: lsblk shows your disks and partitions clearly, and fdisk lets you safely create or change partitions.

Common Mistakes
Running fdisk without sudo
fdisk needs administrative rights to change disk partitions, so it will fail without sudo.
Always run fdisk with sudo, like 'sudo fdisk /dev/sdb'.
Trying to write changes in fdisk without understanding commands
Writing wrong changes can delete or corrupt partitions and data.
Use 'p' to print partitions and 'q' to quit without saving if unsure. Learn commands before writing changes.
Confusing disk names like /dev/sda and /dev/sdb
Modifying the wrong disk can cause data loss on important drives.
Use lsblk first to identify the correct disk before running fdisk.
Summary
Use lsblk to list all disks and partitions with sizes and mount points.
Use sudo fdisk /dev/sdX to open the disk for partition management.
Inside fdisk, use 'p' to print partitions and 'q' to quit without saving.