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

Re: find and coy



On 9/1/13, Craig L. <craig@gtek.biz> wrote:
> 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.

It is evident that you are achieving part of what you want.

It is evident (since you are asking a question), that you are failing
to achieve the other part of what you want. It was unnecessary to have
pointed it out, so apologies for that.

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

Actually, what you want I think is independent of stdout, as in, you
perhaps could use xargs

>> I'm not familiar with -exec option.
>
> With all due respect, follow your advice: man find and search for
> -exec.

With all due respect, _you_ are the one seeking assistance. I tried to
point you in a direction I knew about and thought might be useful to
you. It is not my job to come up with the perfect solution for you, so
it is not for me to follow the advice, since it's not my problem. Not
the best way to invite further assistance...

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

I don't. But |xargs is how I would do it, eg:

find /var/lib/postgresql/9.1/backup -mmin -60 -a -iname '*.sql' -printf "blah" \
| xargs cp '{}' /var/data.backup/


Reply to: