du (disk usage) command in Linux is used to check the disk usage for files and directories. It has more options to work with disk usage check for files and directories. I had written a detailed article about du (disk usage) command examples in below article. Please check it to learn more about every option in du command.
du (disk usage) command examples in Linux
Here i have developed a shell script to demonstrate all the du (disk usage) command options in a single script. You can copy and execute this script to check all the options of du commands with output. It is an addon script to demonstrate the du command examples. If you want to check each and every command and practice then check this link for more details.
Steps to execute this script:
- Create a file named du_command_examples.sh in any of the path in the server.
- Copy the below script to the file you create above and save it.
- Change the file permission to 755 with below command.
$ chmod 755 du_command_examples.sh
- Execute the script with below command.
$ sh du_command_examples.sh
Shell script for du (disk usage) command examples:
Below script will explain every option in du (disk usage) command in one place.
#!/bin/bash
#+--------------------------------------------------------------------------------+
#| Name : du_command_examples.sh |
#| Author: www.linuxtec.co |
#| Date : 13_OCT_2022 |
#| Purpose: Demonstrate all the du (disk usage) command examples in single script |
#+--------------------------------------------------------------------------------+
du_commands=('du -0' 'du --null' 'du -a' 'du --all' 'du --apparent-size' 'du -B M' 'du --block-size=MB' 'du -b' 'du --bytes' 'du -c' 'du --total' 'du -D' 'du --dereference-args' 'du -H' 'du -d 2' 'du --max-depth=3' 'du -h' 'du --inodes' 'du -k' 'du -m' 'du --block-size=M' 'du -P' 'du --no-dereference' 'du -S' 'du --separate-dirs' 'du --si' 'du -s' 'du --summarize' 'du --time' 'du --time=atime' 'du --time=ctime' 'du --time=access' 'du --time=use' 'du --time=status' 'du --time-style=full-iso' 'du --time-style=long-iso' 'du --time-style=iso' 'du --time-style=+%H:%M' 'du --exclude=*config*')
for (( i=0; i<${#du_commands[@]}; i++ ))
do
echo "====================== ${du_commands[$i]} ======================"
data=`${du_commands[$i]}`
echo "$data"
echo "===================================================="
echo ""
done
Sample output for this script is below.
====================== du -0 ======================
/root/du_command_examples.sh: line 15: warning: command substitution: ignored null byte in input
0 ./test/.mozilla/extensions0 ./test/.mozilla/plugins0 ./test/.mozilla32 ./test/.config/pulse32 ./test/.config52 ./test52 .
====================================================
====================== du --all ======================
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
4 ./test/.bash_logout
4 ./test/.bash_profile
4 ./test/.bashrc
8 ./test/.config/pulse/bb1681141a2c4cdbb8a4304d6d88aad1-device-volumes.tdb
4 ./test/.config/pulse/bb1681141a2c4cdbb8a4304d6d88aad1-stream-volumes.tdb
4 ./test/.config/pulse/bb1681141a2c4cdbb8a4304d6d88aad1-card-database.tdb
4 ./test/.config/pulse/cookie
4 ./test/.config/pulse/bb1681141a2c4cdbb8a4304d6d88aad1-default-sink
4 ./test/.config/pulse/bb1681141a2c4cdbb8a4304d6d88aad1-default-source
32 ./test/.config/pulse
32 ./test/.config
4 ./test/.esd_auth
4 ./test/.bash_history
52 ./test
52 .
====================================================
====================== du --apparent-size ======================
1 ./test/.mozilla/extensions
1 ./test/.mozilla/plugins
1 ./test/.mozilla
14 ./test/.config/pulse
14 ./test/.config
15 ./test
15 .
====================================================
====================== du -B M ======================
0M ./test/.mozilla/extensions
0M ./test/.mozilla/plugins
0M ./test/.mozilla
1M ./test/.config/pulse
1M ./test/.config
1M ./test
1M .
====================================================
====================== du --time ======================
0 2019-05-13 22:41 ./test/.mozilla/extensions
0 2019-05-13 22:41 ./test/.mozilla/plugins
0 2022-09-21 03:21 ./test/.mozilla
32 2022-10-11 13:12 ./test/.config/pulse
32 2022-10-11 13:12 ./test/.config
52 2022-10-11 13:16 ./test
52 2022-10-11 13:16 .
====================================================