[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shell script removing log files.

On 12/22/2014 at 01:51 PM, peter@easthope.ca wrote:

> This command in a shell script removes unwanted log files.
> for i in $( echo *.Log ); do 
>   /bin/rm $i; 
>   echo "Removed $i." 
> done
> In the edge case of no matching files, rm complains.
> /bin/rm: cannot remove `*.Log': No such file or directory
> If echo is replaced with ls, it complains when there 
> is no match.
> Does anyone have a tidy solution for this task?

I'm not quite sure just what it is that you're wanting to do, but if my
guess is correct, this should get the job done:

if [ -e "/path/to/*.log" ] ; then
  for i in /path/to/*.log ; do
    /bin/rm "$i" &>/dev/null && echo "Removed $i." ;

That should run the removal loop only if there is at least one log file
present to be removed, and report removal only if the removal actually
succeeded - rather than claiming success even if, e.g., the file could
not be removed because appropriate write permission was missing.

If that isn't what you want to do, then you'll need to explain better
what it is you're trying to accomplish.

   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

Attachment: signature.asc
Description: OpenPGP digital signature

Reply to: