Clean Up Disk Space on CentOS

Commands to check disk space:

  1. $ df command – Shows the amount of disk space used and available in file systems.
  2. $ du command – Display the amount of disk space used by the specified files and for each sub-directory.
  3. $ btrfs fi df /device/ – Show disk space usage information for a btrfs based mount point/file system.
  4. Pass the -h option to see output in human readable format size in gigabytes or terabytes or megabytes: like $ df -h
  5. Display output using inode usage instead of block usage like $ df -i
  6. Pass the -T option to display the type of each file systems listed such as ext4, btrfs, ext2, nfs4, fuse, cgroup, cputset etc. like $ df -T
  7. Pass -t TYPE to only see specific file system liek $ df -t ext3
  8. To list all but exclude ext2 filesystem pass the -x TYPE option. like $ df -x ext2
  9. Pass the -a or –all option to the df command to show all file system. like $ df -a
  10. For btrfs filesystem use the btrfs fi df command to see space usage information for a mount point. The syntax is: btrfs filesystem df /path/ $btrfs fi df /dev/path

df COMMAND OUTPUT

Display
Name
Output 
option
Description
FilesystemsourceThe source of the mount point, usually a device.
1K-blockssizeTotal number of blocks.
UsedusedNumber of used blocks.
AvailableavailNumber of available blocks.
Use%pcentPercentage of USED divided by SIZE.
Mounted ontargetThe mount point.

You can pass the output format defined by ‘valid field name’ as follows:
$ df --output=field1,field2,...
$ df --output=source,used,avail /data/

Output Options
Human Readable

Clear disk space on CentOS

Following are quick commands to clear disk space on CentOS 6 or CentOS 7 servers.

$ yum -y install yum-utils

1- Trim Log Files

Following command will truncate any *.log files on the volume /var that are either older than 7 days and greater than 50M or older than 30 days.

$ find /var -name "*.log" \( \( -size +50M -mtime +7 \) -o -mtime +30 \) -exec truncate {} --size 0 \;

2- Clean Yum Cache

$ yum clean all
$ rm -rf /var/cache/yum # Remove orphaned data 
$ rm -rf /var/tmp/yum-* # Delete regular user cache

3- Remove Orphan Packages

# Check existing orphan packages
$ package-cleanup --quiet --leaves --exclude-bin 
# confirm removing
$ package-cleanup --quiet --leaves --exclude-bin | xargs yum remove -y 

4- Remove out dated kernels

# Keeps only latest two kernels, Note: reboot first in order to boot up from # latest kernel.
$ package-cleanup --oldkernels --count=2

5- Remove Composer cache & WP CLI cached WordPress downloads

# WordPress new setup site cache
$ rm -rf /root/.wp-cli/cache/*
$ rm -rf /home/*/.wp-cli/cache/*

# Remove Composer cache
$ rm -rf /root/.composer/cache
$ rm -rf /home/*/.composer/cache

6- Remove core dumps

#If some severe failures with PHP and core dumps enabled
$ find -regex ".*/core\.[0-9]+$" -delete

7-  Remove error_log files

$find /home/*/public_html/ -name error_log -delete

8- Remove Node.js caches

$ rm -rf /root/.npm /home/*/.npm /root/.node-gyp /home/*/.node-gyp /tmp/npm-*

9- Cannot find what is filling up disk space

if you having a hard time trying to audit disk utilization on your Linux system?. If you cannot find what is using the space up with du -sh * . A very useful option that might help you find out what is using up space is ncdu Install it.

$ sudo yum install ncdu  # For CentOS 
$ sudo dnf install ncdu  # For Fedora
$ sudo apt-get install ncdu # For Ubuntu/Debian

Once the package is installed on your OS, simply run:

$ ncdu /dir/

Ncdu also provide a number of options to manipulate files and folders – Navigation, sorting, and even deleting:

  • up, k – used to move the cursor up
  • down, j – used to move the cursor down
  • right, enter, l > – Open selected directory
  • left, <,h  – This opens parent directory
  • n – order by name (press again for descending order)
  • s – Order by file size (press again for descending order)
  • d – Delete selected file or directory
  • g – Show percentage and/or graph
  • – Toggle dirs before files when sorting.
  • – Toggle display of child item counts.
  • – Spawn shell in the current directory.
  • i  – Show information about the selected item
  • r – Refresh/recalculate the current directory.
  • q – Quit ncdu

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.