Unix Shell - using "find" to remove files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > July 2005 > using "find" to remove files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author using "find" to remove files
kat

2005-07-28, 6:00 pm

I'm trying to clean up some log files using wildcards and wrote a
simple script called cleanup.ksh:
#!/bin/ksh
echo find $1 -name "$2" -exec rm -f {} \;
find $1 -name "$2" -exec rm -f {} \;

$> touch apps1.log apps2.log apps3.log
$> cleanup.ksh /tmp apps*.log
find /tmp -name apps1.log -exec rm -f {} ;
$> ls app*log
apps2.log apps3.log

However when I ran the script, the $2 was automatically evaluated to
"apps1.log" and thus only one file was deleted. I would like to delete
all files that match the string - apps*.log. If I run this cmd (find
/tmp -name "apps*.log" -exec rm -f {} \;) directly on the command line,
it works fine. All three files were removed. I'm not sure what the
problem is. Can someone help?

thanks.

katie

Chris

2005-07-28, 6:00 pm

The shell is expanding apps*.log before it executes the command. This
is standard behavior. The command executed as you types it above is
translated to the following by the shell prior to invoking your script:

$> cleanup.ksh /tmp apps1.log apps2.log app3.log

That is why you are getting apps1.log as the value of $2 in your
script. If you look at them, $3 would be apps2.log and $4 would be
apps3.log.

You need to enclose your second argument to cleanup.ksh in quotation
marks if you do not want it expanded by the shell:

$> cleanup.ksh /tmp "apps*.log"

This will run as you are expecting here.

Make sense?

kat

2005-07-28, 6:00 pm

Perfect sense. Thanks!

cLIeNUX user

2005-07-28, 6:00 pm


Be damn careful.

John L

2005-07-29, 8:08 am


"Chris" <ctaliercio@yahoo.com> wrote in message news:1122570186.397069.246830@f14g2000cwb.googlegroups.com...
> The shell is expanding apps*.log before it executes the command. This
> is standard behavior. The command executed as you types it above is
> translated to the following by the shell prior to invoking your script:
>
> $> cleanup.ksh /tmp apps1.log apps2.log app3.log
>
> That is why you are getting apps1.log as the value of $2 in your
> script. If you look at them, $3 would be apps2.log and $4 would be
> apps3.log.
>
> You need to enclose your second argument to cleanup.ksh in quotation
> marks if you do not want it expanded by the shell:
>
> $> cleanup.ksh /tmp "apps*.log"
>
> This will run as you are expecting here.
>
> Make sense?
>


Absolutely.

However, the OP might consider whether this way of removing
log files is a good idea in the first place. Most sites use
log rotation so that the last three (or whatever number you
choose) log files are in place (often gzipped) should anyone
need to investigate a problem.

Some applications (eg apache) come with log rotation utilities,
as do some operating systems (eg Solaris) and they are simple
to write and can be found on the web. Google "log rotation"
for more details.

--
John.


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com