How to Check the Size of a Directory in Linux

Efficiently managing file systems is crucial in Linux. Knowing how to check directory size is an essential skill for any Linux user or administrator. This article focuses on two powerful commands – du and df – that allow you to calculate directory size and monitor disk space usage on your Linux system.

The du command offers a range of options to calculate directory size. On the other hand, the df command provides a comprehensive overview of file system disk space usage.

This article has provided a comprehensive guide to using these commands effectively, enabling users to calculate directory size and monitor disk space usage.

Why Check Directory Size in Linux?

Monitoring directory size is vital for several reasons:

  • Identifying large files and directories: Quickly finding large files and directories helps you optimize storage space and improve system performance.
  • Optimizing storage space: Regularly check directory size Linux enables you to identify areas where you can free up disk space, reducing the risk of running out of storage capacity.
  • Improving system performance: Large directories can slow down your system. By monitoring directory size, you can take corrective action to maintain optimal system performance.
  • Detecting potential issues: Unusual Linux directory size increases can indicate potential issues, such as data breaches or malware infections, allowing you to take prompt action to address and prevent data security threats.

Method 1: Using the du Command

The du command is a simple and effective way to calculate the size of a directory. The basic syntax is:

du /path/to/directory
document-4

Here we’re checking the disk usage for the Documents directory which returns the value 4. This might be consuming for some users as what this value represents in terms of size. 

Using the -h Option

In such situations, we can use the -h flag which makes the output more human-readable and makes it easier to understand

du -h ~/Documents
document-4.0k

The output shows that the disk usage of the Documents directory is 4k Bytes which is generally considered to be empty. We verified this using the ls command as well which returns nothing in the output.

Using the –max-depth=1 Option

Sometimes the output can be overwhelming and may clutter the output if you use the du command to an entire disk. 

So we can use the –max-depth=1 option to display the content of the main directories and its immediate subdirectories only

du -h --max-depth=1
max-depth

It can be seen in the output that the total space consumed by the directory is 13M bytes.

Using the -s Option

If we’re interested in only displaying the total file size of any directory rather than its content then we can use the -s option which stands for summary:

du -sh
du-sh

Here the (.) represents that we’re in the current working directory. We can use these commands for a different directory as well by providing its complete path.

Real-Time Scenario Utilizing the du Command

Sometimes the system storage is almost full and we want to know which directories are consuming the most space and then we can remove unnecessary data from them.

This can be done by executing the below command:

du -h --max-depth=1 / | sort -rh
usr-var-snap-boot-home

The top 5 directories containing most of the space can be seen in the output. Now we can access them one by one to see their further content using the ls command to check folder size Linux. After that, we can delete any unnecessary commands to make more space.

Method 2: Using the df Command

The df command stands for “disk free.” It displays information about the file system and checks disk space usage on your Linux system. It shows you how much space is available and used on mounted file systems.

df
$-df

The df command outputs a table with the following columns:

  • Filesystem: The device name or mount point of the file system.
  • 1K-blocks: The total number of 1 kilobyte blocks on the file system.
  • Used: The number of used blocks.
  • Available: The number of available blocks.
  • Use%: The percentage of used blocks.
  • Mounted on: The mount point of the file system.

Using the -h option

To display the information in human-readable format, you can use the -h option of the df command:

df -h
$-df-h

You can also use the df command for a specific file system as well. For example, we used the df command for /dev/sda2 file system:

df -h /dev/sda2
$-df-h-dev-sda2

Using the -T Option

The -T option tells you what type of file system is mounted at each point. This is useful for understanding the features and limitations of each file system:

df -T
$df-t

Using the -i Option

The -i option stands for “inode” and displays information about the number of inodes on each file system.

Inodes are data structures that hold metadata about files and directories. Running out of inodes can prevent you from creating new files, even if you have free disk space:

df -i
$df-i

Notably, you can also combine multiple options together to customize the output as per your requirements:

df -hT /dev/sda2
$df-ht

Conclusion

Managing file systems efficiently is crucial in Linux, and knowing how to check the size of a directory is an essential skill. The article explored different methods to calculate directory size in Linux, including the du and the df command. The du command is a simple and effective way to calculate the size of a directory, with options like -h for human-readable output, –max-depth=1 for displaying main directories and immediate subdirectories, and -s for displaying the Linux folder size and total file size.

The df command provides details like the total number of blocks, used blocks, available blocks, and the percentage of used blocks. It offers options like -h for human-readable format, -T for file system type, and -i for inode information. Monitoring directory size is vital for identifying large files and directories, optimizing storage space, improving system performance, and detecting potential issues.

By using commands like du and ncdu, you can effectively check the size of a directory in Linux, ensuring efficient disk space management and optimized system performance. You can practice these commands on Ultahost’s Free VPS hosting, offering a great opportunity to try out VPS hosting without any upfront cost. With a variety of VPS plans available, you can easily find one that meets your needs.

FAQs

How do I check the size of a directory in Linux?
Can I check the sizes of all directories within a directory?
How do I check the sizes of directories recursively?
Can I sort the directories based on their sizes?
How can I check the size of a directory and its contents without including hidden files?

Related Post

How to configure PHP parameters on Linux Serv

PHP a widely used server-side scripting language plays ...

How to Install GCC Compiler on Linux

GCC (GNU Compiler Collection) is a widely used compiler...

How to Remove the Lock Symbol from Files and

The lock symbol primarily indicates restricted or limit...

How to Check Running Processes in Linux?

In Linux, you can efficiently manage your system by che...

Exploring Sed Command in Linux

The sed command stands for stream editor is a powerful ...

How to Use the which Command in Linux

The which command in Linux is a simple yet powerful uti...

Leave a Comment