Solved

Help with script (rm command)

Posted on 2007-04-01
7
1,463 Views
Last Modified: 2013-12-27
The following script will remove log files that were last modified yesterday:

DATE=`TZ=GMT+24 date`
DATE="`echo ${DATE} | awk '{print $2}'` `echo ${DATE} | awk '{print $3}'`"
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'`

That works fine if there are indeed files modified yesterday that can be deleted. If there are no files to be deleted, the rm command will show the usage statement. How can I add the check before applying the rm command? Thanks.
0
Comment
Question by:integrosys
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18834654
touch -t`TZ=GMT+24 date +%Y%m%d`0000 yesterday
touch -t`TZ=GMT+00 date +%Y%m%d`0000 today
find  . -newer yesterday \! -newer today -exec rm {} \;
0
 

Author Comment

by:integrosys
ID: 18834663
I don't really understand your solution. Is it possible to modify my script a bit to get what I want instead of giving me a totally new solution? Thanks.
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 18834681
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'` 2> /dev/null
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:integrosys
ID: 18834698
Hi,

That looks fine. But suppose that there are indeed files to delete, but somehow the rm command failed (maybe due to file permission), how would I be able to catch this condition and return an error code from the script? In short, this is what I want:

if (there are files to delete) {
  rm files
  if (rm command failed) {
    exit 1
  }
}
exit 0
0
 
LVL 7

Assisted Solution

by:nixfreak
nixfreak earned 20 total points
ID: 18838592
Replace the below line:
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'`

with:
FILES=`ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'`
[ -z "$FILES" ] && exit 1
rm $FILES
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 20 total points
ID: 18839800
Just use the

rm -f ......

to stop rm displaying a usage message if no files exist.
0
 

Author Comment

by:integrosys
ID: 18841587
Thanks. All your solutions are useful to me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now