How to Get Folder Size in Linux

Yahya Irmak Feb 02, 2024
  1. Use the du Command to Get the Size of a Directory in Linux
  2. Use the df Command to Get the Size of a Directory in Linux
  3. Use the tree Command to Get the Size of a Directory in Linux
  4. Use the ncdu Command to Get the Size of a Directory in Linux
How to Get Folder Size in Linux

Operating systems use a file system to store files on the computer. These file locations are called folders on Windows and directories on Linux.

This article explains how to get the directory size on the command line in Linux. Then, we will examine the du, df, tree, and ncdu commands in Linux.

Use the du Command to Get the Size of a Directory in Linux

Linux terminal provides users with a powerful command-line interface. One of the features it provides is to find the sizes of directories.

The rest of the article explains the commands used to get file sizes and their parameters.

The du command stands for disk usage and prints the size of the specified directory and its subdirectories recursively. To find out the directory size with the du command, type the command name followed by the name of the directories.

If there are high-privileged sub-directories in the directory, add the sudo command at the beginning.

sudo du /var

However, these sizes are just numbers and may not be clear. The -h flag is used to display the sizes human-readable.

sudo du -h /var

Use the -s flag to find out the total size of the directory without showing each file in the directory one by one.

sudo du -sh /var

Use the -c flag to find the total size of all directories given as parameters.

Also, use the --max-depth flag if you want a maximum number of subdirectories printed on the screen. The --max-depth=0 is the same as -s.

sudo du -hc --max-depth=0 /var /mnt /tmp /etc

du -hc

Use the df Command to Get the Size of a Directory in Linux

The df command stands for disk free. It shows the total, used, and available space sizes of the file system. The -h flag is used to display the sizes human-readable.

df -h /tmp

df

Use the tree Command to Get the Size of a Directory in Linux

The tree command lists the contents of directories in a tree-like format. You can use the -d flag to list only directories without including files and the -h flag to display human-readable sizes.

Also, use the -L flag if you want a maximum number of subdirectories printed on the screen.

tree -d -h -L 1 /var

tree

If the tree tool is not installed on your operating system, you can install it with the following commands.

  1. For Arch Linux / Manjaro
sudo pacman -S tree
  1. For Ubuntu/Debian
sudo apt-get install tree
  1. For CentOS / RedHat
sudo yum install tree

Use the ncdu Command to Get the Size of a Directory in Linux

The ncdu is a visual version of the du command and provides a quick way to know what directories are using your disk space.

ncdu /var

ncdu

You can navigate between the directories using the up and down arrow keys and see the directory’s contents with the Enter key. Then you can exit the program with the q key.

If the ncdu tool is not installed on your operating system, you can install it with the following commands.

  1. For Arch Linux / Manjaro
sudo pacman -S ncdu
  1. For Ubuntu/Debian
sudo apt-get install ncdu
  1. For CentOS / RedHat
sudo yum install ncdu
Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn