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

Re: find and coy



Thanks Zenaan (and apologies to all for the poor formatting of my original
post. I forgot this bloody web interface defaults to that. fmt to the rescue)

On Saturday, August 31, 2013 11:16, "Zenaan Harkness" <zen@freedbms.net> said:

> On 9/1/13, craig@gtek.biz <craig@gtek.biz> wrote:
>> find the recent file and copy only it. I have no problem developing that
>> find command,
> 
> but evidently not quite ...

   find /var/lib/postgresql/9.1/backup -mmin -60 -a -iname '*.sql' \
   -execdir cp '{}' /var/data.backup/ ';'

copies the file to /var/data.backup/dump_08-31-13.sql just fine.

>> but I want to rename the copy in the process by pre-pending
>> the file name with the hostname so I can differentiate between dumps from
>> different servers. I don't want bother with having to implement a
>> destination directory structure since new systems may come and go. So I am
>> trying to see how this works by echoing the find output, and I can see what
>> the problem is but I don't know how to get around it. find's {} place holder
>> is expanding to ./<filename> and I need just <filename>
> 
> man find, and search for "-printf format",
> ie type "man find<enter>/\-printf format"
> 
> Looks like you want a variation on this option:
>   -printf "%f\n"

printf is one of the actions that find can take when a match occurs,
and its action is to output the match to stdout. I don't want to output
the filename to stdout.

> 
> I'm not familiar with -exec option.

With all due respect, follow your advice: man find and search for
-exec. It is another possible action, not an option. It is listed in the
man page about 13 actions up from "-printf format". Instead of printing
the match to stdout, it allows you to define a command to execute on the
match, substituting the string {} with the match and then executing the
defined command on each match. -execdir does the same, but does so as if
CWD was the directory in which the match occurred. This is what I need,
but the match is still returned as a relative path and I need to strip
the leading ./ off of that return.

That being said, this is Linux and there is always more than one way
to accomplish your goal. The following does what I need:

   for dumpfile in \
   `find /var/lib/postgresql/9.1/backup -mmin -60 -a-iname '*.sql'`
   do
      cp $dumpfile /var/data.backup/`hostname`.`basename $dumpfile`
   done

I would still like to know how to do it as a find action if anyone
has suggestions.

Regards,
Craig


Sent - Gtek Web Mail



Reply to: