• Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'du' Command In Linux

2023-05-18 - By Robert Elder

     I use the 'du' command to show the amount of disk space taken up by parts of my file system.  By default, the 'du' command will print the amount of disk space used in kilobytes:

du
10244	./manifestos
2052	./pictures/old
4104	./pictures
1048584	./backups
1062936	.

     I can make the output more clear by including the '-h' flag:

du -h
11M	./manifestos
2.1M	./pictures/old
4.1M	./pictures
1.1G	./backups
1.1G	.

     You can list specific files or directories to see counts for only these items:

du -h pictures backups
2.1M	pictures/old
4.1M	pictures
1.1G	backups

     You can see a total count printed at the end using the '-c' flag:

du -h -c pictures backups
2.1M	pictures/old
4.1M	pictures
1.1G	backups
1.1G	total

     The '-s' flag can be used to omit the printing of subdirectories in the output:

du -h -c -s pictures backups
4.1M	pictures
1.1G	backups
1.1G	total

Finding the Largest File(s) In The Filesystem Using The 'du' Command

     I can use the 'du' command in a shell pipe to find the 5 largest files within the current directory:

find . -type f -exec du {} \; | sort -n | tail -n 5
1024	./pictures/old/cat.jpg
1024	./pictures/old/dog.jpg
2048	./manifestos/software-bloat-is-reducing-GDP.txt
8192	./manifestos/why-tabs-are-better-than-spaces.txt
1048580	./backups/my-huge-backup.dat

Finding the Largest File(s) Or Directories In The Filesystem Using The 'du' Command

     I can also list the 5 largest files or directories within the current directory using this command:

du -a | sort -n | tail -n 5
8192	./manifestos/why-tabs-are-better-than-spaces.txt
10244	./manifestos
1048580	./backups/my-huge-backup.dat
1048584	./backups
1062936	.

     You could also run this on a different directory like this:

du -a /my-favourite-directory | sort -n | tail -n 5

     And that's why the 'du' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Published 2023-10-04
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List

Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
Privacy Policy      Store Policies      Terms of Use