How To Check Disk Usage in Linux with the DU Command

Table of Contents

Often times as system admins we ask ourselves how do we check the disk space on our system? There are loads of utilities out there that can accomplish this but today we will be covering the du command in linux. du (disk usage) is a Linux CLI utility that allows users to see the estimated amount of disk space used by their files and directories. Unlike the df command, which gives a more detailed info of the file system type , du provides a “at a glance” overview about the filesystem type.  If you already haven’t added the df and du commands to check out, then you are missing out! du provides an easy way to check disk space in Linux. du has a variety of options and flags, but I’ll be focusing on a handful of go-to options that will help on how to check Linux disk space.

Check Linux Disk Usage

Getting started with du is easy, open any terminal and enter the following:

				
					du -sh
				
			

The du command displays the total size of all the contents of the current directory in a human-readable summary format (-sh). du can be pointed at both files, directories and other mount points, checking the disk usage of a specific file or directory can be done like so:

				
					du -sh some_file.txt 
#or
du -sh some_dir
				
			

Performing this command in your home directory shows the disk usage in its totality.

				
					#Example Output
du -sh
#17G .
#Example output without -h
du -s
#16822512
				
			

Note: running du without the -h flag is not recommend as it just makes it the output unclear at a glance. Also skipping the -s flag will give you a verbose listing of everything in the directory du is run in, thus plan ahead and pipe the output into something like less, pager, or more for a better time.

DU options

-a

Another useful option to use with du is the -a flag that can be used to display the size of all files and directories in the current directory, including hidden files.

				
					du -ha
				
			

Expect a very long output (or not) depending on where you run it.

-B

The -B option lets you choose the block size, defined by N, which is the number of bytes specified. 

				
					du -B N
				
			

-c

-c adds a ‘total’ report at the end of the output, which is useful if you are checking multiple files or directories at once.

				
					du -c -h ~/Documents
				
			

-d

One more option that I’ve found to be exceptional is the -d flag that tells how far for du to probe on its report. -d takes in a N number value to represent the level to stop reading at, this is especially useful in a directory with lots of files as running du -sh in that situation will give you a terminal readout that is miles long.

				
					du -hacd 1 ~/Documents
				
			

The above command will return a summary for all the directories in the current directory at the top level without delving into the sub-directories and file contents. Which rocks for when you just need a more detailed summary over -s. Specifying higher values will just go down to the next directory level, and so on.

—time

Another killer option is --time as it will display the last modified timestamp for all files and directories.
				
					du -hacd 1 --time /home/linuxman
#Sample --time output
4.0K	2022-12-20 19:01	./.wallaby
8.0K	2023-01-04 11:46	./.zshrc
4.0K	2023-02-06 11:05	./.Xauthority
4.0K	2022-01-08 10:31	./.bash_logout
288M	2023-01-26 19:59	./Downloads
...
17G	2023-02-07 16:24	total
				
			
—time can be further modified by passing the following arguments: --time=WORD and --time=STYLE.

—time=WORD

--time-style=style, changes the timestamp format from the standard to any of the following formats, where in place of style you pass: full-iso, long-iso, iso, or +FORMAT.

  • full-iso: includes the date, time, and time zone information.
  • long-iso: includes the date, time.
  • iso: includes only the date.
  • +FORMAT is just a placeholder for the date command, so the date syntax will fly here. Here’s a quick example:
				
					du -hacd 1 --time --time-style='+%Y-%m-%d %H:%M:%S' ~/Documents
				
			

The +%Y-%m-%d %H:%M:%S format string specifies the following format:

  • %Y: the year in XXXX format
  • %m: the month
  • %d: the day of the month
  • %H: the hour (24-hour clock)
  • %M: the minute
  • %S: the second

Excluding items

The last flag I’ll be talking about is the exclude option because sometimes we don’t need to see everything. du has a couple of options that allow us to leave specified files/directories from a report. The first one I’ll mention is -t.

-t

-t or --threshold is a great way to get a smaller list of files especially if you are looking for disk hogs, as you can specify in bytes (just pass any number), kilo (K), mega (M), and gigabytes (G). For example:

				
					du -hacd 1 -t 200M
				
			

Here I asked for all files above 200 megabytes. The --threshold version works the same way.

				
					du -hacd 1 --threshold=300K
				
			

—exclude

Finally we have the --exclude=PATTERN flag that allows us to remove entire directories from our search results. Here PATTERN simple means the directories to exclude in REGEX, so wildcards (*) can be used. Here’s an example of this:

				
					du -hac --exclude=path/to/exclude 
				
			

You can add multiple exclude options as needed to narrow your search results down. As shown below, I’ve chained a couple of excludes together along with a threshold of 2M.

				
					du -hacd 1 --exclude=Documents --exclude=Pictures --exclude=Downloads -t 2M
				
			

Option Summary

Option Action
-a, –all
write count of all files, not just directories.
-c, –total
produce grand total.
-d, –max-depth=N
print total for directory only if it is N or fewer levels below command line argument.
-h, –human-readable
print sizes in human readable format
-s, –summarize
display only total for each directory
–time
show time of last modification of any file or directory
–exclude=PATTERN
exclude files that match PATTERN, uses REGEX.
t, --threshold=SIZE
exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative

Conclusion

du is a simple yet powerful tool for finding those pesky disk hogs on your Linux system. Whether you’re a sysadmin or simply a day-to-day user wanting to keep your hard drive organized,duis where it’s at. du provides a great insight into  individual file sizes and distribution of the files/directories on your file system. Using du effectively will save a lot of heartaches in the long run when it comes to managing disk space, and you’ll feel like the coolest kid on the block because your hard drive won’t be full of crap! Thanks for reading and make sure to subscribe for more posts like this one!

Meet the Author

Leave a Reply