Solved

Help with script (rm command)

Posted on 2007-04-01
7
1,470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…
Suggested Courses

615 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