Solved

Help with script (rm command)

Posted on 2007-04-01
7
1,460 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

746 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

13 Experts available now in Live!

Get 1:1 Help Now