Shell script to delete files when disk is full

shell script to check disk space on multiple servers
shell script to check available disk space
linux motion auto delete old files
shell script to watch disk space
shell script for disk utilization
write a shell script to delete all the temporary files
deleting files but disk space is still full linux
script for file system alert

I am writing a small little script to clear space on my linux everyday via CRON if the cache directory grows too large. Since I am really green at bash scripting, I will need a little bit of help from you linux gurus out there.

Here is basically the logic (pseudo-code)

    if ( Drive Space Left < 5GB )
    {
        change directory to '/home/user/lotsa_cache_files/'

        if ( current working directory = '/home/user/lotsa_cache_files/')
        {
            delete files in /home/user/lotsa_cache_files/
        }
    }
Getting drive space left

I plan to get the drive space left from the '/dev/sda5' command. If returns the following value to me for your info :

Filesystem           1K-blocks      Used Available Use% Mounted on<br>
/dev/sda5            225981844 202987200  11330252  95% /

So a little regex might be necessary to get the '11330252' out of the returned value

A little paranoia

The 'if ( current working directory = /home/user/lotsa_cache_files/)' part is just a defensive mechanism for the paranoia within me. I wanna make sure that I am indeed in '/home/user/lotsa_cache_files/' before I proceed with the delete command which is potentially destructive if the current working directory is not present for some reason.

Deleting files

The deletion of files will be done with the command below instead of the usual rm -f:

find . -name "*" -print | xargs rm

This is due to the inherent inability of linux systems to 'rm' a directory if it contains too many files, as I have learnt in the past.

Just another proposal (comments within code):

FILESYSTEM=/dev/sda1 # or whatever filesystem to monitor
CAPACITY=95 # delete if FS is over 95% of usage 
CACHEDIR=/home/user/lotsa_cache_files/

# Proceed if filesystem capacity is over than the value of CAPACITY (using df POSIX syntax)
# using [ instead of [[ for better error handling.
if [ $(df -P $FILESYSTEM | awk '{ gsub("%",""); capacity = $5 }; END { print capacity }') -gt $CAPACITY ]
then
    # lets do some secure removal (if $CACHEDIR is empty or is not a directory find will exit
    # with error which is quite safe for missruns.):
    find "$CACHEDIR" --maxdepth 1 --type f -exec rm -f {} \;
    # remove "maxdepth and type" if you want to do a recursive removal of files and dirs
    find "$CACHEDIR" -exec rm -f {} \;
fi 

Call the script from crontab to do scheduled cleanings

Script that deletes old files to keep disk from filling up, The script will keep on deleting the oldest file present on disk until deleteoldfiles.sh /Volumes/usb 92 DELETE OLD FILES 1.00 Usage of 90%  Folders are created long before the files are creating. This script will delete all files upto that date but will delete the folders too irrespective of dates, thus you loose current files in the folders too, thats should be mentioned here.

I would do it this way:

# get the available space left on the device
size=$(df -k /dev/sda5 | tail -1 | awk '{print $4}')

# check if the available space is smaller than 5GB (5000000kB)
if (($size<5000000)); then
  # find all files under /home/user/lotsa_cache_files and delete them
  find /home/user/lotsa_cache_files -name "*" -delete
fi

How to Automatically Delete /tmp folders in Linux? Automatic Disk , Complete Linux DiskCleanup Script: Diskclean-Linux.sh 68. #!/bin/bash. # Diskclean-Linux.sh - Remove unused files from /tmp directories. The script will keep on deleting the oldest file present on disk until used capacity is below the threshold. So you can tell the script to monitor volume /storage and delete old files if the used capacity is bigger than 95 percent. The script works like this:./deleteoldfiles.sh <mount point> <percentage>

Here's the script I use to delete old files in a directory to free up space...

#!/bin/bash
#
#  prune_dir - prune directory by deleting files if we are low on space
#
DIR=$1
CAPACITY_LIMIT=$2

if [ "$DIR" == "" ]
then
    echo "ERROR: directory not specified"
    exit 1
fi

if ! cd $DIR
then
    echo "ERROR: unable to chdir to directory '$DIR'"
    exit 2
fi

if [ "$CAPACITY_LIMIT" == "" ]
then
    CAPACITY_LIMIT=95   # default limit
fi

CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')

if [ $CAPACITY -gt $CAPACITY_LIMIT ]
then
    #
    # Get list of files, oldest first.
    # Delete the oldest files until
    # we are below the limit. Just
    # delete regular files, ignore directories.
    #
    ls -rt | while read FILE
    do
        if [ -f $FILE ]
        then
            if rm -f $FILE
            then
                echo "Deleted $FILE"

                CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}')

                if [ $CAPACITY -le $CAPACITY_LIMIT ]
                then
                    # we're below the limit, so stop deleting
                    exit
                fi
            fi
        fi
    done
fi

Automatic deletion of old files when disk is full, The images are all roughly the same file size, and are. of files to delete between each free disk space check # Set shell such that empty file  Shell script to watch the disk space. df displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown. Read man page of df if you are new to df command.

To detect the occupation of a filesystem, I use this :

df -k $FILESYSTEM | tail -1 | awk '{print $5}'

which give me the occupation percentage of the filesystem, this way, I do not need to compute it :)

If you use bash, you can use the pushd/popd operation to change directory and be sure to be in.

pushd '/home/user/lotsa_cache_files/'
do the stuff
popd

Deleting oldest files to free space as needed on Linux, Instead of a find -mtime, do an ls -t, for example: DIR=/tmp FREESPACE=​1000000 find $DIR -type f | xargs ls -1rt | while read f ; do if [ `df --output=avail $​DIR | tail  Automatic deletion of old files when disk is full I am using the "motion" software to capture still images to a volume of limited size. The images are all roughly the same file size, and are in one directory.

Unable to remove file using rm: Disk space is full, Hi all, My disk space is 100% full. df -k -> 100% One of my debug files consume huge amount of space and i want to remove the same to s | The UNIX and Linux  It is a very crucial task to monitor disk space usage on any system. If not properly monitored it can end up with poor performance or application crash. In this tutorial, I will show you a simple shell script that checks the used space on all mounted devices and warns if the used space is more than a threshold.

Shell script to delete files when disk is full, I am writing a small little script to clear space on my linux everyday via CRON if the cache directory grows too large. Since I am really green at  Hi, I have the following task to perform using shell script. The user will provide a directory name along with a date. The script will delete all the files in the specified directory that was created | The UNIX and Linux Forums

Shell script to watch the disk space, df displays the amount of disk space available on the file system #!/bin/sh # set -x # Shell script to monitor or watch the disk space # It 2)If the file is FULL ,mail will be triggered to admin team to delete temp and cache files. This post explains how to delete files using Powershell command ‘Remove-Item’. We can delete a file using remove-item command as below. Open powershell prompt and execute the command. We can delete as many files as we want with single remove-item command. We just need to add the file names separated by comma.

Comments
  • "inherent inability of linux systems to 'rm' a directory if it contains too many files" Does that also imply to "rm -rf"?
  • I suggest using the -delete flag instead of -exec rm -f {} \;. Also consider adding -xdev as the first flag to stay in the same filesystem.
  • That's what I was going to recommend: Hard code the directory in the find command and don't use xargs because it can be dangerous.
  • Please check again the code! It seems that it will delete files within the current folder.
  • @Max it does cd $DIR so it won't.