LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 06-21-2007, 01:50 PM   #1
Anithen
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Slackware 12, Slackware 12.1, Slackware 12.2
Posts: 105

Rep: Reputation: 15
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
 
Old 06-21-2007, 03:35 PM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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)

Code:
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
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

You can also take a look at some bash scripting guides, like this one:
http://tldp.org/LDP/abs/html/
 
Old 06-21-2007, 04:23 PM   #3
Anithen
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Slackware 12, Slackware 12.1, Slackware 12.2
Posts: 105

Original Poster
Rep: Reputation: 15
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
 
Old 06-21-2007, 04:57 PM   #4
drumz
Member
 
Registered: Apr 2005
Location: Scottsdale, AZ, USA
Distribution: Slackware
Posts: 188

Rep: Reputation: 33
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.
 
Old 06-21-2007, 05:12 PM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
yeah, forgot about that, thanks for reminder. Maybe you can use

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

Last edited by H_TeXMeX_H; 06-21-2007 at 05:13 PM.
 
Old 06-21-2007, 05:41 PM   #6
Anithen
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Slackware 12, Slackware 12.1, Slackware 12.2
Posts: 105

Original Poster
Rep: Reputation: 15
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.
 
Old 06-21-2007, 09:47 PM   #7
Anithen
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Slackware 12, Slackware 12.1, Slackware 12.2
Posts: 105

Original Poster
Rep: Reputation: 15
I just wanted to report back that everything's fine, and the advice worked great.

Thanks!
 
Old 06-21-2007, 10:18 PM   #8
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

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


Reply

Tags
bash, df, du, find, script


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
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 01:21 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
bash script to output only differences in two files czarherr Programming 4 11-08-2004 06:25 PM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 07:54 PM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM


All times are GMT -5. The time now is 12:48 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