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.
How can I check disk usage for every month?
A kilobyte is 1000 bytes, always has (referring to the
-k
switch part). A kilo is always a unit of 1000 (e.g. a kilometer is also 1000 meters). A kibibyte is 1024 bytes. I know that Americans aren’t used to the metric system, but please, get it right when you do use it. I see these mistakes happen in blogs like these all the time…How can i find the 1st five largest in the file
/etc
.@Jilson,
Check this article to Find Out Top Directories and Files Disk Space in Linux.
If I want to calculate the size of files with particular extensions in a directory. How can I calculate it?
Does any one know or experienced the output of “du -csh” mismatched with grand total ?
For ex: du -csh *
10M File_A
14M Dir_A
22M total
but expectation is 24 MB as total.
There can be two reasons (that I can think of):
One is Rounding:
The sizes are quite quickly rounded up (a few k over the mark is enough).
10M File_A – might be 9.2M
14M Dir_A – might be 13.2M
23M total – because that adds up to 22.4, which again is rounded up.
But I think that can’t explain it fully.
It might be because you simplified your example and there are more of these files/directories of a few k which all get rounded up to 1M.
– The functioning of *
Another way is that there are files/directories starting with . in your main directory.
These are not shown in the list when you use *, but they are calculated in the total of the main directory.
I would like to mention also this combination, in order to find the biggest sub-folders in current location:
du -h –max-depth=1
thanks
Yes – I ended up going to an ancient site looking for that switch – it should be like number 2 on the list.
fyi: On some systems – freebsd is particular, depth is just -d [depth] – so “du -d 1 -h” is the secret sauce for the question ‘wtf happened to my space?’
Is is possible to collect historical data analysis of disk space usage per user ?
@rlinux,
No you can’t use du (disk usage) command to get the disk space usage per user, yes but you can do use some command-line tricks with the help of find and awk command to find out the disk space usage for all users on the system including root user, here is the command.
But i need want to get the historical disk space usage, not current usage.
You could start a cron to run this command at different times to log the historical data
find / -printf “%u %s\n” | awk ‘{user[$1]+=$2}; END{ for( i in user) print i ” ” user[i]}’
find / -printf “%u %s\n” | awk ‘{user[$1]+=$2}; END{ for( i in user) print i ” ” user[i]}’ > /yourlogdir/log_`date +_%m_%d_%Y_%H%M%S`
Another nice option is to sort by size: du -h * | sort -h
that’s a very desirable addition!
how to find du of / ie when I ran du -sh / it gives du: cannot access `./proc/14498/task/14518/fdinfo/35147′: No such file or directory
du: cannot access `./proc/14498/task/14518/fdinfo/35156′: No such file or directory
@Ashish,
du command can’t be run on partitions, it just meant for showing disk usage of only files/directories…
Thanks for tips. Well explain ans example
very nice work. It helped me.
Thanks.. Its really useful…
thanks a ton :)
Helpful
Hi Ravi,
Can you please help me with a command which can provide the Disk I/O Limit.
Please Note that, i am asking for the Disk I/O limit not the current io rate given by iostat.
Regards,
Manoj Rana
very handy. this helped me free up my disk space.
helpful tips. thank you.
Thank you for this. It is very helpful to me! I appreciate you sharing your knowledge.
another nice option I found very handsome:
du -h –max-depth=1
yeah :)
thanks this is what i was looking for.
Is there a way to only include certain types of files.
For example, something like:
du -ah –include=”*.php” /home/tecmint
PS: That’s a very helpful post – thanks for sharing!
You can run any command that gives you results in linux/unix by piping it through a grep command. So all you need to accomplish your task is something simple like du -ah | grep php. It would not accomplish your task perfectly since you would get files with php in the name as well but you would dig up all the .php files.