In Unix and Linux systems, checking the size of a directory from the command line is a common task for system administrators and users alike. Whether you want to monitor disk usage, clean up space, or track the size of a specific directory, the du (disk usage) command is the most commonly used tool for this purpose. This guide will show you how to get the size of a directory and its contents using various command-line options.
Using the du
Command
The du command, short for “disk usage,” is the go-to tool for checking the size of directories and files in Unix/Linux. By default, du displays the size of each directory it processes.
Basic Syntax of du
The basic syntax of the du
command is as follows:
du [options] [directory]
Where:
- [options]: Specifies how you want to view the output.
- [directory]: The path of the directory you want to check.
Example 1: Get the Size of a Directory
To get the size of a specific directory, simply run the du command followed by the directory path:
du /path/to/directory
For example:
du /home/user/Documents
This command will display the size of the directory and all subdirectories in blocks. However, the output may not be very human-readable, so it’s often helpful to modify the output with options.
Example 2: Get the Size of a Directory in Human-Readable Format
To display the directory size in a human-readable format (e.g., KB, MB, GB), use the -h option:
du -h /path/to/directory
For example:
du -h /home/user/Documents
This will show the size of each subdirectory within the specified directory, along with the total size of the directory itself, in a readable format.
Example 3: Get the Total Size of a Directory
If you’re only interested in the total size of the directory and don’t want to see the sizes of individual subdirectories, use the -s (summary) option:
du -sh /path/to/directory
For example:
du -sh /home/user/Documents
This will display just the total size of the directory, in a human-readable format.
Using Additional Options with du
Example 4: Display the Size of a Directory with Depth Control
If you want to limit how deep du checks in a directory hierarchy, you can use the –max-depth option. This is useful when you only want to check the size of the top-level directory without drilling into subdirectories.
For example, to get the size of the top-level directory without listing subdirectories, use:
du -h --max-depth=1 /path/to/directory
For example:
du -h --max-depth=1 /home/user/Documents
This will show the size of /home/user/Documents
and its immediate subdirectories but will not list deeper subdirectories.
Example 5: Exclude Specific Files or Directories
You can use the –exclude option to exclude certain files or subdirectories from the size calculation.
For example, to exclude a directory named backup
within /home/user/Documents
:
du -sh --exclude='backup' /home/user/Documents
This command will display the size of /home/user/Documents
while ignoring the backup
folder.
Using the df
Command to Check Disk Space
While du is great for checking the size of specific directories, the df (disk free) command is useful for checking the overall disk usage of a filesystem. This command shows the available and used space on mounted filesystems.
Example 6: Check Overall Disk Usage with df
To check the overall disk usage of your system, use:
df -h
This command will display a summary of the disk usage of all mounted filesystems in a human-readable format (KB, MB, GB). Each row corresponds to a filesystem, showing the total size, used space, available space, and the mount point.
Combining du
with Other Commands
Example 7: Sorting Directory Sizes
You can combine du with other commands like sort to order the directories by size, which can help identify which directories are taking up the most space.
For example, to sort the output by size in descending order:
du -h /path/to/directory | sort -h
For example:
du -h /home/user/Documents | sort -h
This command will display the sizes of all subdirectories in /home/user/Documents
and sort them from smallest to largest.
Example 8: Find the Largest Directories
To list only the top 5 largest directories, you can combine du with sort and head:
du -h /path/to/directory | sort -rh | head -n 5
For example:
du -h /home/user/Documents | sort -rh | head -n 5
This command shows the five largest directories under /home/user/Documents
, sorted in descending order.
Alternative Method: ncdu
– A User-Friendly Disk Usage Analyzer
If you’re looking for an interactive tool that makes navigating and checking directory sizes easier, ncdu (NCurses Disk Usage) is an excellent option.
Installing ncdu
To install ncdu on your system, run:
sudo apt install ncdu # For Debian/Ubuntu
sudo yum install ncdu # For CentOS/RHEL
sudo pacman -S ncdu # For Arch Linux
Example 9: Running ncdu
Once installed, you can launch it by typing:
ncdu /path/to/directory
For example:
ncdu /home/user/Documents
This command will launch an interactive interface that lets you explore and sort directory sizes easily. You can use the arrow keys to navigate and inspect each subdirectory.
Conclusion
To get the size of a directory in Unix or Linux, the du command is the most commonly used tool, offering a range of options to view disk usage in a human-readable format, limit the depth of subdirectory inspection, and more. For those who prefer more control or need advanced functionality, combining du with commands like sort or using interactive tools like ncdu can make managing disk space much more efficient. Whether you’re checking space for regular maintenance or cleaning up your system, these tools make it easy to monitor and manage disk usage from the command line.
- Apple’s M3 iPad Air: Exciting Upgrades Expected Early Next Year - November 10, 2024
- Download The iPhone SE 3 Wallpapers for Your Devices Here - November 7, 2024
- OLED MacBook Air Might Face Delays Beyond 2027 Due to Cost and Supply Chain Challenges - November 7, 2024