du (Disk Usage) Command To Check Disk Usage Of Files and Directories/Folders In Linux

One common thing every admin should take care before installing a new application on system or system upgrade or upgrading system to next available version (operating system upgrade) is current disk space usage.

Whether sufficient disk space is available or not to perform an operation? because when you running out of disk space, you may face so many problem which we can’t tell you exactly what will cause.

Linux administrator has checklist for daily/routine activity which will help admin to maintain the environment without any issues in terms of up and running.

Checking system disk usage is one of the main task for administrator to avoid further damage on system.

Suggested Read : 3 Easy Ways To Get A Summary Of Disk Usage (Directory & Folders Size) In Linux

du command stands for Disk Usage. It’s a standard Unix program which used to estimate file space usage in present working directory.

It summarize disk usage recursively to get a directory and its sub-directory size. In this article we are going to use possible options with du command to show you, how efficiently we can use du command to get a disk usage.

Print Disk Usage Without Any Option

The following command will print the disk usage summary of a /home/magi directory and its sub directories (Excluded files in output).

$ du /home/magi
4	/home/magi/.dbus
4	/home/magi/.lnav/crash
72	/home/magi/.lnav/formats/default
4	/home/magi/.lnav/formats/installed
80	/home/magi/.lnav/formats
212	/home/magi/.lnav
8	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/#ajax.cdnjs.com
8	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/#www.cyberciti.biz
24	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys
28	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer
32	/home/magi/.macromedia/Flash_Player/macromedia.com/support
36	/home/magi/.macromedia/Flash_Player/macromedia.com

Print Disk Usage Output With Files

The below command will print the disk usage summary of a /home/magi directory. The output shows directory ,sub-directories and files. Use -a option with du command.

$ du -a /home/magi
4	/home/magi/.dbus
4	/home/magi/c63f0847ff645ab3ffc4f959ee1517c6bac6b471.zip
2164	/home/magi/gifine-create-animated-gif-in-linux-4.gif
0	/home/magi/.lnav/exec.history
4	/home/magi/.lnav/view-info-c64e378469faa12cdf59210e3908c4a4.ts1485845651.ppid3384.json
0	/home/magi/.lnav/cmd.history
0	/home/magi/.lnav/capture.history
4	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485844997.ppid3384.json
4	/home/magi/.lnav/view-info-8bfea43949fa2bc97e47f1c9be3a873f.ts1485844883.ppid3384.json
4	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485527812.ppid3384.json
20	/home/magi/.lnav/log_metadata.db

Print Disk Usage Output In Human Readable Format

To print the disk usage summary of a /home/magi in human readable format. By default df command shows the output in Bytes which is not clear and everyone get confused to understand the actual size. Use -h option with du command to get a output in proper format like Bytes, Kilobytes, Megabytes, Gigabytes, etc.,.

$ du -ah /home/magi
4.0K	/home/magi/.dbus
4.0K	/home/magi/c63f0847ff645ab3ffc4f959ee1517c6bac6b471.zip
2.2M	/home/magi/gifine-create-animated-gif-in-linux-4.gif
0	/home/magi/.lnav/exec.history
4.0K	/home/magi/.lnav/view-info-c64e378469faa12cdf59210e3908c4a4.ts1485845651.ppid3384.json
0	/home/magi/.lnav/cmd.history
0	/home/magi/.lnav/capture.history
4.0K	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485844997.ppid3384.json
4.0K	/home/magi/.lnav/view-info-8bfea43949fa2bc97e47f1c9be3a873f.ts1485844883.ppid3384.json
4.0K	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485527812.ppid3384.json
20K	/home/magi/.lnav/log_metadata.db

Print Disk Usage Output With Grand Total

To print the disk usage summary of a /home/magi with grand total in the output. Use -c option with du command.

$ du -ahc /home/magi
4.0K	/home/magi/.dbus
4.0K	/home/magi/c63f0847ff645ab3ffc4f959ee1517c6bac6b471.zip
2.2M	/home/magi/gifine-create-animated-gif-in-linux-4.gif
0	/home/magi/.lnav/exec.history
4.0K	/home/magi/.lnav/view-info-c64e378469faa12cdf59210e3908c4a4.ts1485845651.ppid3384.json
0	/home/magi/.lnav/cmd.history
0	/home/magi/.lnav/capture.history
4.0K	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485844997.ppid3384.json
4.0K	/home/magi/.lnav/view-info-8bfea43949fa2bc97e47f1c9be3a873f.ts1485844883.ppid3384.json
4.0K	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485527812.ppid3384.json
20K	/home/magi/.lnav/log_metadata.db
.
.
87M	/home/magi/Documents
160K	/home/magi/omnirun.tar
12K	/home/magi/examples.desktop
0	/home/magi/.xsel.log
4.0K	/home/magi/.nano
8.0K	/home/magi/.bashrc
0	/home/magi/.log
4.4G	/home/magi
4.4G	total

Print Only Grand Total

To print only a grand total disk usage summary of a /home/magi. Use -s option with du command.

$ du -sh /home/magi/
4.4G	/home/magi/

Print Disk Usage Output In Kilobyte

To print the disk usage summary of a /home/magi in Kilobyte format. Use -k option with du command.

$ du -k /home/magi/
4	/home/magi/.dbus
4	/home/magi/.lnav/crash
72	/home/magi/.lnav/formats/default
4	/home/magi/.lnav/formats/installed
80	/home/magi/.lnav/formats
212	/home/magi/.lnav
8	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/#ajax.cdnjs.com
8	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/#www.cyberciti.biz
24	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys
28	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer
32	/home/magi/.macromedia/Flash_Player/macromedia.com/support
36	/home/magi/.macromedia/Flash_Player/macromedia.com

Print Disk Usage Output In Megabyte

To print the disk usage summary of a /home/magi in Megabyte format. Use -m option with du command.

$ du -mh /home/magi/
4.0K	/home/magi/.dbus
4.0K	/home/magi/.lnav/crash
72K	/home/magi/.lnav/formats/default
4.0K	/home/magi/.lnav/formats/installed
80K	/home/magi/.lnav/formats
212K	/home/magi/.lnav
8.0K	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/#ajax.cdnjs.com
8.0K	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys/#www.cyberciti.biz
24K	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys
28K	/home/magi/.macromedia/Flash_Player/macromedia.com/support/flashplayer
32K	/home/magi/.macromedia/Flash_Player/macromedia.com/support
36K	/home/magi/.macromedia/Flash_Player/macromedia.com

Exclude Certain Files Format In Output

To print the disk usage summary of a /home/magi and exclude the text files in output as we don’t want to calculate the text files size. Use --exclude option with du command, also mention the file format which you want to exclude. In my case we are excluding the text files in output.

$ du -ah --exclude="*.json" /home/magi/
4.0K	/home/magi/.dbus
4.0K	/home/magi/c63f0847ff645ab3ffc4f959ee1517c6bac6b471.zip
2.2M	/home/magi/gifine-create-animated-gif-in-linux-4.gif
0	/home/magi/.lnav/exec.history
0	/home/magi/.lnav/cmd.history
0	/home/magi/.lnav/capture.history
20K	/home/magi/.lnav/log_metadata.db
4.0K	/home/magi/.lnav/crash
4.0K	/home/magi/.lnav/search.history
0	/home/magi/.lnav/sql.history
4.0K	/home/magi/.lnav/formats/default/dhclient-summary.lnav
4.0K	/home/magi/.lnav/formats/default/dump-pid.sh
56K	/home/magi/.lnav/formats/default/default-formats.json.sample
4.0K	/home/magi/.lnav/formats/default/partition-by-boot.lnav

Print An Output With Timestamp

Add the timestamp values in disk usage output to understand when it’s modified. Use --time option with du command.

$ du -ah --time /home/magi/
4.0K	2017-01-02 22:26	/home/magi/.dbus
4.0K	2017-04-14 07:57	/home/magi/c63f0847ff645ab3ffc4f959ee1517c6bac6b471.zip
2.2M	2017-06-07 14:41	/home/magi/gifine-create-animated-gif-in-linux-4.gif
0	2017-01-31 12:41	/home/magi/.lnav/exec.history
4.0K	2017-01-31 12:24	/home/magi/.lnav/view-info-c64e378469faa12cdf59210e3908c4a4.ts1485845651.ppid3384.json
0	2017-01-31 12:41	/home/magi/.lnav/cmd.history
0	2017-01-31 12:41	/home/magi/.lnav/capture.history
4.0K	2017-01-31 12:22	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485844997.ppid3384.json
4.0K	2017-01-31 12:11	/home/magi/.lnav/view-info-8bfea43949fa2bc97e47f1c9be3a873f.ts1485844883.ppid3384.json
4.0K	2017-01-27 20:07	/home/magi/.lnav/view-info-eea2b981759af8049b7ab01bdbde6789.ts1485527812.ppid3384.json
20K	2017-01-31 12:41	/home/magi/.lnav/log_metadata.db

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *