Operating System - Linux
1745793 Members
3646 Online
108722 Solutions
New Discussion юеВ

Re: Need help with -exec option of "find" command.

 
Sagar Thanawala
New Member

Need help with -exec option of "find" command.

Hello,

Iam new to this .
My task is to code a script which will rename log files with "date" appended to it.


FOr example :-
pkg_name.cntl.log has to be renamed to
pgk_name.cntl.log.yymmdd.hh.mm

ex. WM01P.cntl.log.060108.13.05

I have to usethe find command with -exec option.

I ran the find command to check it out [with -exec option].

find /etc/cmcluster -name *cntl.log -type f -exec ls -ltr {} \;

and output is :-
/etc/cmcluster/C01/dbC01.cntl.log
/etc/cmcluster/C01/ciC01.cntl.log
/etc/cmcluster/DS1/dbDS1.cntl.log
/etc/cmcluster/DS1/ciDS1.cntl.log
/etc/cmcluster/DS2/dbDS2.cntl.log
/etc/cmcluster/DS2/ciDS2.cntl.log
/etc/cmcluster/DS3/dbDS3.cntl.log
/etc/cmcluster/DS3/ciDS3.cntl.log
/etc/cmcluster/E01/dbE01.cntl.log
/etc/cmcluster/E01/ciE01.cntl.log
/etc/cmcluster/S01/dbS01.cntl.log
/etc/cmcluster/S01/ciS01.cntl.log
/etc/cmcluster/U02/dbU02.cntl.log
/etc/cmcluster/U02/ciU02.cntl.log
/etc/cmcluster/oracledevl/oracledevl.cntl.log
/etc/cmcluster/DW1/dbDW1.cntl.log
/etc/cmcluster/DW1/ciDW1.cntl.log

Please note that the directories are different after cmluster direcotry.

Let me know how to script this.

Thanks in Advance
5 REPLIES 5
Hein van den Heuvel
Honored Contributor

Re: Need help with -exec option of "find" command.

I would use a perl script similar to the one below.
In a pinch you make the fixed 'glob' string an argument.
I would choose this solution because it will work unmodified on Hpux, Tru64, Linux, Windoze, OpenVMS,... independent of a shell choice and so on.

hth,
Hein.


use strict;
use warnings;
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
my $timestamp = sprintf (".%02d%02d%02d.%02d.%02d",
$year%100, $mon+1, $mday, $hour, $min);
while (<*.log>) {
rename $_, $_ . $timestamp;
}
Hein van den Heuvel
Honored Contributor

Re: Need help with -exec option of "find" command.

Acutally.. I replied too soon / did not read carefully enough.

You clearly need to traverse a directory tree. That's nor too hard in perl through a module: File::Find

But I would possibly let find do the finding and change the perl to the sample below(untested, the other was tested)
The -exec perl my-rename.pl {}

Hein.

------------------ my-rename.pl ---------
use strict;
use warnings;
my $old = shift;
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
my $new = sprintf ("%s.%02d%02d%02d.%02d.%02d",
$old, $year%100, $mon+1, $mday, $hour, $min);
rename $old, $new;
Biswajit Tripathy
Honored Contributor

Re: Need help with -exec option of "find" command.

How about the following:

---
TAIL=$(date "+%m%d%y.%H.%M.%S")
for file in `find /etc/cmcluster -name '*cntl.log' -type f`
do
mv $file $file.$TAIL
done
---

Note the single quote around wildcard * with the
'find' command line. Modify the date command to
get whatever format you want.

- Biswajit
:-)
Sagar Thanawala
New Member

Re: Need help with -exec option of "find" command.

Hello Guys,

Thanks all of you.
I used your suggestions adn tried and it works. Will decide on one of these.

Thanks once again !

Sagar
James R. Ferguson
Acclaimed Contributor

Re: Need help with -exec option of "find" command.

Hi:

Here's another, short variation to accomplish your goal:

# EXT=`date "+%m%d%y.%H.%M"`
# find /etc/cmcluster -type f -name "*cntl.log" | xargs -i mv {} {}.$EXT

Regards!

...JRF...