原链接:http://blog.pcwuyu.com/2015/591.html

原分类:Arch Linux, Linux


Today we will discuss about how to find or summarize the disk usage on Unix/Linux systems using the du command. du stands forDisk Usage. In this handy tutorial, let us see how to use du command in real time.

Syntax

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

Usage

Let us see some practical examples. In this tutorial, I will be using /home/sk/unixmen/ directory to test du command. The /home/sk/unixmen directory contains the following content.

$ ls /home/sk/unixmen/ Akon - So Blue.mp4 panel.zip Senthilkumar_Resume.pdf sk.txt Ubuntu 13.10

1. Find Disk Usage with out any options

Entering the du command without any parameters will display following result.

$ du /home/sk/unixmen/

Sample output:

673800    /home/sk/unixmen/Ubuntu 13.10
679084    /home/sk/unixmen/

By default, du command displays the result in Kilobytes.

2. Display Disk Usage including all files and folders

As you see in the above result, If you execute du command without any parameters, it will display disk usage occupied by the directories only. So, If you want to find the disk usage of all files and directories, use -a flag.

$ du -a /home/sk/unixmen/

Sample output:

516    /home/sk/unixmen/panel.zip
4672    /home/sk/unixmen/Akon - So Blue.mp4
0    /home/sk/unixmen/sk.txt
673796    /home/sk/unixmen/Ubuntu 13.10/ubuntu-13.10-server-i386.iso
673800    /home/sk/unixmen/Ubuntu 13.10
92    /home/sk/unixmen/Senthilkumar_Resume.pdf
679084    /home/sk/unixmen/

3. Display Disk Usage in human readable format

As you see in the above outputs, the disk usage size is shown as some random numbers (679084) which is difficult to understand the exact size, atleaset for beginners. So let us display the disk usage size in human readable format such as in KB, MB, GB etc.

To calculate the disk usage in human readable format, use -h flag.

$ du -ah /home/sk/unixmen/

Sample Output:

516K    /home/sk/unixmen/panel.zip
4.6M    /home/sk/unixmen/Akon - So Blue.mp4
0    /home/sk/unixmen/sk.txt
659M    /home/sk/unixmen/Ubuntu 13.10/ubuntu-13.10-server-i386.iso
659M    /home/sk/unixmen/Ubuntu 13.10
92K    /home/sk/unixmen/Senthilkumar_Resume.pdf
664M    /home/sk/unixmen/

Now you can easily understand the disk usage sizes displayed in KB, MB, or GB format.

4. Display Disk Usage in MB or GB only

To display the disk usage of files and directories in Megabytes, or Gigabytes, use the parameter -B followed by M or G.

To display disk usage in Megabytes, enter the following command:

$ du -BM /home/sk/unixmen/

Sample output:

659M    /home/sk/unixmen/Ubuntu 13.10
664M    /home/sk/unixmen/

To display disk usage in Gigabytes, enter the following command:

$ du -BG /home/sk/unixmen/

Sample output:

1G    /home/sk/unixmen/Ubuntu 13.10
1G    /home/sk/unixmen/

5. Display Total Disk Usage

To display the total disk usage of files and directories, use -c option.

$ du -c /home/sk/unixmen/

Sample output:

673800    /home/sk/unixmen/Ubuntu 13.10
679084    /home/sk/unixmen/
679084    total

6. Display Total Disk Usage only

The previous command with -c option displays total size of the Disk Usage along with directories. If you want to skip all the results, and to display the total disk usage only, then use -s option.

$ du -s /home/sk/unixmen/

Sample output:

679084    /home/sk/unixmen/

To display the same result in human readable format, use -h option along with -s option.

$ du -sh /home/sk/unixmen/

Sample output:

664M    /home/sk/unixmen/

7. Display Disk Usage by excluding particular file types

Want to display the disk usage by excluding particular file types? For example, here we will summarize the disk usage result by excluding zip files.

$ du -h --exclude=*.zip* /home/sk/unixmen/

Sample output:

659M    /home/sk/unixmen/Ubuntu 13.10
663M    /home/sk/unixmen/

8. Display the Disk Usage by modification time

Do you want to display the disk usage by the modification time of files and directories? If yes, use –time option.

$ du -h --time /home/sk/unixmen/

Sample output:

659M    2014-02-25 17:56    /home/sk/unixmen/Ubuntu 13.10
664M    2014-02-25 17:56    /home/sk/unixmen/

I hope you get the enough details and usage of du command. For more advanced details, please refer the man pages.

$ man du

Cheers!