How to Display File Size in KB, MB or GB in Linux Terminal

Table of Contents

Display File Size in KB, MB or GB in Linux Terminal

You probably already know that you can use ls command with long listing option -l to show file size in Linux.

ls -l /opt/sys/

But unfortunately, the long listing shows the file size in blocks and that’s not of much use to us humans.

root@vps:~# ls -l /opt/sys/
total 5655032
-rw-r--r-- 1 root root    3999808 Aug  4 13:37 AnyDesk.exe
-rw-r----- 1 root root      35622 Aug  4 13:37 auth.log
-rw-r--r-- 1 root root   96000792 Aug  4 13:37 balenaEtcher-1.10.2-x64.AppImage
-rw-r--r-- 1 root root       9477 Aug  4 13:37 cert1.zip
-rw-r--r-- 1 root root     100936 Aug  4 13:37 dmesg
-rw-r--r-- 1 root root  165841568 Aug  4 13:37 ExpanDrive_Setup_2022.7.1.exe
-rw-r--r-- 1 root root          2 Aug  4 13:48 install.sh
drwxr-xr-x 2 root root       4096 Aug  4 13:45 new_dir
-rw-r--r-- 1 root root 1697906688 Aug  4 13:37 ubuntu-16.04.7-desktop-amd64.iso
-rw-r--r-- 1 root root 3826831360 Aug  4 13:41 ubuntu-22.04.1-desktop-amd64.iso

The good thing is that you can combine the option -l with -h to show the file size in a human-readable format.

ls -lh /opt/sys

As you can see, it is better to display file size in a human-readable format. File sizes are now displayed in K (for KB), M for (MB). If the file size is in Bytes, it is not displayed with any suffix. In this example, install.sh is 2 Bytes in size.

root@vps:~# ls -lh /opt/sys/
total 5.4G
-rw-r--r-- 1 root root 3.9M Aug  4 13:37 AnyDesk.exe
-rw-r----- 1 root root  35K Aug  4 13:37 auth.log
-rw-r--r-- 1 root root  92M Aug  4 13:37 balenaEtcher-1.10.2-x64.AppImage
-rw-r--r-- 1 root root 9.3K Aug  4 13:37 cert1.zip
-rw-r--r-- 1 root root  99K Aug  4 13:37 dmesg
-rw-r--r-- 1 root root 159M Aug  4 13:37 ExpanDrive_Setup_2022.7.1.exe
-rw-r--r-- 1 root root    2 Aug  4 13:48 install.sh
drwxr-xr-x 2 root root 4.0K Aug  4 13:45 new_dir
-rw-r--r-- 1 root root 1.6G Aug  4 13:37 ubuntu-16.04.7-desktop-amd64.iso
-rw-r--r-- 1 root root 3.6G Aug  4 13:41 ubuntu-22.04.1-desktop-amd64.iso

Did you notice the size of new_dir directory? It is 4 KB. If you use ls -lh command on directories, it always shows the size of directory as 4.0 K.

-rw-r--r-- 1 root root 159M Aug  4 13:37 ExpanDrive_Setup_2022.7.1.exe
-rw-r--r-- 1 root root    2 Aug  4 13:48 install.sh
drwxr-xr-x 2 root root 4.0K Aug  4 13:45 new_dir
-rw-r--r-- 1 root root 1.6G Aug  4 13:37 ubuntu-16.04.7-desktop-amd64.iso

You’ll have to use du command to get the real size of a directory in Linux.

du -h /opt/sys/new_dir/

This is the output for the new_dir directory.

root@vps:~# du -h /opt/sys/new_dir/
2.6M    /opt/sys/new_dir/ACCDownload_20230804152427/Audio
2.6M    /opt/sys/new_dir/ACCDownload_20230804152427
804K    /opt/sys/new_dir/ACCDownload_20230804143438/Audio
812K    /opt/sys/new_dir/ACCDownload_20230804143438
84M     /opt/sys/new_dir/

If you find the output of the du command too verbose and would like to see just the total size of the directory in a human readable format, you can use the sum option -s.

du -sh /opt/sys/new_dir/
84M     /opt/sys/new_dir/

Leave a Comment

Required fields are marked *