LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-05-2009, 04:10 AM   #1
nekawa
LQ Newbie
 
Registered: May 2009
Posts: 16

Rep: Reputation: 1
bash script to delete files / folders based on date and freespace


please comment let me know what u think one of the first scripts I've written while I was bored..
basically u can change the variables, it will delete the oldest thing in the folder and u can set how many MB until it should start deleting, quite basic, but quite useful if u have an ftp/server that gathers things and the HD gets full often

Like i said it's pretty basic, can be forked in to the background by using "command &" as normal

::

# ! /bin/sh
# ! WARNING will delete files/folders until room is free

# set freespace in MB Megabyte 1000x1000 per 'df'

FS=200

# set directory for oldest files to be deleted from to be deleted
# no ending slash here!

DR1=/home/awaken/bin/new

# check diskspace
function freemb () {

FMB=`df . -B MB | tail -1 | awk {'print $4'} | grep -oE [0-9]*`
}
freemb

# check oldest file and/or directory
function oldest () {

OLDFLE=`ls -t --color=no "$DR1" | tail -1`
}
oldest

function theloop () {

freemb
if [ "$FMB" -lt "$FS" ]
then
oldest
rm -rf -- "$DR1/$OLDFLE"
sleep 60
theloop
else
sleep 60
theloop
fi
}
theloop
 
Old 06-05-2009, 05:00 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Hello and welcome to LQ.

LQ uses vBB so you can use BB tags to make posted code easier readable:
Code:
#!/bin/sh --
#Purpose: delete files/folders until space is freed.

# Unset if no debug is necessary
set -Cvxe 

# Whoami?
progn="$0"; progn="${progn//*\//}"

[ $# -ne 0 ] && { echo "${progn}: delete files/folders until space is freed. No args allowed, exiting." > /dev/stderr; exit 127; }

# Set freespace in MB Megabyte 1000x1000 per 'df'.
MAXFREESPACE=20000

# Set directory for oldest files to be deleted from to be deleted no ending slash here!
DELROOT=/home/awaken/bin/new

[ -d "${DELROOT}" ] || { logger "${progn}: no DELROOT, exiting; exit 127; }

# Check diskspace.
freemb() { CURRSPACE=`df . -B MB | awk '/^\// {print $4}'`; FREEDSPACE=${FREEDSPACE//[^0-9]/}; }

# Check oldest file and/or directory.
oldest() { OLDESTFILE=`ls -t --color=no "${DELROOT}" | tail -1`; }

main() {
 freemb
 if [ $CURRSPACE -lt $MAXFREESPACE ]; then
  oldest
  echo "rm \(-rf\) -- /${OLDESTFILE}" \
  || { logger "${progn}: in (${FUNCNAME}): Unable to delete "${DELROOT}/${OLDESTFILE}", exiting."; exit 1; }."
  sleep 2
  main
 else
  sleep 60
  main
 fi
}

main

exit 0
What I changed was:
- adding an exit code. Not a hard requirement, but often other apps will (or may in the future) depend on the exit code of a script. Therefore it's a good custom to exit the script in a friendly way.
- set "debugging" on. Often comes in handy to see values expanded.
- use descriptive variable names.
- use UPPERCASE variable names. Not a hard requirement, but makes it easier to read.
- use some minor checks to exit if not configured or on execution error.
- use proper quoting as in integers vs strings.
- use "echo" on "fatal" commands will keep things safe while executing them.

If you're going to expand this, you could use:
- a "trap" to catch signals like CTRL+C and such.
- "getopts" to configure DELROOT from the CLI.
- a more fine-grained way of finding files, say "find" with a printf of epoch plus filename?
And maybe define "free space" as a percentage: "df" output already provides it.

Practically I would never run this script because it does not discriminate between files and dirs. Besides, if a certain dir is deleted the result freeing up more of the occupied disk space than you might expect.

The script had a few problems but other than that it looks OK I think.

Last edited by unSpawn; 06-05-2009 at 05:01 AM.
 
Old 06-07-2009, 05:09 AM   #3
nekawa
LQ Newbie
 
Registered: May 2009
Posts: 16

Original Poster
Rep: Reputation: 1
thanx for the nice info..
..most people wouldn't want this script..
..obviously not something u'd want to run as root from /


purpose of this script (for me at least) is for a download folder..
..like an ftp that contains several versions of a program where there are folders and files like so:

.
..
DIR1
DIR2
DIR3
DIR4
FILE1.TZ
FILE2.TZ
FILE3.TZ
FILE4.TZ

dir1/file1 normally have roughly the same date/time, in this particular case, i need either the whole file or the whole directory removed, as most of u know, 'half' of a source code would be pretty useless if it were to remove half a directory, and not the rest..
 
Old 06-08-2009, 10:52 AM   #4
1stdead
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
Smile

Hi Nekawa.
I was searching for such a script, haven't found one working like i want it too. So it would be quite nice if you made some improvements for this script.

- Run from not root in crontab
- Multiple dirs like
- dir1
- dir1
- dir2
- dir2
- dir1
- file1
- specialname
- dir3
etc.
and it has to find the oldest global file and then delete it and so on.
make some dirs/files not affected by this script


if you need something to get inspired from:
corvette.servebeer.com/glftpd/free_space_v1.2.2.html
grandis.nu/glftpd/modules.php?name=Downloads (search for space)

Thx
 
Old 06-08-2009, 11:02 AM   #5
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Quote:
Originally Posted by 1stdead View Post
Hi Nekawa.
I was searching for such a script, haven't found one working like i want it too. So it would be quite nice if you made some improvements for this script.
Write your own; if you're looking for one, don't ask someone to rewrite theirs to suit your requirements
 
Old 06-08-2009, 09:00 PM   #6
nekawa
LQ Newbie
 
Registered: May 2009
Posts: 16

Original Poster
Rep: Reputation: 1
while I do appreciate the appreciation, If u notice I am quite a newbie myself. I can manage to write a script that works for my purpose, however I wouldn't try scripting for someone else..
but it sounds like u could do what your trying to do pretty easy
the 'ls' command probably isn't going to work for u
maybe this will help you
Code:
find /bin /etc -maxdepth 1 -name "*.*" -print  | sed 's!^.*/!!'
this will list files in multiple directories (only files not directories) u can go from there, maybe u can find a way for 'find' to sort by date for you then it would be real easy I know u can tell find to list directories also if that's what u want..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
Delete files based on current date -1 Winanjaya Linux - Newbie 3 06-23-2008 04:38 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
Delete old files and folders Script? AsteX Linux - General 4 11-11-2004 06:26 PM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 07:54 PM


All times are GMT -5. The time now is 03:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration