The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
delete file older than N days ericaworld Shell Programming and Scripting 7 02-21-2008 08:34 AM
Delete user file(s) older then 'X' days ?? varungupta UNIX for Advanced & Expert Users 2 08-24-2007 05:01 AM
delete files and folders older than 3 days melanie_pfefer Shell Programming and Scripting 7 12-18-2006 12:58 PM
How can I delete files older than 7 days? odogbolu98 UNIX for Dummies Questions & Answers 3 02-26-2002 08:35 PM
delete files older than 7 days lesstjm UNIX for Dummies Questions & Answers 1 11-06-2001 10:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-21-2008
kamathg kamathg is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 43
Delete files older than certain days - Wrong !!!

Guys,

I had raised a question about deleting files older than today in a specific directory and i got this as an answer

find ${ARCH_DEST}/*.gz -mtime +0 -exec rm -f {} \;

What happens when there aren't files that meet this criteria ? Can it delete any other directories ? I had a shocking output of the script yesterday as it looks to me that it has deleted quite a number of directories.

Could it be this command that generated this output ?
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/backup': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/files': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/files/lib': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/files/lib/libn10.a': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/files/lib32': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/files/lib32/libn10.a': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/etc': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/etc/config': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/original_patch/etc/xml': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/rac': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/scratch': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/files': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/files/lib': Is a directory
rm: cannot remove `product/10.2.0/.patch_storage/6397938_Sep_21_2007_07_19_39/files/lib/libn10.a': Is a directory


It removed quite a lot of files from a number of directories. It has worked before. Could it be this script ? I've made sure that the script cd's to the right directory

Thanks
  #2 (permalink)  
Old 07-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Add the "-type f" option to exclude directories:

Code:
find ${ARCH_DEST}/*.gz -mtime +0 -type f -exec rm -f {} \;
You should try it first with an ls command before using the rm command:

Code:
find ${ARCH_DEST}/*.gz -mtime +0 -type f -exec ls -l {} \;
Regards
  #3 (permalink)  
Old 07-21-2008
kamathg kamathg is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 43
Thank you Franklin.
The ARCH_DEST is obtained from within a Oracle database, but accidentally the database down when this happened. Could it be that it ran the command for all the directories ?

Also, if no .gz files are available, the the return code is non-zero. basically, i am trying to see if the deletion .gz is successful and if it fails, report it as an error.

Something like this

Delete_Archive() {

LogDest="select value from v\$parameter where name='standby_archive_dest';"
runSql "${LogDest}"
ARCH_DEST="${COUNT}"
cd ${ARCH_DEST}

echo `pwd`

#find ${ARCH_DEST}/*.gz -mtime +0 -exec rm -f {} \;
rm *.gz

RC2=$?

if [[ $RC2 != 0 ]]
then
Mail_Subject="Dataguard on host `hostname`. Delete archive logs older than 24 hrs failed. Please investigate (SEV-4)"
ls -ltr | ${ARCH_DEST} > "${ORACLE_BASE}/oralogs/DELETE_ARCHIVE.log"
Mail_User "$Mail_Subject" "${ORACLE_BASE}/oralogs/DELETE_ARCHIVE.log"
exit 1
fi
}


Can this be coded better ? Can i put a condition saying if the number of files deleted is 0, then it's not an error ?

Thanks
  #4 (permalink)  
Old 07-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Code:
echo `pwd` => pwd is sufficient

#find ${ARCH_DEST}/*.gz -mtime +0 -exec rm -f {} \;
rm *.gz => this deletes all .gz files!!

RC2=$? => now it gives the returncode of the rm command not the returncode of the find command
Replace the above code with:

Code:
pwd

find ${ARCH_DEST}/*.gz -mtime +0 -exec ls -l {} \; # Use ls -l first to be sure you get the expected files
# find ${ARCH_DEST}/*.gz -mtime +0 -exec rm -f {} \;
RC2=$?
Regards
  #5 (permalink)  
Old 07-21-2008
sudhamacs sudhamacs is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 98
put an if condition to check that ur sql returned the results successfully, otherwise the find command reads all the *.gz files from all the subdirectories and deletes.
  #6 (permalink)  
Old 07-22-2008
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,172
sudhamacs and franklin52 make good points.

Test with "ls" first.
Check that ARCH_DEST is not blank.
Use "-type f" to only find files.


Here's another version with some more points:
This version:
* has no limit on the number of filenames.
* does nothing if there are no files to delete (and does not error).
* works if there are spaces in the filename or directory name.
* "escapes" the "*" and the "." in the "-name" parameter to avoid false matches.


#!/bin/ksh

if [ -d "${ARCH_DEST}" ]
then
find "${ARCH_DEST}/" -name \*\.gz -type f -mtime +0 -print | while read FILENAME
do
ls -ald "${FILENAME}"
# rm "${FILENAME}"
done
else
echo "ARCH_DEST incorrect or missing: ${ARCH_DEST}"
fi
  #7 (permalink)  
Old 07-22-2008
BMDan BMDan is offline
Registered User
  
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 66
When testing with ls, remember to pass "-d", or you'll get strange results if you do encounter a directory somehow.
Closed Thread

Bookmarks

Tags
mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0