Using ls to list directories and their total sizes
Categories:
Listing Directories and Their Total Sizes with ls

Learn how to effectively use the ls command in Linux/Unix to list directories and calculate their cumulative sizes, providing insights into disk usage.
Understanding disk space usage is crucial for system administration, development, and general file management. While the ls command is primarily known for listing directory contents, it can be combined with other utilities to provide valuable information about directory sizes. This article will guide you through various methods to list directories and their total sizes using ls and related commands, helping you identify large directories and manage your storage more efficiently.
Basic Directory Listing with ls
The ls command, by default, lists the contents of a directory. To list directories specifically, you can use the -d option, which tells ls to list the directory itself rather than its contents. However, ls -d alone doesn't show the size of the directory's contents. It only shows the size of the directory entry itself, which is usually a small, fixed value.
ls -d ./
ls -d /var/log
Listing directories themselves, not their contents.
Combining ls with du for Directory Sizes
To get the actual size of a directory, including all its subdirectories and files, the du (disk usage) command is the tool of choice. You can combine ls with du using command substitution or pipes to achieve the desired output. The du -sh command is particularly useful as it summarizes the total size of each directory in a human-readable format.
du -sh */
# Or, to list all directories and their sizes in the current path:
find . -maxdepth 1 -type d -print0 | xargs -0 du -sh
Using du -sh to get human-readable sizes for directories.
du -sh * command will list the size of all subdirectories and files in the current directory. Appending a / to * (*/) ensures that only directories are considered, preventing du from processing individual files in the current directory.Sorting Directories by Size
Once you have a list of directories and their sizes, it's often helpful to sort them to quickly identify the largest consumers of disk space. You can pipe the output of du to the sort command. The -h option for sort enables human-readable sorting, which correctly handles sizes like '1K', '2M', '3G'.
du -sh * | sort -rh
# To sort only directories:
find . -maxdepth 1 -type d -print0 | xargs -0 du -sh | sort -rh
Sorting directories by size in reverse human-readable order.

Workflow for listing and sorting directory sizes.
du on very large directory structures, especially at the root (/). It can take a significant amount of time and consume system resources. Always specify a target directory or use maxdepth with find.Advanced Filtering and Display
For more specific scenarios, you might want to filter the output or display it in a particular way. For instance, you can use grep to search for specific directory names or awk to format the output. The find command is incredibly powerful for locating directories based on various criteria before passing them to du.
# Find directories named 'cache' and show their sizes
find . -type d -name "*cache*" -print0 | xargs -0 du -sh
# List top 5 largest directories in the current path
du -sh * | sort -rh | head -n 5
Advanced filtering and displaying top largest directories.
By mastering these combinations of ls, du, find, xargs, and sort, you gain powerful tools for managing disk space and understanding your file system's layout. These commands are fundamental for any Linux/Unix user or administrator.