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. Naota |
well, not sure if this is what you want, but for example, here's the output of my 'df -h'
Code:
Filesystem Size Used Avail Use% Mounted on Code:
spaceleft=$(df -h | grep hda1 | awk '{ print $4 }' | sed 's/G//g') 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 Code:
25 Code:
if test $spaceleft -lt 1 -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: http://tldp.org/LDP/abs/html/ |
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.
Peace, Naota |
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.
|
yeah, forgot about that, thanks for reminder. Maybe you can use
Code:
df -B G |
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.
|
I just wanted to report back that everything's fine, and the advice worked great.
Thanks! |
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 08:46 PM. |