![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
Add the "-type f" option to exclude directories:
Code:
find ${ARCH_DEST}/*.gz -mtime +0 -type f -exec rm -f {} \;
Code:
find ${ARCH_DEST}/*.gz -mtime +0 -type f -exec ls -l {} \;
|
|
||||
|
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 |
|
||||
|
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
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=$?
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| mtime |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|