Saturday, August 13, 2016

du (disk usage) Command

The basic syntax for du is:

 du [options] [directories and/or files]

Commands

du -h

For display everything sorted by file size - # du –sk .[A-Z]* *|  sort –n

Display screenful output at a time as du generates more output than can fit on the console / screen:
Command - # du –h | less

To find top 3 directories, enter - # du –sk * | sort –nr | -3

Show summary in bytes - # du –b or du –ah

To get the last time modification - # du –ah –time

To arrange the output items according to size, du can be piped to the sort command, whose -n option tells it to list the output in numeric order with the smallest files first, as follows:
Command - # du | sort –n

As du will often generate more output than can fit on the monitor screen at one time, the output will fly by at high speed and be virtually unreadable. Fortunately, it is easy to display the output one screenful at a time by piping it to the less filter, for example,
Command - # du -h | less

The output of less can be advanced one screenful at a time by pressing the space bar, and it can be moved backward one screenful at a time by pressing the b key.
The output of du can likewise be piped to less after it has been passed through one or more other filters, for example,

Command - # du -h | sort -n | less

There are several other ways of monitoring disk space consumption and reporting file sizes. Although very useful tools, they are generally not good substitutes for du.
Among them is the df command, which is likewise used by system administrators to monitor disk usage. However, unlike du, it can only show the space consumption on entire partitions, and it lacks du's fine-grained ability to track the space usage of individual directories and files.
du is not designed to show the space consumption of partitions. The closest that it can come is to show the sizes of the first tier of directories in the root directory (i.e., the directory which contains all other directories and which is represented by a forward slash), several of which may be on their own partitions (depending on how the system has been set up). This is accomplished by becoming the root user and issuing the following

command:- #  du -h --max-depth=1 /

No comments:

Post a Comment