Du

From Dikapedia
Jump to: navigation, search


du - estimate file space usage. Summarizes disk usage of each FILE, recursively for directories.

The two commands (df & du) reports their figures from different sources, and changes to the filesystems are not always considered in the same way.

The disk free command (abbreviated to "df") is the standard Linux/Unix command used to display available disk space for the file systems. The "df" command uses the statvfs() system call and asks the file system for the current space statistics. This information is obtained directly from the filesystem superblock, so only the whole values for the filesystem and not individual values for a single directory can be retrieved. The "Used" value amount reported includes files that are still held open by processes and in memory, but are no longer on the volume as mentioned above in the possible causes. These files would be flagged as "deleted" in the output of the "lsof" command, and the space consumed by these files will only be released once the process is no longer running.

The "df" command returns results quickly but not always accurately, where the "du" command takes longer, but is far more accurate representation of the persisted file system.

The disk usage command (abbreviated to "du") will display the file space allocated to each file and directory contained in the current directory. Links will be displayed as the size of the link file, not what is being linked to; the size of the content of directories is displayed, as expected. This reports allocation space and not the absolute file space on the the file system.

The "du" command interrogates the properties of existing files on the disk (volume) and does not include those files present in memory, thus it is a far more accurate.


How To Use Du



The Df command lets you know how much space is used by each file system, but even then, you still need to figure out what is consuming all of that disk space. This is where du comes in.


Du can report how much disk space is consumed by each directory. When piping it to sort command, you can see which directories consume the most disk space.


A good method is to save the results in /tmp (if there’s is enough space) so you can refer to the output multiple times and not have to rerun du.


Du command will not output anything to the screen but instead it creates a sorted list of which directories consume the most space and outputs the list to /tmp/duck-root.


If you then use tail on that file, you can see the top ten directories that use space:

$ cd /
$ sudo du -ckx | sort -n > /tmp/duck-root

$ sudo tail /tmp/duck-root
67872 /lib/modules/2.6.24-19-server
67876 /lib/modules
69092 /var/cache/apt
69448 /var/cache
76924 /usr/share
82832 /lib
124164 /usr
404168 /
404168 total

As you can see, /usr is taking up the most disk space, then /lib, then /usr/share, etc...

  • Note that the output separates out /var/cache/apt and /var/cache, so you can tell that /var/cache/apt is the subdirectory that consumes the most space under /var/cache.
If you just want to see the total du of a directory:
$ du -sh /home/ec2-user
28K	/home/ec2-user
 
$ du -sh /home/
32K	/home/
 
$ sudo du -sh /var
82M	/var
If you want to display the biggest directories in the current working directory, run:
# du -a | sort -n -r | head -n 5
If you want to find out top biggest directories under /home partition:
# du -a /home | sort -n -r | head -n 5