Operating System - HP-UX
1834144 Members
1991 Online
110064 Solutions
New Discussion

"find" problem: multiple use of result in -exec clause

 
SOLVED
Go to solution
Raynald Boucher
Super Advisor

"find" problem: multiple use of result in -exec clause

Hello all,
I tried several variations but find myself stumped... Maybe it's impossible but I'd like to know why.

I'm trying to create an ftp command file by using the following find statement:

find . -type f -newer start_file_name -exec echo put {} `basename {}` >> ftp_commands \;

I want to use "basename" because the directory structure doesn't exist on the target server.
A test of the above produces the following:

put ./work/pts8531/tsk/test_all_sql.sh ./work/pts8531/tsk/test_all_sql.sh
put ./work/pts8531/ctk/test_all_sql.sh ./work/pts8531/ctk/test_all_sql.sh
put ./test ./test
put ./nbcase/testcomp.sql ./nbcase/testcomp.sql
put ./ora600/test_query.sql ./ora600/test_query.sql
put ./ora600/test_query.log ./ora600/test_query.log
put ./sysmon/test ./sysmon/test

Any ideas?

RayB
9 REPLIES 9
Sundar_7
Honored Contributor
Solution

Re: "find" problem: multiple use of result in -exec clause

You can try this if you would like

find . -type f -newer start_file_name -exec printf "put %s " {} \; -exec basename {} \;
Learn What to do ,How to do and more importantly When to do ?
A. Clay Stephenson
Acclaimed Contributor

Re: "find" problem: multiple use of result in -exec clause

I would take a dive and conquer approach:

#!/usr/bin/sh
typeset OUT=ftp_commands
rm -f ${OUT}
typeset F=""
find . -type f -newer start_file_name -print | while read F
do
echo "put ${F} $(basename ${F})" >> ${OUT}
done


If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: "find" problem: multiple use of result in -exec clause

Hi Ray:

Use 'xargs'.

# find . -type f -newer start_file_name | xargs -i echo put {} `basename {}` >> ftp_commands

Regards!

...JRF...

Court Campbell
Honored Contributor

Re: "find" problem: multiple use of result in -exec clause

Just for grins

find . -type f -newer start_file_name -exec echo put {} \\c \; -exec basename {} \; > ftp_commands
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Raynald Boucher
Super Advisor

Re: "find" problem: multiple use of result in -exec clause

Ayoye!

3 perfect answers.
Sorry James, close but no cigar.
Looks like the result can only be used once by "-exec" clause or used outside the execution of the find command.

Shows I have a lot to learn...

Thanks all
Raynald Boucher
Super Advisor

Re: "find" problem: multiple use of result in -exec clause

Cheersa all,
thanks again
Dennis Handly
Acclaimed Contributor

Re: "find" problem: multiple use of result in -exec clause

The problem with `basename ... ` is that it is evaluated before the find command is executed.

You might be able to use $(...) and then quote the $ then use the sh and eval but Clay's solution would be easier to understand.
Eric Raeburn
Trusted Contributor

Re: "find" problem: multiple use of result in -exec clause

This also works:

for f in $(find...) ; do
echo $f $(basename $f) >> file
done
Eric Raeburn
Trusted Contributor

Re: "find" problem: multiple use of result in -exec clause

sorry...i left out the "put" from the echo command.