Tutorial: Managing Disk Space: Finding Largest Files via SSH on Ubuntu/Debian
Effective server maintenance requires regular monitoring of disk space usage. When a disk partition nears capacity, identifying the largest files is the most efficient way to reclaim space. The following guide details two methods for locating large files on Ubuntu and Debian systems using the command line.
Method 1: Using the find Command
The find command is highly efficient for locating files that exceed a specific size threshold. This is useful when searching for files larger than a defined limit (e.g., 500MB).
find /path/to/directory -type f -size +500M -exec ls -lh {} +
/path/to/directory: The starting point for the search.-type f: Specifies that only files (not directories) should be listed.-size +500M: Filters for files larger than 500 Megabytes.-exec ls -lh {} +: Executes thels -lhcommand to display details.
Method 2: Using the du and sort Commands
For identifying the “Top N” largest files or directories, combining du (disk usage) with sort is the preferred approach. This provides a ranked list.
du -ah /path/to/directory | sort -rh | head -n 10
du -ah: Calculates disk usage for all files and directories in human-readable format.sort -rh: Sorts the results by size in reverse (largest first) and interprets human-readable numbers (e.g., K, M, G).head -n 10: Restricts the output to the top 10 results.
Sample Output
The execution of the du command produces an output similar to the following:
4.2G /var/lib/docker/containers/a1b2c3d4/a1b2c3d4-json.log 2.1G /var/log/syslog 1.8G /home/user/backups/db_backup.sql 950M /usr/share/zoneinfo/data.bin ...