linux command to get size of files and directories present in a particular folder?
Categories:
Mastering Linux: How to Get File and Directory Sizes

Learn essential Linux commands to accurately determine the size of files and directories within any given folder, crucial for disk space management and system monitoring.
Understanding disk space usage is a fundamental skill for any Linux user or administrator. Whether you're troubleshooting a full disk, optimizing storage, or simply curious about where your space is going, knowing how to quickly ascertain the size of files and directories is invaluable. This article will guide you through the most common and effective Linux commands to achieve this, providing practical examples and tips for various scenarios.
The 'du' Command: Disk Usage
The du
(disk usage) command is your primary tool for inspecting directory and file sizes. It estimates file space usage. When used without any arguments, du
will recursively list the disk usage of each file and subdirectory within the current directory, which can be overwhelming for large folders. Therefore, it's almost always used with specific options to make its output more readable and useful.
du -sh /path/to/directory
Get the human-readable total size of a specific directory.
Let's break down the common options for du
:
du
command reports disk space usage, which might be slightly different from the actual file size reported by ls -l
due to block allocation on the filesystem. du
shows the space actually consumed on disk.Common 'du' Options Explained
Understanding these options will allow you to tailor du
's output to your specific needs.
flowchart TD A[Start: du command] --> B{Options?} B --> |No options| C[Recursive listing of all files/dirs] B --> |'-h' (Human-readable)| D[Sizes in K, M, G] B --> |'-s' (Summarize)| E[Total size of specified dir/file] B --> |'-a' (All files)| F[Include individual file sizes] B --> |'-c' (Grand total)| G[Add a grand total at the end] B --> |'-x' (One filesystem)| H[Stay on current filesystem] C --> I[End] D --> I E --> I F --> I G --> I H --> I
Flowchart of common 'du' command options and their effects.
Here are some practical examples of using du
:
# Get human-readable size of all subdirectories and files in the current directory
du -h
# Get human-readable total size of the current directory
du -sh .
# Get human-readable total size of a specific directory, e.g., /var/log
du -sh /var/log
# List human-readable sizes of all files and directories (including hidden ones) in the current folder, sorted by size
du -ah | sort -rh
# Find the top 10 largest directories in /home
du -h /home | sort -rh | head -n 10
Practical examples of the 'du' command for various scenarios.
The 'ls' Command: Listing File Sizes
While du
is excellent for directories, the ls
command is more commonly used for individual files and for a quick overview of file sizes within a single directory level. The key option here is -l
for long listing format, combined with -h
for human-readable sizes.
ls -lh /path/to/folder
List files and their human-readable sizes in a specific folder.
When you use ls -lh
, you'll see output similar to this:
-rw-r--r-- 1 user group 1.2K Jan 1 10:00 file1.txt
-rw-r--r-- 1 user group 5.7M Jan 1 10:05 document.pdf
drwxr-xr-x 2 user group 4.0K Jan 1 10:10 subdirectory
The fifth column shows the size of the file or directory. For directories, ls
typically shows a small, fixed size (e.g., 4.0K), which represents the size of the directory entry itself, not the content within it. For the content of directories, du
is the correct tool.
ls -lh
shows the size of the directory entry, not the cumulative size of its contents. For directory contents, always use du
.