-   Slackware (
-   -   A command or script to rm files based on df output? (

Anithen 06-21-2007 01:50 PM

A command or script to rm files based on df output?
Hello, Everyone.

I'm doing broadcatching, but with usenet instead of bittorrent. Media will be added to a directory continuously. Before I use cron to apply my usenet broadcatching script I would like to figure out what kind of bash script I could create to use `find` to remove files over a certain age based on the output of `df -h`. I am familiar with how to use `find` for deleting based on age, but I only want `find` to come into play if the output of `df -h` is, for instance, 1GB or less. If I can't use `df -h` for this, I would like to use `du -sh /dir/`, and based on its output run the necessary `find` command if the output is, for instance, 100GB or more.

To anyone who can assist in any way, I'm grateful. Even if someone out there has a solution using totaly different commands, I'm thirsty for the knowledge, and will be glad to drink. Thank you very much in advance.


H_TeXMeX_H 06-21-2007 03:35 PM

well, not sure if this is what you want, but for example, here's the output of my 'df -h'


Filesystem            Size  Used Avail Use% Mounted on
/dev/hda1              73G  49G  25G  67% /

In this case, the space available on /dev/hda1 is 25 GB. Te get that value in the shell you can do this: (note, this may not be the best way, but it works)


spaceleft=$(df -h | grep hda1 | awk '{ print $4 }' | sed 's/G//g')
the first pipe to grep gets the line with hda1 on it
the second pipe to awk gets the 4th element (note the $4)
the third and final pipe uses sed to change 'G' to a '' (nothing) ... it deletes G

So, spaceleft is assigned the value

You can then use this in an if statement like this:

if test $spaceleft -lt 1
  find ...
  rm -f

-lt is less than
-le is less than or equal to
-gt is greater than
-ge is greater than or equal to

You can also take a look at some bash scripting guides, like this one:

Anithen 06-21-2007 04:23 PM

H_TeXMeX_H, thanks so much for this awesome solution. I can't wait to leave work and try this. Thanks for the abs link, too. This knowledge is something I will use for many future challenges. Already, because of your help, I am thinking of other projects I've abandoned or handled less efficiently because I couldn't picture an example like yours.


drumz 06-21-2007 04:57 PM

I don't think you want to use the '-h' switch. What if the space left is returned in MB? I would suggest leaving the output in bytes and working with that, so you don't have to worry about units. Just a suggestion.

H_TeXMeX_H 06-21-2007 05:12 PM

yeah, forgot about that, thanks for reminder. Maybe you can use


df -B G
that way it always displays units in G or use M for megabytes and adjust things accordingly.

Anithen 06-21-2007 05:41 PM

Thanks, Drumz and H_TeXMeX_H. Come to think of it, I'd much rather have the output be in MB, so I'll definitely be using this advice.

Anithen 06-21-2007 09:47 PM

I just wanted to report back that everything's fine, and the advice worked great.


H_TeXMeX_H 06-21-2007 10:18 PM

Great. Just remember to keep thinking about it, and try to consider worst case scenarios ... especially for a script that 'rm's anything.

All times are GMT -5. The time now is 02:25 PM.