Script free disk space from deleted files

Share this post to your friends !

WriteBash - Why is space not being freed from disk after deleting a file in Linux? Even though the file was successfully deleted, the disk space was not freed. How to free disk space? My case is when a user uploads huge files to the web server and there are a lot of temporary files (I configured in the /tmp directory) that were generated during the upload process. I discovered that the web server’s disk was full and I could not find the file in the /tmp directory that caused it.

After a few searches, I discovered that the files were in the /proc directory. These files were deleted but for some reason  are still open by some application in the system.

script-free-disk-space-from-deleted-files Script free disk space from deleted files
Find files were deleted on /proc

Script free disk space

This script basically looks for files in the / proc directory labeled “deleted” and then deletes that file. In this script I added a function to check the root partition size, if it exceeds the 80% threshold, then I will delete the file.

Read moreScript send email warning ssh login

# Script author: Danie Pham
# Script site:
# Script date: 30-12-2017
# Script ver: 1.0
# Script use to delete files that have been labeled as "deleted" in /proc directory

# Function check the size of root partition /
f_check_free () {
    # Get current size
    CURRENT=`df -Th | grep -w / | awk '{print $5}' | awk -F'%' '{print $1}'`

    # If size >= 80%
    if [[ "$CURRENT" -ge 80 ]]; then
        # Call function f_free_disk

# Function execute free_disk
f_free_disk () {
    # Define a temp file

    # Find all files has label "deleted" in /proc directory and save pathname to temp file
    find /proc/*/fd -ls | grep  '(deleted)' > $FREE_DISK

    # The loop - read temp file and delete each file
    while read -r line
            FILE=`echo $line | awk '{print $11}'`
    done < "$FREE_DISK"

    # Delete temp file after finish
    rm -f $FREE_DISK

# Main function
f_main () {


You can download the script here.

Use script free disk space

After you put this script on your server, I usually put it in a directory that contains common scripts. Such as /opt/scripts or whatever directory you want. You execute the executable for the script as follows:

chmod 700 /opt/scripts/

This script can be done automatically, which means it will automatically check the size of the root partition and perform deleted files filling up your hard drive automatically without you have to check manually.

You set the crontab for the script to execute automatically every 5 minutes.

crontab -l | { cat; echo "# Check root partition and free disk space every 5 minutes"; } | crontab -
crontab -l | { cat; echo "*/5 * * * * /opt/scripts/"; } | crontab -


This script works automatically and it can give you peace of mind that your drive is full of surprises. You will not have to worry that your system crashes because it is full of hard drives.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.