How to Free Disk Space From Your Linux Terminal

5 minute read

Motivation

Sometimes we run out of disk space and we’re forced to do some cleaning but we’re not sure what to delete, and what files are using most of our precious disk space.

In this article we’ll discuss how to find these informations:

  • What disks do you have in use
  • Total size of Disk Space
  • How much disk space is used and how much is left
  • How to find the heaviest files

After reading this you’ll have simple commands that will help you to quickly identify what is using your disk space fast.

Identify disk usage

You can use the df command, it stands for disk free. It displays the amount of available and used disk space on the file systems mounted on your computer.

A good system administrator practice is to use the man command to know how to use the command at hand. Let’s do that.

man df

man df

You will see a list of options available in the command, the one that its of interest to us is the -h option. Let’s use the command without arguments and the -h argument. This is an example from my server.

df with -h

As we observe, the -h option will give us a more human friendly sizes.

To quit the man page just press q.

This is the details of that information:

  • File System: The name of the file system or partition.
  • 1K-blocks: The total number of 1KB blocks allocated to the file system.
  • Used: The number of 1KB blocks that are currently used.
  • Available: The number of 1KB blocks that are still available for use.
  • Use%: The percentage of the file system’s capacity that is currently used.
  • Mounted on: The directory where the file system is mounted.

With this information alone you can see the usage of your main partitions.

In the previous example of my server you can see that my main partition is called /dev/vda1 with:

  • Size: 25GB of capacity
  • Used: 20GB
  • Available: 4.6GB
  • Used%: 82%

I only have one main partition, but you may have more. Keep in mind that the File Systems could be one of these:

  • /dev/sda1: A partition on the first hard disk.
  • tmpfs: A temporary file system stored in RAM.
  • nfs://server/path: A network file system mounted over a network.

But you only care about your partitions.

Identifying the folders that use most of the space

Previously we used df to find what partions we have, what space they have left and where the are mounted. In my case the partion /dev/vda1 is mounted in the root directory /.

We’ll use a series of commands to find who is responsable for using our precious space.

Its time to find what files are the heavies within that partition. For that, we’ll use the du (directory usage) command.

If we see the manual for this command:

man du

man du

du - estimate file space usage Summarize device usage of the set of FILEs, recursively for directories.

Let’s use du command in the home directory and pass the arguments:

-s: To summarize per each file. "display only a total for each argument"
-h: To print it in human-readable format.

As we’re searching in the root directory, we’ll need to use sudo.

sudo du -sh /*

du with error

Now, lets ignore the errors by adding 2>/dev/null this suppresses error messages for inaccessible files (like the ones in /proc).

sudo du -sh /* 2>/dev/null

du without error

Now let’s sort these directories and list only the top 10 heaviest. To do that we’ll use two commands: sort and head.

  • sort is a command that will help us to sort the list, if we use -rh it will sort in human-readable reverse order (largest first).
  • head takes the top elements of the list, we can specify how many with -n 10
du -sh /* 2>/dev/null | sort -rh | head -n 10

du sort head

Now we know that we should look for inside the /var directory. With 17GB of use is by far the heaviest directory.

du search

And busted! There it is, Docker! probably my docker images and volumes are using too much space, Docker uses 11GB of space in my disk!

meme

How about the heaviest files?

What we did before, it’s a top-down approach to find the heaviest folder in out disk. Now let’s try to find the heaviest files.

Now that you know how to use commands like du, sort and head lets introduce a very handy one to the pipeline: find.

NOTE: Now we’ll be searching files within the Home Directory ~.

find ~/ -type f -exec du -h {} + 2>/dev/null | sort -rh | head -n 10

Ok, I know, there’s a lot going on there, but there are elements that you’re already familiar with, like the tail of the pipeline sort -rh | head -n 10.

If we see the manual of find we’ll find the in the ACTIONS secction the following:

-exec command {} +
      This  variant  of the -exec action runs the specified command on
      the selected files, but the command line is built by appending
      each selected file name at the end; the total number of invoca‐
      tions of the command will be much less than the number of matched
      files.  The command line is built in much the same way that xargs
      builds its command lines.  Only one instance of `{}' is allowed
      within the command, and it must appear at the end, immediately
      before the `+'; it needs to be escaped (with a `\') or quoted to
      protect it from interpretation by the shell.  The command
      is executed in the starting directory.  If any invocation with
      the `+' form returns a non-zero value as exit status, then find
      returns a non-zero exit status.  If find  encounters  an  error,
      this  can  sometimes cause an immediate exit, so some pending
      commands may not be run at all.  For this reason
      -exec my-command ... {} + -quit may not result in my-command
      actually being run.
      This variant of -exec always returns true.

In other words if we write find <folder> -exec du -h {} + what find will do is the apply the du -h command on each file that the find command finds.

Lets break it down:

  • find ~/ -type f: This command finds all files (-type f) in your home directory (~).
  • -exec du -h {} +: For each file found, du -h calculates its size in a human-readable format (e.g., MB, GB).
  • sort -rh: Sorts the output by size, largest first (-rh).
  • head -n 10: Limits the output to the top 10 largest files.

Summary

To know your disk usage:

df -h

To find heaviest folders:

du -sh /* | sort -rh | head -n 10
du -sh /* 2>/dev/null | sort -rh | head -n 10

To find heaviest files withing a directory. Home directory in this example (~)

find ~/ -type f -exec du -h {} + | sort -rh | head -n 10
find ~/ -type f -exec du -h {} + 2>/dev/null | sort -rh | head -n 10

And there you have it, this is probable one of the most useful combos to see your disk usage and identify the heavist files.

About Me

I’m a Software Engineer, writer, tech enthusiast, origami lover, amateur photographer, I love trees.

My tech: JavaScript, Node.js, React, Ruby, Bash, Docker.

You can follow me on X, LinkedIn or check on more articles.

If you liked this posts, please consider supporting me by buying me a coffee! :)