fdisk and lsblk in Linux CLI - Time & Space Complexity
When using commands like fdisk and lsblk, it helps to understand how their execution time changes as the number of disks or partitions grows.
We want to know how the time to list or manage disks grows when there are more devices or partitions.
Analyze the time complexity of the following commands.
lsblk
fdisk -l
lsblk lists all block devices and their partitions. fdisk -l lists detailed partition info for all disks.
Both commands scan devices and partitions one by one.
- Primary operation: Reading info for each disk and its partitions.
- How many times: Once per disk and once per partition inside each disk.
As the number of disks and partitions grows, the commands take longer because they check each item.
| Input Size (number of disks + partitions) | Approx. Operations |
|---|---|
| 10 | About 10 device reads |
| 100 | About 100 device reads |
| 1000 | About 1000 device reads |
Pattern observation: The time grows roughly in direct proportion to the number of devices and partitions.
Time Complexity: O(n)
This means the time to run these commands grows linearly with the number of disks and partitions.
[X] Wrong: "These commands run instantly no matter how many disks there are."
[OK] Correct: The commands must check each disk and partition, so more devices mean more work and longer time.
Understanding how command execution time grows with input size helps you reason about system performance and scripting efficiency in real tasks.
What if we only list partitions on a single disk instead of all disks? How would the time complexity change?
Practice
fdisk command in Linux?Solution
Step 1: Understand the role of fdisk
Thefdiskcommand is used to create, delete, and modify disk partitions on Linux systems.Step 2: Compare with other commands
Other options like listing directories, processes, or network connections are unrelated to disk partitioning.Final Answer:
To create and modify disk partitions -> Option BQuick Check:
fdisk = disk partition tool [OK]
- Confusing fdisk with lsblk
- Thinking fdisk lists files
- Assuming fdisk monitors network
lsblk?Solution
Step 1: Identify the option to list all devices
The--alloption withlsblklists all devices, including empty ones.Step 2: Check other options
-dlists only whole disks (no partitions),-llists in list format,-fshows filesystem info but not all devices.Final Answer:
lsblk --all -> Option AQuick Check:
lsblk --all = show all devices [OK]
- Using -d instead of --all
- Confusing -l with --all
- Using -f to list devices
/dev/sda1?
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk sda1 8:1 0 50G 0 part / sda2 8:2 0 50G 0 part /home
Solution
Step 1: Read the lsblk output
The line for/dev/sda1shows a size of 50G under the SIZE column.Step 2: Understand partition sizes
The disksdais 100G total, split into two partitions of 50G each.Final Answer:
50G -> Option CQuick Check:
/dev/sda1 size = 50G [OK]
- Confusing disk size with partition size
- Adding partition sizes incorrectly
- Ignoring the SIZE column
fdisk /dev/sdb but get the error: cannot open /dev/sdb: Permission denied. What is the most likely cause?Solution
Step 1: Understand permission requirements
Runningfdiskon disks requires root privileges; without sudo or root, device access is denied.Step 2: Check other options
Disk being mounted or formatted does not cause 'Permission denied' error; missing lsblk is unrelated.Final Answer:
You forgot to run the command as root or with sudo -> Option DQuick Check:
fdisk needs root access [OK]
- Assuming mounted disk causes 'Permission denied'
- Thinking formatting affects fdisk detection
- Confusing fdisk errors with lsblk issues
/dev/sdc using fdisk. After creating the partition, which command should you run to immediately see the updated partition layout in a tree view?Solution
Step 1: Identify command to show partition tree
lsblkdisplays block devices and partitions in a clear tree format, perfect for checking new partitions.Step 2: Compare with other commands
fdisk -llists partitions but not in tree view;mountmounts partitions;df -hshows disk usage, not layout.Final Answer:
lsblk /dev/sdc -> Option AQuick Check:
lsblk shows partition tree [OK]
- Using fdisk -l expecting tree view
- Trying to mount before checking partitions
- Using df to see partition layout
Ready to prove it?
Pass the test and this pattern moves unknown
