Delete files older than "x" if directory size is greater than "y"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete files older than "x" if directory size is greater than "y"
# 1  
Old 02-10-2010
Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y"

Code:
#!/bin/bash

du -hs $1
while read SIZE ENTRY
do

if [ ${SIZE} -gt 200 ];
then

find $1 -mtime +$2 -exec rm -f {} \;
echo "Files older than $2 days deleted"
else
echo "free Space available"
fi
done

I am not sure if everthing is correct it took me some tome to write it, and the only thing is to trigger it only if directory size is greater than.
pls Help
# 2  
Old 02-10-2010
You have to crontab the script to check direcotry size after some interval.
# 3  
Old 02-10-2010
Im no bash expert... (so use with caution as I cant test for you... based on my sh/ksh knowledge
Code:
#!/bin/bash
# you could add some comments on how it should work...
# Its always handy to give some more information and
# with a program that rm, test all parameters are set 
# and are coherent or come out with a short help/syntax
# to display
du -hs $1          # I suppose you mean $1=the input directory?
SIZE=$(du -sk $1)
# where do you get the size form?
while read SIZE ENTRY   # either you echo "enter size..."
                                 # and the read SIZE
do
                                 # or you accpet SIZE as 2nd argument $2
if [ ${SIZE} -gt 200 ];   # which gives you 
                         # if [ $(SIZE) -gt "$2" ] 
then

find $1 -mtime +$2 -exec rm -f {} \;
echo "Files older than $2 days deleted"
else
echo "free Space available"
fi
done



---------- Post updated at 18:17 ---------- Previous update was at 18:14 ----------

Your $2 would become $3...

---------- Post updated at 18:22 ---------- Previous update was at 18:17 ----------

About testing your arguments, this is a start:
Code:
#Test  params
if [ $# -eq 0 ];
then
        echo " params missing!"
        echo "Usage : $0 directory size (in KB)"
        exit 1
else
   if  [ $# -gt 3 ];
   then
        echo " too many arguments"
        echo "Usage : $0 directory size (in KB)  days"
        exit 1
   else
      blah blah
      ...
  fi
fi


Last edited by vbe; 02-10-2010 at 01:28 PM..
# 4  
Old 02-10-2010
James what do you get when you type "du -sh" as an output?

you can use "-size" option too when looking for a file as well as "-mtime".

And VBE made all confusing points clear which i stuck with.
# 5  
Old 02-11-2010
"du -sh $1" gives the size of the directory. My idea was to = this size with an argument.

@Krabu

Crontab was the next step.
@Vbe
sry that there were no comments in the scrypt just getting started, i will try to comment more.

and yes I need third Argument $3 you right.

---------- Post updated 02-11-10 at 07:41 AM ---------- Previous update was 02-10-10 at 01:07 PM ----------

Code:
#!/bin/bash

# shows size of the directory $1 in kb (-ks), can be cahnged into -mb for #example for Megabytes

size=$(du -ks $1 | awk '{print $1}')

#Compares the Size with your max input size $2

if [ $size -gt $2 ];
then

#if greater deletes all files older than $3 days in directory $1 and it´s #subdirectories

find $1 -mtime +$3 -exec rm -f {} \;
echo "Files older than $3 days deleted"
else
echo "free Space available"
fi


works now for me. thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. UNIX for Dummies Questions & Answers

look for file size greater than "0" of specific pattern and move those to another directory

Hi , i have some files of specific pattern ...i need to look for files which are having size greater than zero and move those files to another directory.. Ex... abc_0702, abc_0709, abc_782 abc_1234 ...etc need to find out which is having the size >0 and move those to target directory..... (7 Replies)
Discussion started by: dssyadav
7 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Script for delete tmp files older than 15 days and owned by "xxx" id

Hi All , I want to delete files from /tmp directory created by "xxxx" id. because i got the list says more than 60 thousand files were created by "xxxx" id since 2002. The /tmp directory has lot of files created by different user ids like root,system etc.. But, i need a script to... (2 Replies)
Discussion started by: vparunkumar
2 Replies

8. Shell Programming and Scripting

Unix commands delete all files starting with "X" except "X" itself. HELP!!!!?

im a new student in programming and im stuck on this question so please please HELP ME. thanks. the question is this: enter a command to delete all files that have filenames starting with labtest, except labtest itself (delete all files startign with 'labtest' followed by one or more... (2 Replies)
Discussion started by: soccerball
2 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question