The du command is used to get information about the disk usage in Linux. The du command provides a lot of useful information about disk usage like directory size, file size, free percentage, etc.
Print File Sizes as Byte
The du command is used to list given files and directories sizes in the byte unit. There is no extra parameter. The following command lists the current working directory and its child sizes.
du
Alternatively, we can specify different than the current working directory in order to list files and directories sizes. In this example, we will provide the absolute full path to list sizes.
du /var/log
Print File Sizes in Human Readable Format
By default the sizes are printed as byte unit which is not easy to read and understand. The human readable format can be used where the sizes are printed as kilobyte, megabyte, gigabyte etc. The -h option is used to print sizes in human readable format.
du -h /var/log
The output will be like below.
625M /var/log/journal/295c2cf105a140e5ab505a9e74ff560e 625M /var/log/journal 252K /var/log/apt 1012K /var/log/installer 4,0K /var/log/private 4,0K /var/log/gdm3 64K /var/log/unattended-upgrades 4,0K /var/log/openvpn 4,0K /var/log/lightdm 40K /var/log/cups 4,0K /var/log/sssd 64K /var/log/apache2 4,0K /var/log/dist-upgrade 32K /var/log/mysql 4,0K /var/log/speech-dispatcher 629M /var/log
Print Sizes For All Files and Directories
By default the du command prints only directory sizes with their contents. But by providing the -a option all file sizes can be also printed like below. Alternatively, the long format of the -a option can be used which is –all.
du -h -a /var/log
The output is like below. We can see that the files with *.gz and *.1 extension and their sizes are printed too.
4,0K /var/log/vmware-network.log 16K /var/log/boot.log.2 4,0K /var/log/kern.log.2.gz 625M /var/log/journal 180K /var/log/syslog.6.gz 1,3M /var/log/dpkg.log.1 4,0K /var/log/kern.log.3.gz 4,0K /var/log/kern.log.1 12K /var/log/apt/history.log 72K /var/log/apt/eipp.log.xz 96K /var/log/apt/term.log 32K /var/log/apt/term.log.1.gz ...
Print Sizes By Excluding Some Files and Directories
By default the du command prints all specified files and directories sizes. But we can also exclude some files and directories by using the -X option. The -X option long-form is –exclude. Some file or directory pattern is provided which will be excluded. In the following example, we will exclude files with the “*.gz” extension.
du -X="*.gz"
Exclude Symbolic Link
Linux uses the symbolic links heavily and different directories may contain symbolic links to the other directories or paths. We can exclude the symbolic links with the -L option like below.
du -h -L /var/log
Print Total Size Of All Directories
The total sizes of the directories can be printed by using the -c option. The total size includes given directory content and all of its child directories sizes. Alternatively, the long form of the -c option can be used which is –total.
$ du -h -c /var/log
625M /var/log/journal/295c2cf105a140e5ab505a9e74ff560e 625M /var/log/journal 252K /var/log/apt 1012K /var/log/installer 4,0K /var/log/private 4,0K /var/log/gdm3 64K /var/log/unattended-upgrades 4,0K /var/log/openvpn 4,0K /var/log/lightdm 40K /var/log/cups 4,0K /var/log/sssd 64K /var/log/apache2 4,0K /var/log/dist-upgrade 32K /var/log/mysql 4,0K /var/log/speech-dispatcher 629M /var/log 629M total
Print Sizes For Specified Level Files and Directories
By default the du command traverse all child directories and their contents. But this may be unnecessary for some situations. We can specify the directories to reverse and lower-level directories are not calculated. The -d option and the level number can be used for this operation. In the following example, we will just traverse level 1 directories and files.
du -d 1 /var/log
639020 /var/log/journal 252 /var/log/apt 1012 /var/log/installer 4 /var/log/private 4 /var/log/gdm3 64 /var/log/unattended-upgrades 4 /var/log/openvpn 4 /var/log/lightdm 40 /var/log/cups 4 /var/log/sssd 64 /var/log/apache2 4 /var/log/dist-upgrade 32 /var/log/mysql 4 /var/log/speech-dispatcher 644028 /var/log
Print Current Directory Size As Summary
The -s option of the du command can be used to print summary size of the file system like below.
$ du -s -h /var/log
629M /var/log
Print Modification Time of Files and Directories
Even it is not the main job of the du command the last modification time of the directories and folders can be listed with the –time option like below.
du --time -h /var/log
625M 2020-12-20 12:41 /var/log/journal/295c2cf105a140e5ab505a9e74ff560e 625M 2020-12-20 12:41 /var/log/journal 252K 2020-12-18 09:50 /var/log/apt 1012K 2020-11-02 17:23 /var/log/installer 4,0K 2020-10-22 16:59 /var/log/private 4,0K 2020-11-02 14:37 /var/log/gdm3 64K 2020-12-20 11:57 /var/log/unattended-upgrades 4,0K 2020-08-18 14:42 /var/log/openvpn 4,0K 2020-02-07 14:40 /var/log/lightdm 40K 2020-12-20 12:24 /var/log/cups 4,0K 2020-11-29 00:00 /var/log/sssd 64K 2020-12-20 00:00 /var/log/apache2 4,0K 2020-09-23 01:59 /var/log/dist-upgrade 32K 2020-12-20 00:00 /var/log/mysql 4,0K 2020-08-20 01:11 /var/log/speech-dispatcher 629M 2020-12-20 12:41 /var/log