|

Clearing space through the Linux command line

Warning the following guide will teach you how to completely delete files from your server. This needs to be run as root so be EXTRA careful before running any command and double check the files that you delete, these will be non-recoverable.

If you are working on a small VPS it is not uncommon to accidentally run out of disk space. This guide will show you some commands you can run to help find extra large files and then delete them.

Once you have logged in through ssh you will want to check your disk usage. The most common command to use is “df”. Here is an example:

df -h

##note the ” -h ” tells the command to use a human readable format

The read out will look something like this.

Filesystem Size Used Avail Use% Mounted on
/dev/vda1 45G 43G 3.5G 98% /
devtmpfs 1022M 0 1022M 0% /dev
tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs 1.1G 8.6M 1022M 1% /run
tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
/dev/loop0 1.3G 2.3M 1.2G 1% /tmp
tmpfs 207M 0 207M 0% /run/user/0

I am looking for the disk mounted as ” / ” this will be the primary partition. In this example my virtualization sets /dev/vda1 as the primary drive, it is on the first line and is at 98% full.

Now to find what is using up all my space.

The following command will search your server for any files that are larger than 100mbs and list them out for you.

find / -size +100000k -exec du -sh {} \;

The search can take several minutes so you will need to be patient, once it has completed you will see a list of files followed by the regular command prompt. It will look something like this

102M /usr/lib/locale/locale-archive
421M /usr/tmpDSK
352M /var/cache/yum/x86_64/7/EA4/gen/filelists_db.sqlite
184M /var/cache/yum/x86_64/7/EA4/gen/primary_db.sqlite
112M /var/lib/rpm/Packages
210M /var/log/munin/munin-node.log
953M /var/log/munin/munin-update.log
199M /var/log/chkservd.log
819M /root/tmp/Cpanel_Form_file.upload.T8FrvSXs
0 /proc/kcore
find: ‘/proc/7187/task/7187/fd/6’: No such file or directory
find: ‘/proc/7187/task/7187/fdinfo/6’: No such file or directory
find: ‘/proc/7187/fd/6’: No such file or directory
find: ‘/proc/7187/fdinfo/6’: No such file or directory
881M /backup/2018-01-31/accounts/username.tar.gz
157M /backup/2018-01-31/system/dirs/_var_cpanel.tar.gz
877M /backup/2018-01-27/accounts/username.tar.gz
914M /backup/weekly/2018-01-14/accounts/username.tar.gz

Looking at that list I see that I have three ~900Mb backups for the account “username” . I no longer need the weekly one that was taken on the 14th of January so I can delete it. To delete a file use the command “rm”

In this example it is

rm -R  /backup/weekly/2018-01-14/accounts/username.tar.gz

##note the -R in the above command is for recursive, and is usually used for directories, adding -R will delete any files or folders that are in that directory.

You will be prompted to confirm the file deletion. Make sure you have the full and correct file that you want to delete, once a file has been deleted there is no way to restore it.

I also noted in the above examples that I have some log files that are getting a little big, I already checked the log files and know that I can delete them as well. Since many files are in use while your server is running you can’t directly delete them with the “rm” command, instead I will simply rewrite the file with empty space, using “echo” (a write-to command)

echo ” ” > /var/log/munin/munin-update.log

This command replaces all the data in the log file /var/log/munin/munin-update.log  with a single space. Like “rm” once you have done this it can’t be undone so check the logs first to make sure you know that the info isn’t needed.

Now that I have removed some of these larger files I can run “df -h” again and see the results.

Filesystem Size Used Avail Use% Mounted on
/dev/vda1 45G 38G 4.3G 90% /
devtmpfs 1022M 0 1022M 0% /dev
tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs 1.1G 8.6M 1022M 1% /run
tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
/dev/loop0 1.3G 2.3M 1.2G 1% /tmp
tmpfs 207M 0 207M 0% /run/user/0

Similar Posts

Leave a Reply

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