Shell script delete log files from folder & subfolders on space usage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script delete log files from folder & subfolders on space usage
# 1  
Old 03-22-2009
Shell script delete log files from folder & subfolders on space usage

Hi,
I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes.
I have to keep the latest 5 files at any time, my problem is that log files are generated based on today date folder eg:
2009_03_20, 2009_03_21, 2009_03_22 etc. and
a log file is generate at 30 time interval gap or may generate 2 files per minute depends upon db activity.

how to keep only latest 5 files and remove rest of the files
my script looks like this in linux env.
#! bin/bash
ORACLE_SID="$1"
limit=$2

export $ORACLE_SID
fdir=/tmp/log
export $fdir

export ORACLE_HOME=`grep $ORACLE_SID: /etc/oratab |awk -F: '{print $2}'`
sqlplus -s "/ as sysdba" <<EOF >$fdir/fravalue
@$fdir/arclg_db.sql
EOF

fradest=`cat $fdir/fravalue |awk '{print $1}'`
spaceused=`cat $fdir/fravalue |awk '{print $2}'`

if [ $spaceused -gt $limit ]; then
echo "time to delete archivelogs to free up space"
find $fradest/$ORACLE_SID/archivelog -type f -maxdepth 2 -printf "%T@\t%p\n"| sort -k1,1n |cut -f2-|sed '$d'|rm -rf

else
echo "Not required to delete logs.. lots of space"
fi
please advice me, or suggestions to improve my script.

Thanks
Saha
# 2  
Old 03-22-2009
Quote:
Originally Posted by saha
Hi,
I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes.
I have to keep the latest 5 files at any time, my problem is that log files are generated based on today date folder eg:
2009_03_20, 2009_03_21, 2009_03_22 etc. and
a log file is generate at 30 time interval gap or may generate 2 files per minute depends upon db activity.

how to keep only latest 5 files and remove rest of the files
my script looks like this in linux env.

Please put code inside [code] tags.
Quote:
Code:
#! bin/bash
ORACLE_SID="$1"
limit=$2

export $ORACLE_SID
fdir=/tmp/log
export $fdir


Why are you trying to export these variables? I see nothing in the script that would require it.

If you did need it, those commands would not export the variables.

Code:
export ORACLE_SID fdir ## no $

Quote:
Code:
export ORACLE_HOME=`grep $ORACLE_SID: /etc/oratab |awk -F: '{print $2}'`
sqlplus -s "/ as sysdba" <<EOF >$fdir/fravalue
@$fdir/arclg_db.sql
EOF

fradest=`cat $fdir/fravalue |awk '{print $1}'`
spaceused=`cat $fdir/fravalue |awk '{print $2}'`


You don't need awk and even if you did, you would certainly not need cat.

Code:
read fradest spaceused < $fdir/fravalue

Quote:
Code:
if [ $spaceused -gt $limit ]; then
  echo "time to delete archivelogs to free up space"
  find $fradest/$ORACLE_SID/archivelog -type f -maxdepth 2 -printf "%T@\t%p\n"|
     sort -k1,1n |
      cut -f2- |
       sed '$d' |
        rm -rf


You haven't supplied any filenames to rm; rm does not read from the standard input. Perhaps you meant:

Code:
xargs rm -rf

Quote:
Code:
 else
     echo "Not required to delete logs.. lots of space"
 fi

# 3  
Old 03-22-2009
Thanks for the code suggestions, i will try to implement and
need inputs on how to keep latest 5 files all the time while remove all other files from the said folders & sub folders when ever the script runs.
# 4  
Old 03-22-2009
Quote:
Originally Posted by saha
how to keep latest 5 files all the time while remove all other files from the said folders & sub folders when ever the script runs.

Code:
ls -t <list of files> | tail -n +6 | xargs rm -f

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Splitting a folder containing different files into subfolders

I have a folder with 4000 (*3) files like gr_q4_gb-1.anc gr_q4_gb-1.anc_cdr_st.txt gr_q4_gb-1.anc_cdr_tr.txt gr_q4_gb-2.anc gr_q4_gb-2.anc_cdr_st.txt gr_q4_gb-2.anc_cdr_tr.txt gr_q4_gb-3.anc gr_q4_gb-3.anc_cdr_st.txt gr_q4_gb-3.anc_cdr_tr.txt . . gr_q4_gb-4000.anc... (6 Replies)
Discussion started by: sammy777888
6 Replies

3. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

4. Shell Programming and Scripting

[Solved] Get files & delete them by shell script

I want to use my script to get any file then delete it once it transfers to my side , I manage to create below script to generate "list" file which contains all file names in "10.10.1.1" then I made "a.out" file which contains the commands that I want to run it on "10.10.1.1" to get & delete the... (2 Replies)
Discussion started by: arm
2 Replies

5. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

6. Shell Programming and Scripting

Perl Script to find the disk usage and to delete the files which is consuming more space

Hi All, I have written a script to check the file system usage and to delete the files which is consuming more space.Please check whether the script is corrcet #Script Starts here #!/usr/local/bin/perl #Program to find the disk space and to delete the older files #Checks the type of OS... (8 Replies)
Discussion started by: arunkarthick
8 Replies

7. Homework & Coursework Questions

unique words in files of folder and its subfolders

Hello, I tried to count all unique words of all files in one folder and its subfolders. Can anybody say me, why this doesnt work: ls| find -d | cat | tr "\ " "\n"| uniq -u | wc -l ??? Cat writes only the names of those files, but not the wors, which should be in them. Thanks for any advice. ... (9 Replies)
Discussion started by: Dworza
9 Replies

8. UNIX for Dummies Questions & Answers

How to obtain a count of files in a folder and it's subfolders

First of all, the extent of my unix knowledge is next to nil. I've been able to telnet to a unix box, and thanks to the Computer Hope website, I've been able to learn a few basic commands to navigate from folder to folder, and view contents. What I really need to do is obtain a count of all... (2 Replies)
Discussion started by: scarfinv
2 Replies

9. UNIX for Dummies Questions & Answers

How to learn the number of files under a particular folder, containing subfolders

Hi ALL I would like know how many files there under a particular folder, which contains subfolders. Thanks (5 Replies)
Discussion started by: cy163
5 Replies

10. UNIX for Dummies Questions & Answers

Basic Q: getting list of all files of type within folder & subfolders

A painfully rudimentary UNIX question for somebody. I've been puzzling over this for the last hour but can't find the right command. I'm simply trying to get a list of all files - and their full paths - within a folder & subfolders which have extension .php and .js. That's it! No amount of... (1 Reply)
Discussion started by: AtomicPenguin
1 Replies
Login or Register to Ask a Question