du Command in Linux - Show Disk Usage of Files and Directories + Examples

Channel: Linux
Abstract: du -sh * command displays line by line the size of each directory and files in the current directory. You may also use du -sh /path/to/file command to

Your computer often gets updates, new programs, logs that will obliviously consume disk space. You need to find how much space these objects consumed and act accordingly.

In a Linux system, you can use du command to find out disk space usage very easily.

Prerequisites
  • A Linux or UNIX-like system.
  • Basic understanding of the Linux command line.
  • A login user with sudo privileges or root privileges
What is du command in Linux

Du command calculates the amount of disk space consumed by files under a particular directory (or subdirectories). Whereas df command gives overall space utilization of filesystem and mounted disks.

How to use du command

The du command displays disk usage in the command line. Without any options shows it displays disk usage of every directory and sub directories recursively.

Open your terminal and type du and press Enter key:

du

du Output

The output shows disk usage on the left and full path, total disk usage of each parent directory, and the last line shows a summary. There is no unit of measure indicated from this output. By default, du shows the size in 1024 bytes.

Du Command Options

Some of the main du command options:

OptionsDescription-h , --human-readableHuman Readable Format-s, --summarizeTotal size of a Directory -aAll files and directories -kDisk usage in KB -mDisk usage in MB -c, --totalGrand total size--timeShow the time of the last modification -X, --exclude=PatternExclude files that match a pattern Useful Du Command with Examples

Familiarizing these commands help users to gain disk usage information easily. Let check some of the useful du commands.

Display Directory Size in Human Readable Format

The default output of du command is not user-friendly. It would be easier to understand if it prints the sizes for example 1K, 25M, 2G, etc. Type du -h command to print the size of the disk in a human-readable format.

du -h

You may also use du -h * command to print user-friendly disk usage of all files and directories in the current directory and subdirectories.

Du command has an option to set implicit behavior though -B, --block-size=SIZE. For example to set block size to 1GB:

du -h --block-size=1G
Display Summarize Disk Usage

The du outputs a lot of information in a directory tree that may be hard to when trying to find the combined size. You may use -s to summarize disk usage of a directory.

You may also du ../<directory-name> to print disk usage one level above. I love using -h option in conjunction for more readability.

To display the combined size of a specified directory type du -sh command followed by the path of the directory.

du -sh /home/linuxopsys/Projects

Whereas, du -sh * command displays line by line the size of each directory and files in the current directory.

You may also use du -sh /path/to/file command to display individual files size.

Display disk usage of all files and directories

Type du -a to list the sizes of all files and directories including file path. The difference with other du commands is that it displays file sizes as well. You can combine -h flag to ease of reading.

du -ah
Display Directory disk usage in Kilobytes (KB units)

Type du -k option to display directory usage in Kilobytes:

du -k
Display disk usage in Megabytes (MB units)

Type du -m option to display directory usage in Kilobytes:

du -m
Display grand total size

In the previous outputs, you might have seen the total usage at the bottom of the list. It's the same but the -c flag displays the text "total" showing the grand total disk usage for the specified directory or file.

To find the total size of specified files type du -ch /directory-path/*.jpg command.

Display disk usage based on last modification time

This is an extremely useful option. The --time option print the time of the last modification of files and directories along with disk usage.

You can see it sorts by time with the newest first in the list.

Exclude files that match a pattern

On some occasions, you may want to calculate the disk usage excluding some files that match a given pattern. For example, you can use -–exclude="*.jpg" option to exclude all jpg files and calculate the total size of the directory.

du -ah --exclude="*.jpg" Projects
Conclusion

In this tutorial, we learned about du command in Linux. There are many options that we have not covered here, you can refer to du man pages for all the information or simply type man du from your terminal.

Ref From: linuxopsys

Related articles