A command or script to rm files based on df output?
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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
/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)
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
Code:
25
You can then use this in an if statement like this:
Code:
if test $spaceleft -lt 1
then
find ...
rm -f
fi
-lt is less than
-le is less than or equal to
-gt is greater than
-ge is greater than or equal to
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.