The „du“ Command Line
The „du“ (Disk Usage) command line is a standard command under Unix and Linux. It is used to list the disk space used by files on a machine and crucial for disk space management on unix and linux systems. Several paramaters enable users to format and filter the results.
Using du is a good starting point when trying to clean up unsed disk space under Unix.
Many smaller machines run Microsoft Windows. There is a wide range of graphical disk space managers for Windows, but few will enable you to scan Linux and Unix servers without having to resort to Samba. The Enterprise Edition of SpaceObServer will scan Unix and Linux servers via SSH.
Using du
To analyze disk usage in a specific directory tree, simply enter the command line.
root@server:/# du
Adding /* will show all directories and files at the top level of the file system:
root@server:/# du -s /*
You can also specify which folder shall be scanned:
root@server:/# du -s /var
There are several ways to filter the information and gain the insights required to proceed.
Parameters
A wide range of parameters can be used to refine the results. Many flags can be used in combination. This is a (by no means complete) selection of useful arguments.
du -s
Display the disk space used by all files in the selected directory by adding -s. You cannot limit the folder depths while using this argument.
root@server:/# du -s /* 32 /allout.txt 6768 /bin 19624 /boot 0 /dev 5832 /etc 3098116 /home 4 /html 0 /initrd.img 127284 /lib 4 /lib64 16 /lost+found 20 /media 4 /mnt 4 /opt 0 /proc 30700 /root 2040 /run 5848 /sbin 4 /selinux 4 /srv 0 /sys 8 /tmp 819524 /usr 1022520 /var 0 /vmlinuz
du -h
Adding the parameter -h shows the results in a human-readable format: It adds size information in Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, and Petabyte.
It is a useful argument to remember and can be attached to any other parameter.
du -c
Use the -c argument to sum up the used disk space in the last line of the results.
root@server:~# du -ch /var 8.0K /var/www 197M /var/lib 773M /var/cache 4.0K /var/local 16K /var/tmp 6.5M /var/backups 80K /var/spool 16K /var/mail 24M /var/log 4.0K /var/opt 1000M /var 1000M total
Combine -h and -s for Detailed and easy-to-read Information
-hs will show you all files in the folder and present the results in human-readable format:
root@server:/# du -hs * 32K allout.txt 6.7M bin 20M boot 0 dev 5.7M etc 3.0G home 4.0K html 0 initrd.img 125M lib 4.0K lib64 16K lost+found 20K media 4.0K mnt 4.0K opt 0 proc 30M root 2.0M run 5.8M sbin 4.0K selinux 4.0K srv 0 sys 8.0K tmp 801M usr 999M var 0 vmlinuz
The disk space manager TreeSize Professional provides similar information in the „Details“ view.
Sort Results
Add | sort to the command line to sort the output. | sort -rh sorts recursively and displays the biggest sizes on top.
root@server:/# du -s /* | sort -rh 3098116 /home 1022520 /var 819524 /usr 127284 /lib 30700 /root 19624 /boot 6768 /bin 5848 /sbin 5832 /etc 2040 /run 32 /allout.txt 20 /media 16 /lost+found 8 /tmp 4 /srv 4 /selinux 4 /opt 4 /mnt 4 /lib64 4 /html 0 /vmlinuz 0 /sys 0 /proc 0 /initrd.img 0 /dev
See the Top10
Limit the output to the first ten lines with the | head command and see the ten largest files or folders.
root@server:/# du -sh /* | sort -rh | head 3,0G /home 999M /var 801M /usr 125M /lib 30M /root 20M /boot 6.7M /bin 5.8M /sbin 5.7M /etc 2.0M /run
Exclude File Types
If you want to exclude certain file types from the equation, use the –exclude argument in combination with other flags. In this example, du will not count .BIN files.
root@server:~# du -sh /var 1000M /var root@server:~# du -sh --exclude="*.bin" /var 957M /var
du helps you gain an overwiew over disk space usage on your machine – collect the data and start from there.
The Easy Way
The server space manager SpaceObServer enables you to scan servers and visualize scan results. A detailed history of disk space usage changes is stored in a database and facilitates professional and easy server space management.