Line in my script trying to get the output of find to be in the here-string but I keep getting "put {}" with the obvious "{}: No such file or directory"
find "$SOURCE_DIR" -type f -name "*.txt" -exec sshpass -p "$PASSWORD" sftp -oPort=$PORT $USER@$HOST:$HOST_DIR <<< $'put' {} 2>&1 \;
How to I pass the filename into the here-string so that sftp will put the file?
My previous line in the script was this which I had no problems with. However, I can no longer use curl in this script.
find "$SOURCE_DIR" -type f -name "*.txt" -exec curl -T {} sftp://$USER:$PASSWORD@$HOST:$PORT$HOST_DIR 2>&1 \;
CodePudding user response:
find -exec
doesn't implicitly start a shell, so it doesn't run shell operations or redirections.
You could make it start a shell (this is discussed in the Complex Actions section of Using Find), but it's just as easy to write a NUL-delimited list of filenames, and read them into your shell:
while IFS= read -r -d '' file <&3; do
sshpass -p "$PASSWORD" sftp -oPort="$PORT" "$USER@$HOST:$HOST_DIR" <<<"put $file"
done 3< <(find "$SOURCE_DIR" -type f -name "*.txt" -print0)
As an additional optimization, think about only running sftp
once, not once per file (note that this is using the GNU -printf
extension to find
):
find "$SOURCE_DIR" -type f -name "*.txt" -printf 'put %p\n' |
sshpass -p "$PASSWORD" sftp -oPort="$PORT" "$USER@$HOST:$HOST_DIR"