Script for deleting files and directories when the file system reaches the threshold


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for deleting files and directories when the file system reaches the threshold
# 1  
Old 01-12-2012
Script for deleting files and directories when the file system reaches the threshold

Hi

Can someone assist in writing a script.
I have a filesystem named /sybase in my aix lpar.
When this filesystem becomes 94% full all the files and directories under /sybase/logs should be deleted immediately. Smilie
# 2  
Old 01-12-2012
This should do the trick:

Code:
#!/bin/bash
use=`df | grep /sybase | awk '{print $(NF-1)}'`   #get the percentage use 
use=${use%*%}  #get rid of the trailing '%'
if [ $use -gt 94 ] ; then
    rm -r /sybase/logs/*
fi

This script should be run by cron as often, as you think is necessary (every hour, day?). If you want it to run every hour, just copy it into /etc/cron.hourly, if that exists on your system. If not, then make an entry in the crontab. Make sure the script is executable.
This User Gave Thanks to mirni For This Post:
# 3  
Old 01-12-2012
one liner ..
Code:
$ df -h /sybase | nawk '/%/{sub(/%/,"");if($5>=94)print "rm -r /sybase/logs/*" }' | sh

This User Gave Thanks to jayan_jay For This Post:
# 4  
Old 01-16-2012
Hi Jayan Jay
The command you have given works fine. Thank you so much
@ Mirni : I saved the content you gave as script.ksh
When I execute it gives the below error:
Code:
cbspssit01:/ # ksh /scripts/script.ksh
/scripts/script.ksh[4]: 1%: more tokens expected

# 5  
Old 01-17-2012
Quote:
Originally Posted by newtoaixos
Hi Jayan Jay
The command you have given works fine.
Actually, jayan's one liner may not work correctly on some systems -- it may clear the directory tree even if the use is low. With GNU df, the header contains '%' sign and so you have two lines there:
Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda7              32G   24G  5.8G  81% /sybase

Awk in jayan's command will process both of these lines. Regardless of the use% in the second line (the actual record), the first line will always be matched. That's because the fifth field of the first line, stripped of the '%', will satisfy the if condition:
Code:
"Use" > 94

is always true in awk's language. That is because the integer 94 is automatically converted to a string, and then compared to "Use".

As for my code, it seems that the variable 'use' is empty. Try to echo 'use' variable before the if statement. What does this print?
Code:
df | grep /sybase


Last edited by mirni; 01-17-2012 at 09:27 AM.. Reason: removed offensive word "bug"
# 6  
Old 01-17-2012
@mirni, i am using SunOS 5.9 and when firing df -h , it wont show Use% in fifth column ..
Code:
$ df -h /
Filesystem                    size  used   avail capacity  Mounted on
/dev/vx/dsk/bootdg/rootvol    9.8G   7.8G   1.9G    81%    /

Hence the header of df -h might vary from OS to OS and dont come to a conclusion that the one liner has a bug .. Smilie

If your OS doesnt support that feature, below is the one liner will suit your match. Smilie
Code:
$ df -h /sybase | nawk '/sybase/{if(int($5)>=94){print "rm /sybase/logs/*"}}' | sh

One more in nawk ..
Code:
$ df -h /sybase | nawk '/sybase/{sub(/%/,"");if($5>=94)print "rm -r /sybase/logs/*" }' | sh

# 7  
Old 01-17-2012
I didn't mean to offend you -- I appreciate you being here helping others. I only wanted to point out a potential pitfall. Although it may work with your version of df (and perhaps the OP's also), it didn't with mine on GNU/Linux system.
Code:
df (GNU coreutils) 8.5

Remember there may be many people viewing this post trying to find a solution to their problem on their variety of *nix platforms...
I reformulated my previous post a little bit.
Peace,
mirni
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a sql script on multiple $Oracle_SID at once and send an alert if reaches the threshold

I am trying to accomplish following tasks in my KSH script: Run a sql script on multiple $ORACLE_SID at once Sends an alert only if the threshold > 2 for any of the $ORACLE_SID Please advice as to how I can approach this!! Thanks in advance! (1 Reply)
Discussion started by: jd_2000
1 Replies

2. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

3. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

4. Solaris

Script fails when generated output file reaches a particular size

Hi All, New to unix. Here is the problem. Running a script that extracts data from hyperion essbase and generates a file in unix. This script fails most of the times with a very low success rate. The data has increased a lot in the last few months resulting in the file being more than 2 gb. ... (2 Replies)
Discussion started by: noufalshaw
2 Replies

5. Shell Programming and Scripting

deleting files in sub directories!

Hello out there, I want to setup a crontab feature to remove temporary pdf files from sub directories that are older than 30 days. I want to presevere the directory structer though. I got this far for a command. Will this remove the pdf's in subdirectories or just directly under the pdf folder? If... (6 Replies)
Discussion started by: vsekvsek
6 Replies

6. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

7. Shell Programming and Scripting

Deleting all empty files in sub directories with a command

Hello Friends, Im trying to delete empty files in subdirectories with a command. I can find them checking only one directory in each step and then show them with my command like below moreover i could not add removing part: ls -l */* | awk '{if ($5==0) printf "%3s %2d %s... (5 Replies)
Discussion started by: EAGL€
5 Replies

8. UNIX for Dummies Questions & Answers

What/How to check before deleting files and directories

Hi there, I have a some directories containing web files that are old, and I need to remove them. I know that there might be sym links and hyperlinks pointing to these old directories. If that's the case, then I'll have to fix the links before deleting these old directories to avoid broken... (4 Replies)
Discussion started by: yvochan
4 Replies

9. UNIX for Dummies Questions & Answers

Deleting string from file of same name within several directories

I searched the forum, but I don't think I was able to find exactly what I'm looking for. Is there a command in shell where I can delete a particular string from every file it occurs in (where the file is named the same, but sits in different directories)? Example: Within the directory I am... (5 Replies)
Discussion started by: halo969
5 Replies

10. UNIX for Dummies Questions & Answers

deleting log files only in particular directories

Hi My problem is i have to remove some log files in specific named directories on a regular basis using shell scripts. What i want my shell script to do is i give the shell script some listing of directories from which to delete all log files recursively. Can anyone please help me. ... (2 Replies)
Discussion started by: sameervs
2 Replies
Login or Register to Ask a Question