DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 14th July 2008
disco disco is offline
New User
 
Join Date: Jul 2008
Posts: 2
Thanked 0 Times in 0 Posts
Default Create a script to rmove oldest files

ok, I have never written a shell script before. What I need to do is remove the oldest files from a known directory when the hard disk is above a certain size, so that the hard disk never becomes full and the oldest data removed.

Any help would be grateful and even better if someone can code me the script
Reply With Quote
  #2   (View Single Post)  
Old 14th July 2008
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 3,608
Thanked 214 Times in 189 Posts
Default

http://www.daemonforums.org/showthread.php?t=651
Reply With Quote
  #3   (View Single Post)  
Old 14th July 2008
disco disco is offline
New User
 
Join Date: Jul 2008
Posts: 2
Thanked 0 Times in 0 Posts
Default

thanks for the link, but this seems to only remove files over n days. What I need to do is when the hard disk is greater the n percent full, remove the oldest files from the directory until the disk is lower than n percent full. Basically a FIFO.
Reply With Quote
  #4   (View Single Post)  
Old 14th July 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,873
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by disco View Post
thanks for the link, but this seems to only remove files over n days. What I need to do is when the hard disk is greater the n percent full, remove the oldest files from the directory until the disk is lower than n percent full.
Here are some hints you can use to help yourself:
  • The following tutorial is a good introduction to shell programming:

    http://steve-parker.org/sh/sh.shtml
  • To determine whether a partition is more than some pre-determined percentage full, look the output of df(1) & think about how to isolate a value in a particular column (HINT: with awk(1), this is easy...).
  • As for how to determine how old files are, look at the manpage for ls(1).
The link provided by jggimi will help you ferret out the details needed to set up cron(8) which can automatically call your script on whatever periodic basis you want.

Last edited by ocicat; 14th July 2008 at 05:44 PM.
Reply With Quote
  #5   (View Single Post)  
Old 14th July 2008
ai-danno's Avatar
ai-danno ai-danno is offline
Spam Deminer
 
Join Date: May 2008
Location: Boca Raton, Florida
Posts: 284
Thanked 35 Times in 31 Posts
Default

Quote:
Originally Posted by disco View Post
Any help would be grateful and even better if someone can code me the script
Welcome to daemonforums! I see that this is your first posting here.

In the link provided by jggimi is a post that relates to finding files by age (by BSDFan666)... you may want to take another look at that.
__________________
Network Firefighter
Reply With Quote
  #6   (View Single Post)  
Old 14th July 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

because AWK is it's own language as well as a program (awk), I'll add to ocicats post by noting the easiest way to get a specific column out of 'df' output with awk.

Code:
df | awk '{ print $1 }'
which will print the first column to standard output, $2 the second column, $3 the third column and so on.


Accessing the column(s) a line at a time is also easy as pie but I'll let you figure that out along with more useful things ;-)
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
matts: a sh script to mail files as attachments from the command line J65nko Guides 6 12th October 2009 06:24 PM
Cleaning Portsnap files in /var/db/portsnap/files bram85 FreeBSD Ports and Packages 2 5th October 2009 09:54 AM
finding files by uid carpman FreeBSD General 3 5th February 2009 07:51 PM
Tried to create new partition using sysinstall but change is not permanent disappearedng FreeBSD General 7 6th July 2008 10:00 PM
Source code of coreutils to create Incident Response Disk audio FreeBSD General 6 5th July 2008 11:00 AM


All times are GMT. The time now is 07:14 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick