Learn how to – Top Tools To View Disk Partitions and Disk Usage on Linux.
The creation and deletion of partitions is a regular task in Linux. This is due to the fact that storage devices such as hard drives and USB disks need to be structured in a certain way before being used. For example, before installing the system or creating any files, a file system must exist. Similarly, a filesystem can only exist if a partition has been created.
Disk partitioning can be defined as the process of dividing a disk into single or multiple logical areas. These divisions are often referred to as partitions. Partitions work as isolated sections where each section behaves as its own hard drive. Once a partition has been created, the disk will store the information about the location and size of partitions in the partition table. In other terms, a partition table describes the layout of partitions of a hard drive. Normally, there are two partition table standards. These are:
- MBR (Master Boot Record): It is also referred to as ms-dos. This partitioning scheme is normally found on older computers. It only four partitions are allowed and can have a maximum disk size of 2 TB. The MBR also contains a boot loader that is written in the initial sectors of the drive.
- GPT (GUID Partition Table): This is widely used nowadays. As opposed to MBR, GPT doesn’t contain a boot loader and can have up to 128 partitions (128-bit entries). It also supports the MBR section for backward compatibility.
Disk partitioning is vital as it helps do the following:
- To upgrade Hard Disk and incorporate a new Hard Disk into the system
- Dual Booting, where users can have Multiple Operating Systems on the same system.
- Efficient disk management where each partition can be managed separately.
- Work with different File Systems using the same system
- To ensure backup and security.
Traditionally, to partition MBR disks, the fdisk
tool is used. However, with technological advancements, fdisk
was developed to understand the GPT format. There are also several other tools that can be used to manage disk partitions on Linux. In this guide, we will learn about the top tools to view Disk Partitions and Disk Usage on Linux.
1. Fdisk
Fdisk also known as the fixed disk is the most widely used tool when viewing Disk Partitions and Disk Usage on Linux systems. It helps users get all the detailed information about partitions and can also be used to monitor, create, delete, move, and copy any partition in your Linux system. Fdisk supports GPT, MBR, Sun, SGI, and BSD partition tables.
This tool can be used in the following ways:
- View all disk partitions
To view all the partitions on your system, use the command with the -l
argument as shown:
sudo fdisk -l
Sample Output:
- View details about specific partitions
You can also view details about a given partition by providing the name of the partition. For example:
sudo fdisk -l /dev/sda
Sample Output:
- Create a partition
Fdisk provides a number of commands that can be used to manage partitions. To get all the available commands, specify the hard disk name and proceed as shown:
sudo fdisk /dev/sda
Sample Output:
Now the above command can be used to manage the partition as required. To get all partition tables, use “p” as shown.
The partitions in the hard disk will be printed as shown above.
To create new a partition, provide the disk name and type “n”. For this demonstration, I will use a secondary hard disk /dev/sdb
identified using sudo fdisk -l
sudo fdisk /dev/sda
To create a partition, type n and set the partition type as shown:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Set the partition number and sectors
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): Press Enter
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): Press Enter
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help):
To verify the creation, print the partition tables;
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK
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: dos
Disk identifier: 0x6602bb9a
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 20971519 20969472 10G 83 Linux
Command (m for help):
Now apply the changes and exit
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
- Delete a partition
To delete a partition, provide the disk name:
sudo fdisk /dev/sdb
Use the “d” to delete the partition.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help):
Apply the changes and exit using “w“:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
2. GDisk
GDisk is a DOS command-line utility, that operates in a similar way to fdisk. GDisk performs everything you can do with FDisk but has some capabilities that FDisk does not. Some of the capabilities include the on-the-fly formatting, increased performance, better partition management e.t.c
The basic operations performed by GDisk are:
- View disk partitions
The command used to view details about a given disk for example /dev/sda is:
sudo gdisk -l /dev/sda
Sample Output:
- List all the available commands
To get a list of the available commands, use “?” as shown;
$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
......
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
Command (? for help):
As seen, the commands resemble those used in FDisk.
- Create a new GPT partition table
To create a new GPT partition table, provide the disk name. For example:
sudo gdisk /dev/sdb
Use the “n” command to create a new partition.
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}: Press Enter
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: Press Enter
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): Press Enter
Changed type of partition to 'Linux filesystem'
Command (? for help):
Verify the creation:
Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Model: QEMU HARDDISK
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 6E1BBEF6-A37B-46A2-A08C-1389FADC68B8
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 20971486 10.0 GiB 8300 Linux filesystem
Command (? for help):
Apply the changes:
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
- Delete a partition
To delete a partition, use “d” as shown;
$ sudo gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): d
Using 1
Once the partition has been selected, write the changes:
Command (? for help): w
inal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
3. Parted
Parted is also a widely used command when displaying and manipulating disk partitions. This is because it understands multiple partition table formats, such as MBR and GPT. It can be used to add, resize, remove partitions, reorganizing disk usage and copy data to partitions.
The common use cases are:
- Listing Partitions
The command for this is:
sudo parted -l
Sample output:
- Create a new partition
Open the disk. For example /dev/sdb
sudo parted /dev/sdb
Sample Output:
GNU Parted 3.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
View all the available commands:
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt)
alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition
table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available
devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and
END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected
device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition
NUMBER
unit UNIT set the default unit to UNIT
version display the version number and
copyright information of GNU Parted
To create a new partition, you can go for either GPT or MSDOS partition table. For GPT, proceed as shown:
##For GPT
(parted) mklabel gpt
##For MSDOS
(parted) mklabel msdos
For this guide, I will go for the MSDOS partition table although the procedure is similar for GPT.
Warning: The existing disk label on /dev/sdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? yes
Once the partition table has been created, we will proceed and create a partition now:
(parted) mkpart
Proceed as shown:
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start?
Start? 1
End? 9090
The start and end values define the size of the partition in MBs. In this case, it is almost 10GB.
To verify the creation, use the command:
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 9090MB 9089MB primary ext4 lba
(parted)
- How to resize partitions
It is possible to shrink and extend partitions using the parted command.
To shrink a partition, use the command below. Remember data can be lost when shrinking the partition.
(parted) resizepart
Partition number? 1
End? [9090MB]? 5000
Warning: Shrinking a partition can cause data loss, are you sure you want to
continue?
Yes/No? yes
Verify the changes
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 5000MB 4999MB primary
(parted)
To extend the partition:
(parted) resizepart
Partition number? 1
End? [5000MB]? 9080
Verify:
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 9080MB 9079MB primary
(parted)
- Delete partition
To delete a partition, use the rm
command and the partition number. For example:
(parted) rm
Partition number? 1
(parted)
Verify:
(parted) print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
(parted)
4. du, ncdu
Du and ncdu are famous commands to track disk usage. ncdu originates from NCurses Disk Usage and is mainly based on du(online tool) commands.
The du tool can be started with the command:
$ du
8 ./.npm/_cacache/index-v5/22/e3
60 ./.npm/_cacache/index-v5/22
8 ./.npm/_cacache/index-v5/fa/9f
8 ./.npm/_cacache/index-v5/fa/36
8 ./.npm/_cacache/index-v5/fa/33
28 ./.npm/_cacache/index-v5/fa
8 ./.npm/_cacache/index-v5/99/7b
8 ./.npm/_cacache/index-v5/99/98
20 ./.npm/_cacache/index-v5/99
6444 ./.npm/_cacache/index-v5
39196 ./.npm/_cacache
400 ./.npm/_logs
39600 ./.npm
4 ./.gnupg/private-keys-v1.d
16 ./.gnupg
4 ./Videos
8 ./.ssh
4 ./Downloads
....
You can get the information above in a more human-readable format;
$ du -h
36K ./.npm/_cacache/index-v5/a0
8.0K ./.npm/_cacache/index-v5/ec/da
8.0K ./.npm/_cacache/index-v5/ec/08
8.0K ./.npm/_cacache/index-v5/ec/f5
28K ./.npm/_cacache/index-v5/ec
8.0K ./.npm/_cacache/index-v5/5e/e7
8.0K ./.npm/_cacache/index-v5/5e/5b
8.0K ./.npm/_cacache/index-v5/5e/fc
8.0K ./.npm/_cacache/index-v5/5e/d8
8.0K ./.npm/_cacache/index-v5/5e/9d
8.0K ./.npm/_cacache/index-v5/5e/8b
52K ./.npm/_cacache/index-v5/5e
8.0K ./.npm/_cacache/index-v5/a1/4f
8.0K ./.npm/_cacache/index-v5/a1/31
8.0K ./.npm/_cacache/index-v5/a1/75
8.0K ./.npm/_cacache/index-v5/a1/30
8.0K ./.npm/_cacache/index-v5/a1/3d
44K ./.npm/_cacache/index-v5/a1
8.0K ./.npm/_cacache/index-v5/ff/d3
8.0K ./.npm/_cacache/index-v5/ff/44
8.0K ./.npm/_cacache/index-v5/ff/37
8.0K ./.npm/_cacache/index-v5/ff/89
36K ./.npm/_cacache/index-v5/ff
....
The values on the left indicate the disk space utilized by the files.
To list the sizes of all files and directories, use the command -a
or --all
For example:
$ du -ah
44K ./frontend/src/types/actions
556K ./frontend/src/types
4.0K ./frontend/src/index.html.ejs
4.0K ./frontend/src/pages/UnAuthorized/index.tsx
8.0K ./frontend/src/pages/UnAuthorized
4.0K ./frontend/src/pages/MySettings/index.tsx
8.0K ./frontend/src/pages/MySettings
4.0K ./frontend/src/pages/MetricApplication/index.tsx
8.0K ./frontend/src/pages/MetricApplication
4.0K ./frontend/src/pages/Metrics/index.tsx
8.0K ./frontend/src/pages/Metrics
4.0K ./frontend/src/pages/TraceDetail/index.tsx
4.0K ./frontend/src/pages/TraceDetail/constants.ts
12K ./frontend/src/pages/TraceDetail
4.0K ./frontend/src/pages/GettingStarted/Section.tsx
....
Once installed, ncdu can be launched from the terminal with the command:
ncdu
Sample Output:
Here, you can use the arrow up and down keys to navigate through the different directories. If you press Enter, you will move into the selected directory.
To exit, press”q“.
It takes quite some time when scanning large directories. For example, starting the tool at the root it will have many files to analyze.
Using the -o option, you avoid preparing a report and instead save all the collected data. For example:
$ sudo ncdu -o /tmp/dureport
/home/ubuntu/signoz/deploy/do...pSort.proj/msgOperation.dict.mrk2 28753 files
You can now view the file:
$ head -11 /tmp/dureport
[1,1,{"progname":"ncdu","progver":"1.14.1","timestamp":1667043990},
[{"name":"/home/ubuntu","asize":4096,"dsize":4096,"dev":2053,"ino":1446098},
[{"name":"Public","asize":4096,"dsize":4096,"ino":1705159}],
[{"name":"Pictures","asize":4096,"dsize":4096,"ino":1705162}],
[{"name":"Music","asize":4096,"dsize":4096,"ino":1705161}],
{"name":".sudo_as_admin_successful","ino":1443137},
{"name":".bashrc","asize":3771,"dsize":4096,"ino":1450156},
{"name":".profile","asize":807,"dsize":4096,"ino":1450157},
[{"name":"sample-nodejs-app","asize":4096,"dsize":4096,"ino":1453579},
{"name":"index.js","asize":156,"dsize":4096,"ino":1454761},
{"name":"package-lock.json","asize":219289,"dsize":221184,"ino":1454762},
To get help when using ncdu, use the command;
$ ncdu --help
ncdu <options> <directory>
-h,--help This help message
-q Quiet mode, refresh interval 2 seconds
-v,-V,--version Print version
-x Same filesystem
-e Enable extended information
-r Read only
-o FILE Export scanned directory to FILE
-f FILE Import scanned directory from FILE
-0,-1,-2 UI to use when scanning (0=none,2=full ncurses)
--si Use base 10 (SI) prefixes instead of base 2
--exclude PATTERN Exclude files that match PATTERN
-X, --exclude-from FILE Exclude files that match any pattern in FILE
-L, --follow-symlinks Follow symbolic links (excluding directories)
--exclude-caches Exclude directories containing CACHEDIR.TAG
--confirm-quit Confirm quitting ncdu
--color SCHEME Set color scheme
5. df
df is a command-line utility tool that allows Linux user to monitor their disk usage. For example, to view the disk usage summary, use the command:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1948344 0 1948344 0% /dev
tmpfs 396924 1364 395560 1% /run
/dev/sda5 40454400 15248488 23118744 40% /
tmpfs 1984620 0 1984620 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 1984620 0 1984620 0% /sys/fs/cgroup
/dev/loop0 128 128 0 100% /snap/bare/5
/dev/loop2 64768 64768 0 100% /snap/core20/1623
/dev/loop1 63488 63488 0 100% /snap/core20/1518
/dev/loop3 410496 410496 0 100% /snap/gnome-3-38-2004/112
/dev/loop5 354688 354688 0 100% /snap/gnome-3-38-2004/119
/dev/loop7 48128 48128 0 100% /snap/snapd/16292
/dev/loop6 66816 66816 0 100% /snap/gtk-common-themes/1519
/dev/loop8 93952 93952 0 100% /snap/gtk-common-themes/1535
/dev/loop4 49152 49152 0 100% /snap/snapd/17336
/dev/loop9 55552 55552 0 100% /snap/snap-store/558
/dev/loop10 47104 47104 0 100% /snap/snap-store/599
/dev/sda1 523248 4 523244 1% /boot/efi
tmpfs 396924 36 396888 1% /run/user/125
tmpfs 396924 8 396916 1% /run/user/1000
To get the disk usage in a human-readable format, execute:
$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 388M 1.4M 387M 1% /run
/dev/sda5 ext4 39G 15G 23G 40% /
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/loop0 squashfs 128K 128K 0 100% /snap/bare/5
/dev/loop2 squashfs 64M 64M 0 100% /snap/core20/1623
/dev/loop1 squashfs 62M 62M 0 100% /snap/core20/1518
/dev/loop3 squashfs 401M 401M 0 100% /snap/gnome-3-38-2004/112
/dev/loop5 squashfs 347M 347M 0 100% /snap/gnome-3-38-2004/119
/dev/loop7 squashfs 47M 47M 0 100% /snap/snapd/16292
/dev/loop6 squashfs 66M 66M 0 100% /snap/gtk-common-themes/1519
/dev/loop8 squashfs 92M 92M 0 100% /snap/gtk-common-themes/1535
/dev/loop4 squashfs 48M 48M 0 100% /snap/snapd/17336
/dev/loop9 squashfs 55M 55M 0 100% /snap/snap-store/558
/dev/loop10 squashfs 46M 46M 0 100% /snap/snap-store/599
/dev/sda1 vfat 511M 4.0K 511M 1% /boot/efi
tmpfs tmpfs 388M 36K 388M 1% /run/user/125
tmpfs tmpfs 388M 8.0K 388M 1% /run/user/1000
6. pydf
pydf is a python command line utility that acts as an alternative to the df command. It works in a similar way to df only that it highlights values with different colors
It can be installed with PIP as shown;
sudo pip install pydf
On Debian/Ubuntu, you can install it with the command:
sudo apt install pydf -y
To use it, issue the command:
pydf -a
Sample Output:
Get a human-readable output with the -h flag
Other available options can be obtained from the command
pydf --help
7. duf
The duf tool also gives you a simple way to check disk usage and free space on your system. It offers more advanced use than du and df in that you can sort, filter, and otherwise alter the display.
This tool can be downloaded from Github and installed as shown.
##On Debian/Ubuntu
VER=$(curl -s https://api.github.com/repos/muesli/duf/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/muesli/duf/releases/download/v${VER}/duf_${VER}_linux_amd64.deb
sudo dpkg -i duf_*_linux_amd64.deb
##On Rhel/Rocky/Alma Linux
VER=$(curl -s https://api.github.com/repos/muesli/duf/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//')
wget https://github.com/muesli/duf/releases/download/v${VER}/duf_${VER}_linux_amd64.rpm
sudo rpm -i duf_*_linux_amd64.rpm
To use it, execute the command:
duf
Sample Output:
Get more detailed information:
duf --all
Sample Output:
You can also limit the search by providing a path:
It is possible to sort the displayed content with the –sort option. The available flags are avail, filesystem, inodes, inodes_avail, inodes_usage, inodes_used, mountpoint, size, type, usage, or used.
For example:
8. dfc
dfc is a nifty tool that provides a more human-friendly overview of file systems, and space usage using graphs and a dash of color. This tool also acts as a drop-in replacement for the df command.
This tool can be installed from source or repositories. For example on Debian/Ubuntu:
sudo apt-get install dfc
To launch the tool, use the command:
dfc
Sample Output:
Get a detailed overview of all filesystems:
dfc -a
Sample Output:
Filter with the name of the filesystem:
$ dfc -t ext
$ dfc -t ext
FILESYSTEM (=) USED FREE (-) %USED AVAILABLE TOTAL MOUNTED ON
/dev/sda5 [=========-----------] 44.3% 21.5G 38.6G /
To get more options, use:
$ dfc -h
Usage: dfc [OPTION(S)] [-c WHEN] [-e FORMAT] [-p FSNAME] [-q SORTBY] [-t FSTYPE] [-u UNIT]
Available options:
-a print all mounted filesystem
-b do not show the graph bar
-c choose color mode. Read the manpage for details
-d show used size
-e export to specified format. Read the manpage for details
-f disable auto-adjust mode (force display)
-h print this message
-i info about inodes
-l only show information about locally mounted file systems
-m use metric (SI unit)
-M do not print "mounted on"
-n do not print header
-o show mount flags
-p filter by file system name. Read the manpage for details
-q sort the output. Read the manpage for details
-s sum the total usage
-t filter by file system type. Read the manpage for details
-T show filesystem type
-u choose the unit in which to show the values. Read the manpage for details
-v print program version
-w use a wider bar
-W wide filename (un truncate)
Verdict
That marks the end of this guide on the top tools to view Disk Partitions and Disk Usage on Linux. Remember that there are several other tools available. If you have interacted with any other niftier tool, feel free to share it in the comments below.
See more:
- How To Resize XFS / Btrfs file systems on Linux
- Resolve no tools available to resize disk with ‘gpt’
- Encrypt Ubuntu / Debian Disk Partition using Cryptsetup
- Resize an ext2/3/4 and XFS root partition without LVM
Leave a Reply
You must be logged in to post a comment.