du command in Linux is used to estimate the file space usage. With du (disk usage) command disk usage will be summarized for files and directories recursively. For du command input is file or directory. We cam pass wild card characters like * or *.txt etc also.
du (Disk Usage) command syntax:
du [option] [FILE]
Display all files size in same line:
With -0 option output can be displayed on same line instead of new line. By default new line will append for each entry in the output.
$ du -0
$ du --null
$ du -0
0 ./.X11-unix0 ./.ICE-unix0 ./.esd-50000 ./.esd-00 ./tracker-extract-files.00 ./systemd-private-3ec50d55a8f743fdb644f19d92c6723d-rtkit-daemon.service-3AG59h/tmp0 ./systemd-private-3ec50d55a8f743fdb644f19d92c6723d-rtkit-daemon.service-3AG59h0 ./systemd-private-3ec50d55a8f743fdb644f19d92c6723d-chronyd.service-c5iBWe/tmp0 .
Display files size inside directories:
By default du command displays the disk usage for directories and files outside the directories. To display the disk usage details for files inside the directories also use -a or –all option.
$ du -a
$ du --all
$ du -a
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
If you want check all the df (disk free) command examples in Linux please visit this page.
Display apparent size of files:
Instead of displaying actual file sizes we can display apparent size of files using –apparent-size. File sizes in apparent option might be smaller than actual file size.
$ du --apparent-size
1 ./test/.mozilla/extensions
1 ./test/.mozilla/plugins
1 ./test/.mozilla
14 ./test/.config/pulse
14 ./test/.config
15 ./test
15 .
Display disk usage size in block sizes:
du command displays the disk sizes in bytes format. To display the disk usage size of the disk in more readable format like KB, MB, GB use -b or –block-size option.
$ du -B M
$ du --block-size=MB
$ du -B M
0M ./test/.mozilla/extensions
0M ./test/.mozilla/plugins
0M ./test/.mozilla
1M ./test/.config/pulse
1M ./test/.config
1M ./test
1M .
Display disk usage size in bytes:
Use -b or –bytes option to display the disk usage of files and directories in bytes format.
$ du -b
$ du --bytes
$ du -b
6 ./test/.mozilla/extensions
6 ./test/.mozilla/plugins
51 ./test/.mozilla
13938 ./test/.config/pulse
13957 ./test/.config
14699 ./test
14717 .
If you want check all the cat command examples in Linux please visit this page.
Display the grand total of all files and directories size:
To display grand total summary of the disk usage size of files and directories use -c or –total option.
$ du -c
$ du --total
$ du --total
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
32 ./test/.config/pulse
32 ./test/.config
52 ./test
52 .
52 total
Dereference the symlinks in the output:
To dereference the symlinks while checking the disk usage size use -D or –dereference-args options.
$ du -D
$ du --dereference-args
$ du -H
Display disk usage for number of sub directories in a directory:
We can limit the disk usage details of sub directories for every directory by using -d or –max-depth option. Based on the max-depth value we provided the du command will traverse that level and displays the disk usage details.
$ du -d 2
$ du --max-depth=3
$ du --max-depth=2
0 ./test/.mozilla
32 ./test/.config
52 ./test
52 .
Display the output in human readable format:
By default disk size displayed in bytes format. We can display the usage details in human readable formats like KB, MB, GB etc.
$ du -h
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
32K ./test/.config/pulse
32K ./test/.config
52K ./test
52K .
Display inodes information of files and directories:
To display the inodes information use –inodes option in du command.
$ du --inodes
1 ./test/.mozilla/extensions
1 ./test/.mozilla/plugins
3 ./test/.mozilla
7 ./test/.config/pulse
8 ./test/.config
17 ./test
18 .
Display the output in block sizes:
The disk usage of files and directories can be displayed in block sizes like KB, MB, GB etc by using -k or –block-size.
$ du -k
$ du -m
$ du --block-size=M
$ du -k
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
32 ./test/.config/pulse
32 ./test/.config
52 ./test
52 .
$ du -m
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
1 ./test/.config/pulse
1 ./test/.config
1 ./test
1 .
Do not display symbolic links data:
While checking the disk usage to not follow the symbolic links use -P or –no-dereference option. This is the default option.
$ du -P
$ du --no-dereference
Do not calculate subdirectories size:
For directories if you don’t want to calculate the size of subdirectories use -S or –separate-dirs option in du command.
$ du -S
$ du --separate-dirs
du -S
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
32 ./test/.config/pulse
0 ./test/.config
20 ./test
0 .
Display the disk usage in powers of 1000:
By default disk usage check with calculate the usage in powers of 1024 and prints the output size. If you want to count the size in powers of 1000 use –si option.
$ du --si
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
33k ./test/.config/pulse
33k ./test/.config
54k ./test
54k .
Display the total summary of each file or directory:
Summarize the disk usage size for each file and directory and prints the count use -s option or –summarize.
$ du -s
$ du --summarize
$ du --summarize
14276 .
Display the last modification time of files and directories:
With –time option we can display the last modification time of files, directories, sub directories.
$ du --time
0 2022-09-21 05:04 ./.cache/evolution/tasks
0 2022-09-21 05:04 ./.cache/evolution
7512 2022-10-12 00:13 ./.cache/tracker
2360 2022-09-21 05:05 ./.cache/gnome-software/appstream
1532 2022-10-11 12:51 ./.cache/gnome-software/shell-extensions
4 2022-09-21 05:06 ./.cache/gnome-software/flatpak-system-default
4 2022-09-21 05:06 ./.cache/gnome-software/flatpak-user-user
1324 2022-10-11 12:51 ./.cache/gnome-software/odrs
5224 2022-10-11 12:51 ./.cache/gnome-software
552 2022-09-21 05:05 ./.cache/gstreamer-1.0
0 2022-09-21 05:07 ./.cache/yelp/WebKitCache/Version 16/Blobs
4 2022-09-21 05:07 ./.cache/yelp/WebKitCache/Version 16
4 2022-09-21 05:07 ./.cache/yelp/WebKitCache
0 2022-09-21 05:07 ./.cache/yelp/CacheStorage
4 2022-09-21 05:07 ./.cache/yelp
13652 2022-10-12 00:25 ./.cache
4 2022-09-21 04:59 ./.dbus/session-bus
4 2022-09-21 04:59 ./.dbus
Display output based on atime, access, use, ctime, status:
Instead of last modified time we can use atime, ctime, access, use, status fields to display the time for files, directories, subdirectories.
$ du --time=atime
$ du --time=ctime
$ du --time=access
$ du --time=use
$ du --time=status
$ du --time=atime
0 2022-10-11 13:50 ./.cache/evolution/tasks
0 2022-10-11 13:58 ./.cache/evolution
7512 2022-10-12 00:42 ./.cache/tracker
2360 2022-10-11 13:58 ./.cache/gnome-software/appstream
1532 2022-10-12 00:12 ./.cache/gnome-software/shell-extensions
4 2022-10-11 13:58 ./.cache/gnome-software/flatpak-system-default
4 2022-10-11 13:58 ./.cache/gnome-software/flatpak-user-user
1324 2022-10-11 13:58 ./.cache/gnome-software/odrs
5224 2022-10-12 00:12 ./.cache/gnome-software
552 2022-10-11 13:58 ./.cache/gstreamer-1.0
0 2022-10-11 13:58 ./.cache/yelp/WebKitCache/Version 16/Blobs
4 2022-10-11 13:58 ./.cache/yelp/WebKitCache/Version 16
4 2022-10-11 13:58 ./.cache/yelp/WebKitCache
0 2022-10-11 13:58 ./.cache/yelp/CacheStorage
4 2022-10-11 13:58 ./.cache/yelp
13652 2022-10-12 00:42 ./.cache
4 2022-10-11 13:58 ./.dbus/session-bus
4 2022-10-11 13:58 ./.dbus
Display time details in multiple formats.
We can display the time details for files and directories in below mentioned formats.
$ du --time-style=full-iso
$ du --time-style=long-iso
$ du --time-style=iso
$ du --time-style=+%H:%M
Exclude files using file pattern:
Use –exclude option to exclude files that match the pattern provided in the command option. You can use wildcard characters like * in patterns as below.
$ du --exclude=PATTERN
$ du --exclude=*test_data*
In below output we are printing the du command output. Here all the files, directories size will be displayed.
$ du
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
32 ./test/.config/pulse
32 ./test/.config
52 ./test
52 .
In below output we are excluding all the files and directories containing config in word.
$ du --exclude=*config*
0 ./test/.mozilla/extensions
0 ./test/.mozilla/plugins
0 ./test/.mozilla
20 ./test
20 .
Please check this shell script to demonstrate all the du (disk usage) command examples.
For more information on du (disk usage) command please visit du command manual site here.