Or copy link
Copy link
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.
Monitoring directory size is vital for several reasons:
The du command is a simple and effective way to calculate the size of a directory. The basic syntax is:
du /path/to/directory
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
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
It can be seen in the output that the total space consumed by the directory is 13M bytes.
Learn about How to Find a File with Find Command in Linux.
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
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
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.
ls
Practice Commands On Our Linux VPS Today!
Ultahost offers Linux hosting with NVMe SSD storage, ensuring your Linux server loads faster and is more responsive.
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
The df command outputs a table with the following columns:
Using the -h option
To display the information in human-readable format, you can use the -h option of the df command:
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
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
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
Notably, you can also combine multiple options together to customize the output as per your requirements:
df -hT /dev/sda2
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.
du
ncdu
You can use the du command in Linux to check the size of a directory. Open a terminal and type du -sh <directory_path>. The -s option is used to display only the total size of the specified directory, and -h is used to display the output in human-readable format (e.g., KB, MB, GB).
Yes, you can use the du command without specifying a particular directory to check the sizes of all directories within the current directory. For example, du -sh * will display the sizes of all directories in the current directory.
To check the sizes of directories and their subdirectories recursively, you can use the du command with the -d or –max-depth option followed by the desired depth level. For example, du -h –max-depth=1 <directory_path> will display the sizes of all immediate subdirectories within the specified directory.
Yes, you can use the du command in combination with other commands like sort to sort directories based on their sizes. For example, du -h –max-depth=1 <directory_path> | sort -rh will display the sizes of all immediate subdirectories within the specified directory sorted in descending order.
By default, the du command includes hidden files and directories. To exclude hidden files and directories, you can use the –exclude option followed by a pattern to exclude them from the calculation. For example, du -sh –exclude=”.*” <directory_path> will exclude hidden files and directories from the size calculation.
The sleep command in Linux is designed to introduce a d...
Bash scripts are powerful tools that allow you to autom...
Environment variables play an important role in shaping...
Securing your website with SSL stands for Secure Socket...
When you are a Linux user, few tools commands are as po...
The "tar.gz" file format is a common archive format use...
Save my name, email, and website in this browser for the next time I comment.
Δ