In Linux, it’s important to monitor the size of your directories to manage disk space effectively. This guide covers various methods to check the size of a directory using command-line tools.
Follow the steps:
- Using the “du” Command:
The du (disk usage) command is the standard tool for checking the size of a directory and its contents.- Basic usage: To check the size of a specific directory, use the following command:
du -sh /path/to/directory- -s: Provides a summary of the directory size (i.e., not listing individual files).
- -h: Displays the size in a human-readable format (KB, MB, GB, etc.).
Example:
du -sh /var/www
Output:
200M /var/www
- Display the size of all subdirectories: To see the size of a directory along with the size of its subdirectories, use:
du -h /path/to/directory
- Basic usage: To check the size of a specific directory, use the following command:
- Using the “ls” command:
While ls is generally used for listing files and directories, you can combine it with options to view file sizes.- List files with their sizes:
ls -lh /path/to/directory- ‘-l’: Displays a detailed list with file sizes.
- ‘-h’: Shows the sizes in a human-readable format.
However, note that ‘ls’ does not give you the total size of the directory; it only shows the size of individual files.
- List files with their sizes:
- Using the “df” Command:
The ‘df’ (disk free) command shows the disk space usage for entire file systems. It is useful to check the available space on your system.- Check available disk space:
df -h /path/to/directory
This will display the amount of free space, total space, and used space for the file system where the directory resides.
- Check available disk space:
- Using the “ncdu” Command (Optional):
‘ncdu’ is a disk usage analyzer with a user-friendly interface. It can be installed on most Linux systems and offers more interactivity compared to du.- Install ‘ncdu’:
sudo apt install ncdu # For Debian/Ubuntu-based systems
sudo yum install ncdu # For RHEL/CentOS-based systems - Usage: To analyze a directory:
ncdu /path/to/directory
‘ncdu’ will provide a navigable interface where you can see the size of directories and their contents.
- Install ‘ncdu’:
Monitoring directory sizes in Linux helps keep your file system organized and prevents disk space issues. The ‘du’ command is the go-to tool, but alternatives like ‘ls’, ‘df’, and ‘ncdu’ can also offer valuable insights depending on your needs.
We hope you found our article helpful. For more informative content, be sure to visit our Knowledge Base regularly.
Are you facing issues deleting files and directories on a Linux system? This guide explains how to use command-line tools for the task. Learn more by reading our article!