Skip to main content

Command Palette

Search for a command to run...

Disk Usage

Updated
5 min read
R
Safety professional by day, Linux explorer by night.

Along the time, a directory can be occupied by many files and you need to know how much space is left either in byte or percentage. There are many ways to do so.

See in detail

See the summary of disk usage in each subdirectories

du

The result would be like this:

32	./subdir1/subdir1.2
148	./subdir1
522980	./subdir2
49024	./subdir3
12536	./subdir4
22860	./subdir5
14022624	.

📝 Info:

  • The du command can produce huge output on large directory trees.

  • Sometimes the total (number at the bottom) does not match the total size because some files might be hidden.

See the summary of disk usage in current directory

du -sh

📝 Info:

  • This command will only give you the size (i.e. 8G), without any list.

  • -s to show summary only (not showing subdirectories).

  • -h to show the disk usage in human readable units (i.e. Kb, Mb, Gb, Tb).

Filtering list of disk usage in current directory

du -sh * | grep ".[extension]"

Case study:

  • You wanna show list of all files in the Downloads directory, and

  • you only need to see image files with JPG, JPEG or PNG extension.

  • Using command du -sh without grep will give you very long list if there are many files in the directory.

Thus, the command would be:

du -sh * | grep -Ei '\.(jpe*g|png)$'

The result would be like this:

96K	IMG1.png
308K	IMG2.png
40K	IMG3.jpg
4.5M	IMG5.jpeg
52K	IMG6.JPG

📝 Info:

  • Learn more about grep grep command.

  • The extensions in the result are insensitive.

Filtering list of disk usage in current directory

 du -sh * | sort -h

The result would be like this:

92K	doc1.pdf
112K	Thumbs.db
148K	subdirectory1
436K	doc2.pdf
13M	subdirectory1
23M	subdirectory3
13G	traffic.log

📝 Info:

  • To show descending (largest file at the top), after pipe, use command sort -rh.

  • If the directory that you are working at is different than the diractory of files you wanna show, use full path like this:

du -sh [fullpath]/* | sort -h

Case study:

  • You wanna list the size of each file and directory in the directory of "subdirectory1" which is under the directory of Downloads, and

  • you are at another directory.

Thus, the command would be:

du -sh ~/Downloads/subdirectory1/* | sort -h

See in General

Partition view

fdisk -l

The cons of this command is the file size is shown in bytes without any thousand separator like this example.

Disk /dev/sda: 500GB
Disk identifier: 0x12345678

Device         Boot   Start       End   Sectors   Size Id Type
/dev/sda1      *       2048    1026047   1024000   500M 83 Linux
/dev/sda2            1026048  976562175 975536128 465.5G 83 Linux
/dev/sda3          976562176  976773119    210944   103M  82 Linux swap / Solaris

Disk /dev/sdb: 1TB
Disk identifier: 0x87654321

Device         Boot   Start       End   Sectors   Size Id Type
/dev/sdb1      *       2048  1953519615 1953517568  931.5G 83 Linux

Clean partition view

df -h --output=source,pcent

Directory tree view

lsblk -o NAME,SIZE,TYPE,MOUNTPOINT

📝 Info:

  • Without -o NAME,SIZE,TYPE,MOUNTPOINT (just lsblk command), the result will show a complete columns.

  • Many Linux users prefer lsblk over fdisk -l because the output is cleaner and already human-readable.

You can also show the usage percentage with this command:

lsblk -f

The result would be like this:

NAME   FSTYPE FSVER LABEL      UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda                                                                                
├─sda1 vfat   FAT32 ESP        8CE7-B98A                                           
├─sda2                                                                             
├─sda3 ntfs         Data1       BAB4402CB32FCA21                                    
├─sda4 ntfs         Data2       782A87502AB43A48                                    
├─sda5 ntfs         WINRETOOLS 9CDA396ADA5240A6                                    
├─sda6 vfat   FAT32            D0DC-E065                             813.7M    20% /boot/efi
└─sda7 ext4   1.0              4570e8d6-5881-4f91-9822-caa4297551f2   74.2G    58% /

Related commands

See and sort files based on file size

For example, you have 3 subdirectories and each subdirectory has files. Use this command to show how many files (not the size) inside each subdirectory.

du -xm

If you wanna sort the result in descending:

du -xm | sort -rn

📝 Info:

  • -x to show the output of the current directory, will not include result from the other directories.

  • -k to show in Kb unit (if it's smaller than 1 Kb, it'll be rounded up to 1 Kb).

  • -m to show in Mb unit (if it's smaller than 1 Mb, it'll be rounded up to 1 Mb).

  • -g to show in Gb unit (if it's smaller than 1 Gb, it'll be rounded up to 1 Gb).

  • -t does not exist.

  • -r to sort the result descending.

  • -n to sort the result based on file size.

sort command wihtout option will treat file size as string. As a result, a directory with name "111 subdirectory" will be placed above "1 subdirectory". Here is the example:

13   ./subdir999
1  ./subdir6
23  ./subdir77
511 ./subdir123

See and sort files based on filename

du -xm | sort -k2

📝 Info:

  • sort -k2 to sort the list based on the second field (column). First field is the filesize.