Bash Shell

While the Linux command ls can display the sizes of files, it doesn’t work properly with directories, which will always be displayed as 4096 bytes. You’ll need to use the du command to recurse into subdirectories and print out a total.

Using The du Command

The best replacement for ls is to use du with a couple flags:

du -had 1

The -h flag stands for “human readable,” and will format the sizes in KB, MB, and GB for you rather than making you fetch a calculator.

The -a flag is “all,” and will include individual files as well as directories, making it useful for mimicking the way ls works.

The -d 1 flag is a limit on how deep du will print the results. By default, it’s configured to print every single subdirectory out, which can be a pain if you have lots of nested files. The -d flag will add them all up and print out the total for each visible directory from where you execute the command.


If you’d prefer it list every directory, you can run the command with the --apparent-size flag, which will display the total size of each subdirectory as if you had done right click > Properties in your file explorer.

This is all jumbled though, so if you’d like to view the biggest directories in the current folder, you can pipe the output to the sort command. Make sure to also use the -h flag, or else sort will treat 128MB as a bigger number than 1GB.

du -had 1 | sort -rh

By default, du uses the current directory, but you can also target specific directories. Just pass the location in as the last argument.

du -had 1 ./world/

Finding The Largest Directories On Your Server

A common task on Linux is debugging large amounts of disk usage. If you’d like to view total usage for each disk, you can use df -h, or install a monitoring tool like glances which will show it more cleanly:

However, this doesn’t drill down into directories and won’t find the source of the problem. For that, you can use du, but remove the -d 1 flag so it will search every directory. Target it at root, sort it, and filter for the top 25 directories so your screen isn’t overflown.

du -ah / | sort -rh | head -n 25

This may print out a few errors for files that it can’t access; make sure you run it with sudo.

Profile Photo for Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times.
Read Full Bio »