How to Find Disk Usage of Files and Directories in Linux
The Linux ‘du‘ (Disk Usage) is a standard Unix/Linux command used to check the disk usage information of files and directories on a machine.
The du command has many parameter options that can be used to get the results in many formats. The du command also displays the files and directory sizes in a recursive manner.
This article explains useful ‘du‘ commands with examples, which can help you determine the sizes of files and directories in Linux. The information provided in this article is sourced from the ‘du‘ command’s man pages.
How to Find the Size of a Directory in Linux
1. To obtain a summary of disk usage for a directory tree ‘/home/tecmint‘ and its subdirectories, use the following command.
# du /home/tecmint 4 /home/tecmint/bin 4 /home/tecmint/.ffbinaries-cache 672 /home/tecmint/.openjfx/cache/17.0.3-ea 676 /home/tecmint/.openjfx/cache 680 /home/tecmint/.openjfx 36 /home/tecmint/.ssh 92 /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml 96 /home/tecmint/.config/xfce4/xfconf 12 /home/tecmint/.config/xfce4/desktop ... 56758808 /home/tecmint
The output of the above command displays the number of disk blocks in the /home/tecmint directory along with its sub-directories.
How to Find the Directory Size in Human Readable Format
2. By employing the '-h'
option with the ‘du‘ command, you can obtain results in a ‘Human Readable Format‘, which enables you to view sizes in units such as Bytes, Kilobytes, Megabytes, Gigabytes, and so on.
# du -h /home/tecmint 4.0K /home/tecmint/bin 4.0K /home/tecmint/.ffbinaries-cache 672K /home/tecmint/.openjfx/cache/17.0.3-ea 676K /home/tecmint/.openjfx/cache 680K /home/tecmint/.openjfx 36K /home/tecmint/.ssh 92K /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml 96K /home/tecmint/.config/xfce4/xfconf 12K /home/tecmint/.config/xfce4/desktop ... 55G /home/tecmint
How to Find Total Size of a Directory in Linux
3. To obtain a summary of the total disk usage size for a directory, use the '-s'
option as follows.
# du -sh /home/tecmint 55G /home/tecmint
4. Utilizing the '-a'
flag with the ‘du‘ command displays the disk usage of all files and directories.
# du -a /home/tecmint 4 /home/tecmint/fix-deb-src-error-ubuntu 4 /home/tecmint/tecmint.py 0 /home/tecmint/bin/FoxitReader 4 /home/tecmint/bin 4 /home/tecmint/.ffbinaries-cache 52 /home/tecmint/Eclipse-with-PyDev.jpg 300 /home/tecmint/Screenshot from 2023-07-27 14-02-45.png 16 /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so 28 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so 20 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so 64 /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so 512 /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so ... 56759528 /home/tecmint
5. Employing the '-a'
flag along with '-h'
displays the disk usage of all files and folders in a human-readable format. The resulting output is easier to comprehend, as it presents file sizes in units such as Kilobytes, Megabytes, and so on.
# du -ah /home/tecmint 4.0K /home/tecmint/fix-deb-src-error-ubuntu 4.0K /home/tecmint/tecmint.py 28K /home/tecmint/download.php?file=%2Foomph%2Fepp%2F2023-06%2FR%2Feclipse-inst-jre-linux64.tar.gz.1 0 /home/tecmint/bin/FoxitReader 4.0K /home/tecmint/bin 4.0K /home/tecmint/.ffbinaries-cache 52K /home/tecmint/Eclipse-with-PyDev.jpg 300K /home/tecmint/Screenshot from 2023-07-27 14-02-45.png 16K /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so 28K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so 20K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so 64K /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so 512K /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so ... 55G /home/tecmint
6. To determine the disk usage of a directory tree and its subdirectories in Kilobyte blocks, utilize the '-k'
option (displays sizes in 1024 bytes units).
# du -k /home/tecmint 4 /home/tecmint/bin 4 /home/tecmint/.ffbinaries-cache 672 /home/tecmint/.openjfx/cache/17.0.3-ea 676 /home/tecmint/.openjfx/cache 680 /home/tecmint/.openjfx 8 /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview 12 /home/tecmint/.uk.co.screamingfrog.seospider.ui.b 36 /home/tecmint/.ssh 92 /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml 96 /home/tecmint/.config/xfce4/xfconf 12 /home/tecmint/.config/xfce4/desktop 4 /home/tecmint/.config/xfce4/xfwm4 8 /home/tecmint/.config/xfce4/panel/launcher-18 8 /home/tecmint/.config/xfce4/panel/launcher-17 8 /home/tecmint/.config/xfce4/panel/launcher-14 8 /home/tecmint/.config/xfce4/panel/launcher-3 8 /home/tecmint/.config/xfce4/panel/launcher-16 8 /home/tecmint/.config/xfce4/panel/launcher-5 ... 56769128 /home/tecmint
7. To obtain a summary of the disk usage for a directory tree, including its subdirectories, in Megabytes (MB) only, use the '-mh'
option as follows. The '-m'
flag counts the blocks in MB units and '-h'
stands for human-readable format.
# du -mh /home/tecmint 4.0K /home/tecmint/bin 4.0K /home/tecmint/.ffbinaries-cache 672K /home/tecmint/.openjfx/cache/17.0.3-ea 676K /home/tecmint/.openjfx/cache 680K /home/tecmint/.openjfx 8.0K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview 12K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b 36K /home/tecmint/.ssh 92K /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml 96K /home/tecmint/.config/xfce4/xfconf 12K /home/tecmint/.config/xfce4/desktop 4.0K /home/tecmint/.config/xfce4/xfwm4 8.0K /home/tecmint/.config/xfce4/panel/launcher-18 8.0K /home/tecmint/.config/xfce4/panel/launcher-17 8.0K /home/tecmint/.config/xfce4/panel/launcher-14 8.0K /home/tecmint/.config/xfce4/panel/launcher-3 ... 55G /home/tecmint
8. The '-c'
flag provides a grand total of the disk space usage at the last line. If your directory takes up 55GB of space, then the last two lines of the output would appear as follows.
# du -ch /home/tecmint 4.0K /home/tecmint/bin 4.0K /home/tecmint/.ffbinaries-cache 672K /home/tecmint/.openjfx/cache/17.0.3-ea 676K /home/tecmint/.openjfx/cache 680K /home/tecmint/.openjfx 8.0K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview 12K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b 36K /home/tecmint/.ssh 92K /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml 96K /home/tecmint/.config/xfce4/xfconf 12K /home/tecmint/.config/xfce4/desktop 4.0K /home/tecmint/.config/xfce4/xfwm4 8.0K /home/tecmint/.config/xfce4/panel/launcher-18 8.0K /home/tecmint/.config/xfce4/panel/launcher-17 8.0K /home/tecmint/.config/xfce4/panel/launcher-14 8.0K /home/tecmint/.config/xfce4/panel/launcher-3 8.0K /home/tecmint/.config/xfce4/panel/launcher-16 ... 55G /home/tecmint 55G total
How to Exclude Files with du Command
9. The following command calculates and displays the disk usage of all files and directories, excluding files that match the given pattern.
In the provided example, the command excludes '.txt'
files when calculating the total size of a directory. This approach allows you to exclude specific file formats using the '--exclude'
flag.
Upon reviewing the output, you’ll notice the absence of entries for '.txt'
files.
# du -ah --exclude="*.txt" /home/tecmint 4.0K /home/tecmint/fix-deb-src-error-ubuntu 4.0K /home/tecmint/tecmint.py 0 /home/tecmint/bin/FoxitReader 4.0K /home/tecmint/bin 4.0K /home/tecmint/.ffbinaries-cache 52K /home/tecmint/Eclipse-with-PyDev.jpg 300K /home/tecmint/Screenshot from 2023-07-27 14-02-45.png 16K /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so 28K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so 20K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so 64K /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so 512K /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so 28K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_freetype.so 672K /home/tecmint/.openjfx/cache/17.0.3-ea ... 55G /home/tecmint
How to Find Directory Usage By Modification Time
10. To display disk usage based on the modification time, use the '--time'
flag as shown below.
# du -ha --time /home/tecmint 4.0K 2023-07-06 13:15 /home/tecmint/fix-deb-src-error-ubuntu 4.0K 2023-06-14 12:50 /home/tecmint/tecmint.py 0 2021-12-07 12:26 /home/tecmint/bin/FoxitReader 4.0K 2021-12-07 12:26 /home/tecmint/bin 4.0K 2022-10-10 11:02 /home/tecmint/.ffbinaries-cache 52K 2023-05-12 12:09 /home/tecmint/Eclipse-with-PyDev.jpg 300K 2023-07-27 14:02 /home/tecmint/Screenshot from 2023-07-27 14-02-45.png 16K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so 28K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so 20K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so 64K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so 512K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so 28K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_freetype.so 672K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea 676K 2022-10-01 12:41 /home/tecmint/.openjfx/cache ... 55G 2023-08-14 10:22 /home/tecmint
The du command is versatile and can be tailored to your needs using various options and flags. Use man du
in the terminal for more detailed information about its options and usage.