How to Find and Delete Large Files on Your VPS or Server
Is the disk space on your VPS or server suddenly full for no apparent reason? Don't worry, this article will show you 3 effective ways to find the files or directories that are consuming the most space and how to handle them safely.
1. Common Causes of a Full Disk
Before we begin, let's identify a few common culprits that often cause this issue:
- Large Log Files: System or application logs (in /var/log) that are not being rotated and grow indefinitely.
- Old Backups: Forgotten backup files like .tar, .zip, or .sql archives.
- Temporary Files: The /tmp directory filled with temporary data that hasn't been cleaned up.
- User's Trash Bin: Sometimes the root user deletes files, but they remain in a trash directory.
2. Methods for Finding Large Files
Here are three command-line tools, from basic to advanced, to help you hunt down those "giant" files.
Method 1: Using the find Command (Basic)
This is the method you mentioned, which is very useful for finding files of a specific size. We can improve the command slightly to produce more readable output.
Command to find all files larger than 1GB across the entire system:
Command breakdown:
- find /: Start searching from the root directory.
- -type f: Search for files only, ignoring directories.
- -size +1G: Find files larger than 1 Gigabyte. You can change this to +100M for 100 Megabytes, or +10G for 10 Gigabytes.
- -exec ls -lh {} ;: Execute the ls -lh command to display detailed, human-readable information for each file found.
- 2>/dev/null: Hide error messages (e.g., "Permission denied").
Method 2: Using du and sort (Advanced)
This method is very powerful for finding which directories are consuming the most space.
Command to find the top 10 largest files/directories in a specific path (e.g., /var):
This command will list the 10 largest items in the /var directory, helping you quickly pinpoint the problem area.
Method 3: Using ncdu (Recommended & User-Friendly)
ncdu (NCurses Disk Usage) is a disk usage analyzer with an intuitive interface directly in your terminal. It allows you to easily navigate through directories and see what's taking up space.
Step 1: Install ncdu
sudo yum install ncdu
# For Ubuntu/Debian
sudo apt-get install ncdu
Step 2: Run the Scan
To scan the entire filesystem, simply run:
After the scan is complete, you can use the arrow keys to navigate, inspect, and even delete files/directories directly from the ncdu interface.
Important Warning Before Deleting
Not every large file is junk! Before deleting any file, make sure you know what it is. Accidentally deleting an operating system file or a database file can completely break your VPS.
Safety Tip: If you find a log file (e.g., error.log) that is too large, instead of removing it with rm, truncate it using the following command. This is safer as it doesn't delete the file's inode, which prevents errors with running processes.