How to Clean Up Disk Space by Finding Large Files Using "find" & "du"

Sun Nov 19 2023

Introduction

Are you constantly battling with low disk space? As we use our computers, it's natural for files to accumulate, often leading to a shortage of storage. Fortunately, with a few commands in the terminal, you can easily identify large files hogging your disk space and clean them up efficiently. Today, I'll show you how to use the du (disk usage) command in combination with find on Unix-like systems to locate and manage these space-consuming files.

Understanding the Basics

Before diving into the commands, it's important to understand what du and find do. The du command is used to estimate file space usage, and find is a powerful tool for searching files in a directory hierarchy.

Locating Large Files

To begin, open your terminal. We'll use a combination of find and du to locate files larger than a specified size. Let's say we're interested in files larger than 1GB. Here’s the command you’ll need:

bash
|
find / -type f -size +1G -exec du -h {} +
  • / indicates that the search should start from the root directory. You can replace this with a specific directory path.
  • -type f ensures that only files are considered, not directories.
  • -size +1G filters the search to files larger than 1 Gigabyte.
  • -exec du -h {} + executes the du command for each file found, displaying their sizes in a human-readable format.

Analyzing the Output

Once you run the command, you'll see a list of files, each with its size. This output is your key to understanding where your disk space is going. Look through the list to identify files that you no longer need or can move to external storage.

Cleaning Up

After identifying the large files, it's time for cleanup. You can delete unnecessary files directly from the terminal or manually through your file manager. If you choose to delete via terminal, use the rm command with caution, as files deleted this way are not easily recoverable.

Caution and Best Practices

  • Backup First: Always back up important data before deleting files.
  • Run as Standard User: Avoid using sudo unless necessary, as it gives you the power to delete system-critical files.
  • Narrow Down Your Search: To prevent an overwhelming amount of output, start your search in directories where large files are likely to be, like ~/Downloads or ~/Videos.
  • Check Before Deleting: Ensure the files you're about to delete are not crucial to system operation or personal use.

Conclusion

Running out of disk space doesn’t have to be a crisis. With these simple terminal commands, you can get a clear picture of what's using your space and take appropriate actions. Regular checks for large files can keep your disk usage optimized and your system running smoothly. Happy cleaning!