LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-06-2007, 01:22 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
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.
 
Old 03-06-2007, 06:03 AM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
Old 03-06-2007, 05:06 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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"
 
Old 03-11-2007, 12:49 AM   #4
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-11-2007, 12:59 AM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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.
 
Old 03-11-2007, 01:08 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
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
 
Old 03-11-2007, 10:08 AM   #7
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
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`
 
Old 03-11-2007, 12:54 PM   #8
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 03-13-2007, 03:48 AM   #9
theGuate
LQ Newbie
 
Registered: Mar 2007
Posts: 1

Rep: Reputation: 0
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
 
  


Reply

Tags
awk, bash, scripting


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] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
shell script output to file drum2jc Linux - Software 1 05-30-2006 06:54 PM
sorting a file in shell script shmuel_orgad Programming 11 05-20-2006 08:16 PM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM
File Deletion Script disinformation Linux - Software 3 04-18-2005 07:00 AM


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