How to Manage your Hard Drive Space in cPanel using command line


If you own a VPS account with root access and you choose to manage it yourself, you must learn or have some understanding of how to use command line.

In this tutorial, we provide simple common steps you can follow to manage your server's hard drive space the right way.

We will be using the df and du commands to generate reports of your disk space usage from within the bash shell used on Linux, macOS, and many other Unix-like operating systems. These commands let you easily identify what’s using up your system’s storage.

Viewing the Total, Available and Used Disk Space.

To find out the available and used disk space, use df (disk filesystems, sometimes called disk free). You may use PuTTy if you have it installed in your machine or you can simply login into your cPanel or WHM and search for "terminal" from the top left conner and click on it. You will see an interface from where you can use to type the suggested command lines.

So typing df you will get the following result:

[root@devjungle ~]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
devtmpfs          930732        0    930732   0% /dev
tmpfs             941092        0    941092   0% /dev/shm
tmpfs             941092    99844    841248  11% /run
tmpfs             941092        0    941092   0% /sys/fs/cgroup
/dev/vda1       40184928 20517044  17606904  54% /
/dev/loop0       1008352     1472    953992   1% /tmp
tmpfs             188220        0    188220   0% /run/user/0

Examine disk space usage

To examine your system's disk space usage, run the command df -h and you will get the following result:

[root@devjungle ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        909M     0  909M   0% /dev
tmpfs           920M     0  920M   0% /dev/shm
tmpfs           920M   98M  822M  11% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
/dev/vda1        39G   20G   17G  54% /
/dev/loop0      985M  1.5M  932M   1% /tmp
tmpfs           184M     0  184M   0% /run/user/0

To get the results with totals

[root@devjungle ~]# df -h --total
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        909M     0  909M   0% /dev
tmpfs           920M     0  920M   0% /dev/shm
tmpfs           920M   98M  822M  11% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
/dev/vda1        39G   20G   17G  54% / 
/dev/loop0      985M  1.5M  932M   1% /tmp
tmpfs           184M     0  184M   0% /run/user/0
total            44G   20G   22G  48% -

The above output indicates that the system's main storage device (/dev/vda1) in the root (/) directory uses 54% of its available disk space and contains 17 GB of free space.

Examine which directory is consuming most space

To determine which files and directories consume most of your hard drive space, run the du (disk usage) command. The output will resemble the following example:

[root@devjungle ~]# du
636     ./tmp/mysql_backup_28_08_2020/performance_schema
2508    ./tmp/mysql_backup_28_08_2020/db
680     ./tmp/mysql_backup_28_08_2020/sys
45912   ./tmp/mysql_backup_28_08_2020/db-name1
19104   ./tmp/mysql_backup_28_08_2020/mysql
8       ./tmp/mysql_backup_28_08_2020/db-name2
10556   ./tmp/mysql_backup_28_08_2020/db-name3
83072   ./tmp/mysql_backup_28_08_2020/db-name4
76012   ./tmp/mysql_backup_28_08_2020/bd-name5
427008  ./tmp/mysql_backup_28_08_2020
427116  ./tmp
10548   ./.spamassassin/sa-compile.cache
10552   ./.spamassassin
4       ./.ssh
12      ./.HttpRequest
4       ./dbs

To get the result in Mega Bites run du -BM command

[root@devjungle ~]# du -BM
1M      ./tmp/mysql_backup_28_08_2020/performance_schema
3M      ./tmp/mysql_backup_28_08_2020/db
1M      ./tmp/mysql_backup_28_08_2020/sys
45M     ./tmp/mysql_backup_28_08_2020/db-name1
19M     ./tmp/mysql_backup_28_08_2020/mysql
1M      ./tmp/mysql_backup_28_08_2020/db-name2
11M     ./tmp/mysql_backup_28_08_2020/db-name3
82M     ./tmp/mysql_backup_28_08_2020/db-name4
75M     ./tmp/mysql_backup_28_08_2020/bd-name5
417M    ./tmp/mysql_backup_28_08_2020
418M    ./tmp
11M     ./.spamassassin/sa-compile.cache
11M     ./.spamassassin
1M      ./.ssh
1M      ./.HttpRequest
1M      ./dbs
Notes:
  • According to cPanel people, they recommend that you only run this command in off-peak hours to prevent additional load on your server.
  • The numbers in the left column represent the sizes of the files and directories that the working directory contains, in human-readable format.

In the above example, the /tmp directory, which mostly contains the system's cPanel account backup data, uses the most disk space (418 MB).

Examine the desired directory

To examine the desired directory, run du -sh /tmp/* command.
[root@devjungle ~]# du -sh /tmp/*
12K     /tmp/17521._USR_LOCAL_CPANEL_SCRIPTS_UPDATENOW__.d1fdee7a.tmp
4.0K    /tmp/17521._USR_LOCAL_CPANEL_SCRIPTS_UPDATENOW__.e5abe144.tmp
12K     /tmp/22032._USR_LOCAL_CPANEL_SCRIPTS_UPDATENOW__.43f92c6b.tmp
4.0K    /tmp/22032._USR_LOCAL_CPANEL_SCRIPTS_UPDATENOW__.4981efd5.tmp
12K     /tmp/25371._USR_LOCAL_CPANEL_SCRIPTS_UPDATENOW__.97fcc106.tmp
4.0K    /tmp/25371._USR_LOCAL_CPANEL_SCRIPTS_UPDATENOW__.f509d822.tmp
16K     /tmp/lost+found
0       /tmp/mysql.sock
4.0K    /tmp/yum_save_tx.2020-09-28.05-07.eB_e_K.yumtx
4.0K    /tmp/yum_save_tx.2020-09-29.05-08.VnvEBg.yumtx
4.0K    /tmp/yum_save_tx.2020-09-30.07-29.xljaKc.yumtx
4.0K    /tmp/yum_save_tx.2020-10-01.07-42.sxcQPO.yumtx
4.0K    /tmp/yum_save_tx.2020-10-02.06-00.BqbADW.yumtx
4.0K    /tmp/yum_save_tx.2020-10-03.08-48.ASszXC.yumtx
4.0K    /tmp/yum_save_tx.2020-10-04.07-05.634vWu.yumtx
4.0K    /tmp/yum_save_tx.2020-10-05.07-04.f76L5G.yumtx
4.0K    /tmp/yum_save_tx.2020-10-06.08-34.UOJ7fC.yumtx
4.0K    /tmp/yum_save_tx.2020-10-07.09-09.cHgHJ9.yumtx
Run df -h --total /dev /run command and obtain a report with totals:
[root@devjungle ~]# df -h --total /dev /run
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        909M     0  909M   0% /dev
tmpfs           920M   98M  822M  11% /run
total           1.8G   98M  1.7G   6% -

Just like df, du command has a human-readable option, -h, which uses a range of block sizes according to the size of each directory. And if you were to run du -h or du -h --totalcommand, you would get the following results:

[root@devjungle ~]# du -h 
1M      ./tmp/mysql_backup_28_08_2020/performance_schema
3M      ./tmp/mysql_backup_28_08_2020/db
1M      ./tmp/mysql_backup_28_08_2020/sys
45M     ./tmp/mysql_backup_28_08_2020/db-name1
19M     ./tmp/mysql_backup_28_08_2020/mysql
1M      ./tmp/mysql_backup_28_08_2020/db-name2
11M     ./tmp/mysql_backup_28_08_2020/db-name3
82M     ./tmp/mysql_backup_28_08_2020/db-name4
75M     ./tmp/mysql_backup_28_08_2020/bd-name5
417M    ./tmp/mysql_backup_28_08_2020
11M     ./.spamassassin/sa-compile.cache
11M     ./.spamassassin
4.0K    ./.ssh
12K     ./.HttpRequest
4.0K    ./dbs

Resulting report with total:

[root@devjungle ~]# du -h --total
1M      ./tmp/mysql_backup_28_08_2020/performance_schema
3M      ./tmp/mysql_backup_28_08_2020/db
1M      ./tmp/mysql_backup_28_08_2020/sys
45M     ./tmp/mysql_backup_28_08_2020/db-name1
19M     ./tmp/mysql_backup_28_08_2020/mysql
1M      ./tmp/mysql_backup_28_08_2020/db-name2
11M     ./tmp/mysql_backup_28_08_2020/db-name3
82M     ./tmp/mysql_backup_28_08_2020/db-name4
75M     ./tmp/mysql_backup_28_08_2020/bd-name5
417M    ./tmp/mysql_backup_28_08_2020
418M    ./tmp
11M     ./.spamassassin/sa-compile.cache
11M     ./.spamassassin
4.0K    ./.ssh
12K     ./.HttpRequest
4.0K    ./dbs
512M    .
512M    total

Adding -s * to du -h for summarization

The -s * (summarize) option gives a total for each directory without displaying the sub-directories within each directory. The following command du -h -s * asks du to return information in summary format, in human readable numbers, for all directories (*) below the current working directory.

[root@devjungle ~]# du -h -s *
12K     cpanel3-skel
4.0K    dbs
8.0K    install.sh
32K     latest
4.0K    legacy_ea3_distiller_files-1574305518.tar.gz
376K    litecube-f
0       log-update.log
0       munin-update.log
28K     perl5
32M     roundcubemail-plugins-kolab
6.2M    roundcube_plus_skin_litecube-f.tar.gz
29M     skin_tmp
418M    tmp

Deleting unwanted files

To delete unwanted files, run rm -f filename command, where filename represents the file to remove:

Delete cPanel File Manager temp files

File Manager always creates a temp file that may or may not get removed based on upload. You can remove all temp files by using the rm -fv /home/*/tmp/Cpanel_* command.

Warning:
Exercise caution when you run the rm utility. You will not be able to recover files or directories that you delete.

Clean up Yum files

Every time you update yum it leaves package cache files on the server. You can clean up all unneeded yum files by running yum clean all commands.

Retaining log entries

Log files often use a large amount of disk space. To retain the system’s most recent log file entries when you remove excess log files, perform the following steps:

  1. If you have not already located the specific files to remove from this directory, run the du command.
  2. Examine the command’s output to determine the files to delete.
  3. For each file, run the following commands, where filename represents the file’s name.
  4. tail -5000 filename > filename.new
    mv filename.new filename
    sync
    
  5. To release the file lock, restart the service that created the file.

Warning
If you have a selfmanaged VPS and you are not comfortable using command line to manage your cPanel / WHM server, do not use it! Please ask for help from your hosting provider or hire a developer to assist you in doing so.