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
If statement not working Hazmeister Shell Programming and Scripting 6 12-13-2007 04:33 AM
korn shell to bash - statement not working brdholman UNIX for Dummies Questions & Answers 5 10-15-2007 09:49 AM
Find cmd not working as expected Vishal123 Shell Programming and Scripting 6 08-29-2007 02:11 AM
how to find in which shell i am working.. pineapple Shell Programming and Scripting 2 04-01-2007 10:56 PM
find command not working jabrady UNIX for Advanced & Expert Users 7 10-04-2005 12:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-14-2005
geomonap geomonap is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 9
Unhappy Ksh find/if statement not working

Hi guys:
I am trying to delete multiple files in a folder with different names. Below is the script that I was trying, but it doesn't work

**************************
#!/bin/ksh
DATE=`date '+20%y%m%d'`
DEL_DIR=<dir where files have to be deleted>
let DATE2=$(($DATE - 2))
let DATE1=$(($DATE -1))
DELFILE_1=abc1.txt.$DATE1*
DELFILE_2=abc1.txt.$DATE2*
DELTAR=cdr.$DATE2.tar

find $DEL_DIR -name $DELFILE_2 -exec rm -f '{}' \;
find $DEL_DIR -name $DELTAR -exec rm -f '{}' \;

**************************
The files I am looking to delete are:
abc.<todays date minus 2>.tar
ex: abc.20051212.tar

abc1.txt.<date-2><some numbers>
ex: abc1.txt.20051212010101

The error message I get is when I run the find command(s) :
find: paths must precede expression
Usage: find [path...] [expression]

The find statement works when on the command line but not in the script. I also used the following command line which gave the same error message when run from the script but not on the command line

find $DEL_DIR -name $DELFILE_2 |xargs rm -f

I also tried using the If statement as shown below but it doesn't delete the files

if [[ -a $DELFILE_2 ]]; then
rm -f $DELFILE_2
fi

fi [[ - a $DELTAR ]]; then
rm -f $DELTAR
fi

The server is running redhat linux 4
Your help is appreciated.

Thanks,

-Gm

Last edited by geomonap; 12-14-2005 at 05:49 PM..
  #2 (permalink)  
Old 12-14-2005
dtruchan dtruchan is offline
Registered User
  
 

Join Date: Dec 2005
Location: Mid-Michigan
Posts: 2
Use find -mtime, or +mtime

find /tmp -type f -name '*.log' -mtime +2 -exec rm -rf '{}' ';'

this would find all files in slash tmp that end with dot log that are two days old.
  #3 (permalink)  
Old 12-14-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Quote:
Originally Posted by dtruchan
this would find all files in slash tmp that end with dot log that are two days old.
A slight correction. This would find files that are more than 3 days old.
  #4 (permalink)  
Old 12-15-2005
geomonap geomonap is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 9
Actually the issue is not that find command is not working. I am able to run the find command successfully on the command line but not in the script. When I use it in the script, it gives me the following error message:

find: paths must precede expression
Usage: find [path...] [expression]


Also, the if statement doesnot work if I find multiple files with the following characteristics:
abc1.txt.$DATE2*

How can i successfully look for files "abc.txt.$DATE2* and delete them. On the command line I can use the find command and delete these files but the same command doesn't work in the script.

-George
  #5 (permalink)  
Old 12-15-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Quote:
Originally Posted by blowtorch
A slight correction. This would find files that are more than 3 days old.
Getting closer

-mtime 2 means exactly 2 days old (i.e. exactly 60*60*24*2 = 172,800 seconds old)
-mtime -2 means less than 2 days old
-mtime +2 means more than 2 days old
  #6 (permalink)  
Old 12-15-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
geomonap, do stuff like:
echo DELFILE_2= $DELFILE_2
just before your find statement so you can see what is happening.

And I doubt that you are running the same find command from your command line. Instead, you are probably replacing the variables with what you think they should contain. Your whole problem is probably garbage in your variables.
  #7 (permalink)  
Old 12-15-2005
geomonap geomonap is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 9
perderabo:
I ran the that entire part of the script on the command line, and I was successful.

the actual files look like this:

abc1.txt.2005121303000
abc1.txt.2005121303001
abc1.txt.2005121409001
abc1.txt.2005121503081

So I need to filter and delete all the files that have 20051213 in them which signifies that it is 2 days older. I don't want to delete them on the basis of the mtime cause the mtime can change.

-George
Closed Thread

Bookmarks

Tags
linux, 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 04:51 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