Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with script (rm command)

Posted on 2007-04-01
7
Medium Priority
?
1,471 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 80 total points
ID: 18834681
rm `ls -ltr ${LOG_PATH}/*.log | grep "${DATE}" |  awk '{print $9}'` 2> /dev/null
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 80 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 80 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
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…
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…

704 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