Running out of disk space on your Ubuntu server can cause performance issues and prevent applications from running properly. This guide provides straightforward steps to help you check disk space usage and clean up your server effectively.
- Check Current Disk Space Usage
To see how much disk space is currently being used, open your terminal and run:
df -h
- df displays disk space usage.
- -h makes the output human-readable.
This command shows disk usage for all mounted filesystems. Look at the Use% column to identify filesystems with high usage.
- Identify Large Directories
To find directories that are using a lot of space, use:
du -ah / | sort -rh | head -n 20
- du -ah /: Displays the disk usage of all files and directories starting from the root, in human-readable format.
- sort -rh: Sorts the output by size, largest first.
- head -n 20: Shows the top 20 largest items.
This will list the largest directories and files on your server.
- Inspect Specific Directories
If a particular directory is using a lot of space, inspect it further with:
du -ah /path/to/directory | sort -rh | head -n 20
Replace /path/to/directory with the actual path to the directory you want to check. This command lists the largest files and directories within the specified directory.
- Remove Unnecessary Packages and Clean Cache
To free up space by removing unnecessary packages and cleaning the package cache, run:
sudo apt-get autoremove
sudo apt-get clean
- autoremove removes packages that are no longer needed.
- clean clears out the local repository of retrieved package files.
- Delete Old Log Files
Log files can accumulate over time. To delete log files older than 30 days, use:
sudo find /var/log -type f -name "*.log" -mtime +30 -exec rm -f {} \;
- /var/log: Directory where log files are typically stored.
- -mtime +30: Finds files modified more than 30 days ago.
- -exec rm -f {}: Deletes the files found.
- Empty the Trash
If files are in the Trash, they still occupy disk space. To empty the Trash for the current user, run:
rm -rf ~/.local/share/Trash/* This command deletes all files in the Trash directory for the current user.
- Remove Large Files Manually
For any large files you no longer need, delete them manually with:
rm /path/to/large-file
Replace /path/to/large-file with the path to the file you want to delete.
- Compress Files to Save Space
To save space, compress files or directories you don’t need frequently:
tar -czvf archive-name.tar.gz /path/to/directory
- tar -czvf: Compresses files into a .tar.gz archive.
- archive-name.tar.gz: Name of the output archive.
- /path/to/directory: Directory you want to compress.
- Automate Regular Cleanup
To automate regular disk cleanup tasks, set up a cron job. Edit your crontab with:
crontab -e
Add a line like the following to run a cleanup script every Sunday at 3 AM:
0 3 * * 0 /path/to/your/cleanup-script.sh
Replace /path/to/your/cleanup-script.sh with the path to your script.
Conclusion
By following these steps, you can regularly monitor and clean up disk space on your Ubuntu servers, ensuring they run smoothly and efficiently. Keep this guide handy for quick reference whenever you need to free up space.
Save this guide as a reference to maintain your server’s health and prevent disk space issues from impacting performance.