LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   shell script on file deletion (http://www.linuxquestions.org/questions/programming-9/shell-script-on-file-deletion-534961/)

noir911 03-06-2007 01:22 AM

shell script on file deletion
 
I'm trying to write a script which will delete files more than 14 days old and will send an email out saying which files been deleted and the total size deleted. I'm stuck at getting the total size.

here's my script:

Code:


#!/bin/bash

RECP="me@domain.com"

DAYS=14

DIR="/home/backup01 /home/backup02 /home/backup03 /home/backup04 /home/backup05"

FIND=`find $DIR -type f -mtime +$DAYS -exec du -s {} \; | awk '{ print $1 }'`

echo "Total size: $FIND | bc"

Any help would be much appreciated. Thanks.

jlinkels 03-06-2007 06:03 AM

I believe it will work if you replace the awk statement with this one:

awk 'BEGIN {sum=0} {sum += $1}END {print sum}'

You don't need the $FIND variable and the bc.

Not sure if you can execute the rm {} in the same find statement, but you could try though.

jlinkels

cfaj 03-06-2007 05:06 PM

Quote:

Originally Posted by noir911
I'm trying to write a script which will delete files more than 14 days old and will send an email out saying which files been deleted and the total size deleted. I'm stuck at getting the total size.

here's my script:

Code:


#!/bin/bash

RECP="me@domain.com"

DAYS=14

DIR="/home/backup01 /home/backup02 /home/backup03 /home/backup04 /home/backup05"

FIND=`find $DIR -type f -mtime +$DAYS -exec du -s {} \; | awk '{ print $1 }'`

echo "Total size: $FIND | bc"

Any help would be much appreciated. Thanks.

With GNU find (as on Linux, for example), you could use:
Code:

FIND=`find $DIR -type f -mtime +$DAYS -printf "%s\n" | awk '{ s += $1 } END { print s }'`
echo "Total size: $FIND"

To remove the files at the same time (untested):

Code:

FIND=`find $DIR -type f -mtime +$DAYS -printf "%s\n" -exec rm {} + | awk '{ s += $1 } END { print s }'`
echo "Total size: $FIND"


noir911 03-11-2007 12:49 AM

Thanks for your response. I tried

Code:


FIND=`find $DIR -type f -mtime +$DAYS -printf "%s\n" | awk '{ s += 1 } END { print s }'`
echo "Total size: $FIND"

and the result comes to

Quote:


Total size: 7.17187e+11

And then I recalculated this figure which gives me: 30.4951639.

Any further help would be much appreciated.

cfaj 03-11-2007 12:59 AM

Quote:

Originally Posted by noir911
Thanks for your response. I tried

Code:


FIND=`find $DIR -type f -mtime +$DAYS -printf "%s\n" | awk '{ s += 1 } END { print s }'`
echo "Total size: $FIND"

and the result comes to



And then I recalculated this figure which gives me: 30.4951639.

Any further help would be much appreciated.

Code:

FIND=`find $DIR -type f -mtime +$DAYS -printf "%s\n" |
        awk '{ s += $1 } END { printf "%d\n", s }'`

If you think the arithmetic is wrong, print the lists of values and compare them.

kshkid 03-11-2007 01:08 AM

Quote:

DIR="/home/backup01 /home/backup02 /home/backup03 /home/backup04 /home/backup05"

FIND=`find $DIR -type f -mtime +$DAYS -exec du -s {} \; | awk '{ print $1 }'`

I wonder how would this work,

ultimately find would then search for directory named,
Code:

/home/backup01 /home/backup02 /home/backup03 /home/backup04 /home/backup05

Individual directories need to be given to find, and the size calculated as a cumulative answer :)

ygloo 03-11-2007 10:08 AM

hello,
"df" has option to show total sum of disk use:
-c

FIND=`find $DIR -type f -mtime +$DAYS -print0 | xargs -0 du -ch | tail -1`

indienick 03-11-2007 12:54 PM

Why not temporarily move all the files to be deleted into an empty temp directory, sum the output of "ls -l" (using awk or sed - I can't remember which one - to isolate the file sizes (in bytes) in the fifth (5th) column) and print that to the screen.

Yeah, it causes the unnecessary dependency of a directory creation and deletion, but, to me, it makes the most sense, and is the easiest.

EDIT: I would post a code example, but I've got little to no experience with BASH, AWK and SED scripting.

theGuate 03-13-2007 03:48 AM

Hi,
What I do for deletion of old files..
Option find -could be ctime or mtime
-ctime File's status was last changed n*24 hours ago
-mtime File's data was last modified n*24 hours ago.
example:

find . -mtime +60 -exec ls -lrt {} \; 2>/dev/null
( only to verify what I'm going to delete )
and then
find . -mtime +60 -exec rm {} \; 2>/dev/null
here I delete the file that last modified 60day ago, from 60days to today are OK


All times are GMT -5. The time now is 07:13 PM.