Transfer files using lftp in bash script

Simple way with ftp:

#!/bin/bash

ftp -inv ip << EOF
user username password

cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload

bye
EOF

With lftp:

#!/bin/bash

lftp -u username,password ip << EOF

cd /home/xxx/xxx/what/you/want/
put what_you_want_to_upload

bye
EOF

From lftp manual:

-u <user>[,<pass>]  use the user/password for authentication

You can use mkdir for create a directory. And you can use put command several time like this:

put what_you_want_to_upload
put what_you_want_to_upload2
put what_you_want_to_upload3

And you can close connection with bye


You can check folder is exist or not like this:

#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test1231")

if [ "$checkfolder" == "" ];
then
echo "folder does not exist"
else
echo "folder exist"
fi

From lftp manual:

-c <cmd>            execute the commands and exit

And you can open another connection for put some files.


I don't know how to check folder is exist or not with one connection, but I can do that like this. Maybe you can find better solution:

#!/bin/bash
checkfolder=$(lftp -c "open -u user,pass ip; ls /home/test1/test2")

if [ "$checkfolder" == "" ];
then

lftp -u user,pass ip << EOF

mkdir test2
cd test2
put testfile.txt
bye
EOF

else

echo "The directory already exists - exiting"

fi