How to delete unused files in Linux CentOS
Tags: CentOS, linux, server administration
In this article i just want to learn some useful commands that helps you free up your server’s disk space by deleting unnecessary files.
Before we continue to looking at the different techniques, keep in mind that because in Linux anything is a file, you need to constantly make assured that the file(s) you cleaning are not necessary user or system files. Clearing up the content of a critical system or configuration files might result in a fatal application or system problems or malfunction.
You need to login via SSH as root user or a user with full administrative privileges.
Install yum utilities first
[root@ ~]# yum -y install yum-utils
Cleaning old log files
[root@ ~]# find /var -name "*.log" ( ( -size +100M -mtime +10 ) -o -mtime +30 ) -exec truncate {} --size 0 ;
This command will remove all files with extension .log that has at leat 10 days old and is larger than 100MB or it is older than 30 days. You can modify days or file size with what ever you want.
Cleaning YUM cache
[root@ ~]# yum clean all
This command just deletes remained cache files by YUM package installation. It doesn’t uninstall any YUM packages.
If you have removed or disabled some of YUM repositories then there is some cache files that remained intact. You can delete them by this command:
[root@ ~]# rm -rf /var/cache/yum
If you have more than one user that has root access to server then it is good also to clear tmp files of YUM command for other users:
[root@ ~]# rm -rf /var/tmp/yum-*
Cleaning YUM orphan packages
First we check if any orphan packages exists
[root@ ~]# package-cleanup --quiet --leaves --exclude-bin
If answer is yes then
[root@ ~]# package-cleanup --quiet --leaves --exclude-bin | xargs yum remove -y
Cleaning old kernel files
This is the safe command that just cleans unused old kernel files
[root@ ~]# package-cleanup --oldkernels --count=1
Cleaning error_log files in Cpanel
IF you use Cpanel for managing your web server then this command finds and cleans all error_log files inside root directories of your websites.
[root@ ~]# find /home/*/public_html/ -name error_log -delete
Free up space in Plesk Onyx
If you use Plesk control panel on your production server then you can use its GUI via this path:
https://yourserverip:8443/repair/
This section has some useful tools for cleaning unnecessary files.