SoFunction
Updated on 2025-05-08

Detailed explanation of common commands for viewing Linux directory size

1. Du Command Basics

duThe Disk Usage command is a utility in Linux/Unix systems for estimating file and directory disk usage. It recursively traverses the directory structure, computes the disk space occupied by each file and subdirectories, and displays the results in blocks (by default).

Basic syntax

du [Options] [File or directory]

If no file or directory is specified,duThe disk usage of the current directory and all subdirectories will be counted by default.

Why do I need the -h option?

OriginalduThe command output is in units of disk blocks (usually 1 block = 512 bytes or 1KB), which is not human reading-friendly. For example:

du data

Possible output:

123456  data/subdir1
789012  data/subdir2
912468  data

Such numbers are difficult to understand intuitively.-hThe emergence of the (human-readable) option solves this problem, which automatically selects the most appropriate unit (KB, MB, GB, or TB) to display the size:

du -h data

The output becomes:

4.0K    data/subdir1
8.2M    data/subdir2
1.2G    data

2. Detailed explanation of du -h command

Output interpretation

du -hThe typical output contains multiple lines of information:

  • Subdirectory line: Shows the size and path of each subdirectory
  • Total rows(Last line): Shows the total size of the specified directory

For example:

4.0K    data/subdir1
8.2M    data/subdir2
1.2G    data

here:

  • data/subdir1Occupy 4.0KB
  • data/subdir2Occupy 8.2MB
  • The wholedataDirectory occupies 1.2GB

Common options combinations

-s(Summary): Only the total size is displayed, not the subdirectory details

du -sh data

Output:

1.2G    data

-c(total): Add a total line at the end

du -hc data

Output:

4.0K    data/subdir1
8.2M    data/subdir2
1.2G    data
1.2G    Total dosage

--max-depth=N: Control the display directory depth

du -h --max-depth=1 data

Only display the size of the first-level subdirectories under data

3. Comparison between du and other commands

du vs df

  • du(Disk Usage): Calculate the space occupied by files and directories from the file system perspective
  • df(Disk Free): Displays the overall disk usage of the file system

duMore suitable for finding the space occupied by specific directories or files, anddfIt is more suitable for viewing the usage of the entire disk or partition.

du vs ls -l

  • ls -lWhat is displayed is the actual size of the file (logical size)
  • duIt shows the disk space occupied by the file (may be larger than the actual size due to block allocation)

For example, a 1-byte file:

  • ls -lShow 1 byte
  • duMay show 4KB (depending on the block size of the file system)

4. Practical application scenarios

1. Find the large directory

du -h / | sort -rh | head -n 20

This command combination:

  • Calculate all directory sizes starting from the root directory
  • Output in human-readable format
  • Sort in reverse order by size
  • Show the top 20 largest directories

2. Monitor user disk usage

du -sh /home/*

Quickly view disk usage for all user home directories.

3. Exclude specific directories

du -h --exclude='*.log' /var

Statistics/varDirectory size, but exclude all.logdocument.

4. Compare directory changes

du -sh data
# After doing some operationsdu -sh data

Compare the changes in directory size by performing twice.

5. Advanced skills and precautions

1. Processing symbolic links

By default,duIt will count the file size pointed to by the symbolic link. use-LOptions can follow symbolic links:

du -Lh /path

2. Display the modification time

Combined--timeOptions can display the last modification time:

du -h --time data

3. Performance optimization

For large file systems,duIt may take a long time. Can:

  • use--apparent-sizeShow apparent size rather than disk usage (faster but not accurate enough)
  • Limit directory depth--max-depth
  • Running during off-peak hours

4. Cross-file system statistics

defaultduIt will count other file systems under the mount point. use-xCan be restricted to the current file system:

du -xh /

6. FAQs

Q1: WhyduanddfIs the total space displayed inconsistent?

A1: Possible reasons include:

  • Files that have been deleted but still have process open
  • File system reserved space
  • Different statistical methods (dfCount the entire file system,duStatistics specific documents)

Q2: How to count the number of files in a directory instead of size?

A2: Usefindandwc

find data -type f | wc -l

Q3:duWhy is the displayed size larger than the actual file sum?

A3:duThe disk usage is counted, including:

  • The actual content of the file
  • File system metadata
  • Additional space due to block allocation

This is the end of this article about the detailed explanation of common commands for viewing Linux directory size. For more related contents for viewing Linux directory size, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!