Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4591852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:27:18+00:00 2026-05-21T22:27:18+00:00

I am writing a small little script to clear space on my linux everyday

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:27:18+00:00Added an answer on May 21, 2026 at 10:27 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small script to see if certain ports on certain devices are
I'm writing a little Java client side app which uploads things using http streams
I'm writing a Windows Phone 7 app that needs to be location aware. Specifically
I' developing a small android maze game and I'm experiencing a strange effect which
I am currently writing stack that is being implemented with a linked list. I
I keep hearing that global variables should never be used, but I have a
I'm trying to create a macro that produces in-cell graphing as seen over at
I don't really have a real world problem, yet, but I'm trying to learn
After months of frustration and of time spent in inserting needles in voodoo dolls
This one liner... Console.println(io.Source.fromFile(names.txt).getLines.mkString.split(,).map{x:String => x.slice(1, x.length -1)}.sortBy { x => x}.zipWithIndex.map{t =>{ (t._2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.