FTP files in different folders using shell script - bash

I'm trying for an ftp script, that sends files in different folders within the same connection, but no luck with below script.
#!/bin/bash
HOST_NAME=host.server
username= user_name
passwd= password
remote = /path_to_remote/folder
local = /path_to_local/folder
folder=$1
pwd
ftp -in <<EOF
open $HOST_NAME
user $username $passwd
cd local/
lcd remote/
put a_filename_<timestamp>.txt
mkdir $remote/$folder
cd $remote/$folder
lcd $local/$folder
put b_filename.txt
close
bye
Adding to this, at run-time, is it possible to send only the latest files created in the last 10 minutes?

Try this:
#!/bin/bash
HOST_NAME=host.server
username=user_name
passwd=password
remote=/path_to_remote/folder
local=/path_to_local/folder
folder="$1"
pwd
ftp -in <<EOF
open "$HOST_NAME"
user "$username" "$passwd"
cd local/
lcd remote/
put a_filename_<timestamp>.txt
mkdir "$remote/$folder"
cd "$remote/$folder"
lcd "$local/$folder"
put b_filename.txt
close
bye
EOF
Notes:
The shell doesn't allow spaces next to =.
Quote variables.
OP code lacked closing EOF.
I've left the <timestamp> alone, that'd be a different Q.

Related

How to run multi commands in one sh file?

I have a mac os and
my question is about 'How to run multi commands in one sh file?', like this :
#!/bin/bash
ftp
open domain.com
user
pass
cd /public_html/test_folder
lcd /Users/mac/downloads
mput file
a
This commands for :
open ftp
connect to my website
enter user name
enter password
cd << for open folder in my website
lcd << open folder in my computer
mput << upload file from my computer
a << for question in ftp for file type or something like this
I add && in end each line but no work he start and stop in command line number 2
I'm sorry for my bad language :(
Use lftp, very good in ftp scripting, when you require reconnect+continue file transfer, ssh/scp transfer, etc.
It should be similar to this:
#!/bin/bash
## note: mput: -c (retry+continue); run: lftp -c 'help mput' to see help
lftp -c 'mput -c -O ftp://name:passd#server.com/path/ file1.txt file2.txt'
Alternatively, you can write lftp scripts, example (note: mirror -R = upload):
#!/usr/bin/lftp -f
open ftp://ftp.server.com
user name passwd
cd /remote/path
lcd /local/path
mirror -R folder

ksh script to send .jpg to remote server through ftp script

Hello just created a ksh script to ftp .jpg image to a remote server but the images are showing in bad qulity when I send them with the script is there a line I should fix to not alterate the image a deliver the image to the remote server like the original please help, should I add the bin line?
cp -r /path/dir/*.jpg /path/dir
cp -r /path/dir/*.JPG /path/dirREMOTE
USER='xxx'
PASSWORD=xxx'
source_dir='cd /path/images/'
target_dir='cd /images'
ftp -n xxx.xx.xxx.xx <<_FTP
quote USER $USER
quote PASS $PASSWORD
lcd /xxx/xxx/
cd /xxx
mput *.jpg
bye
_FTP
/home/test_scripts/test_script9.sh
/home/test_scripts/test_script7.sh
exit
enter code here

How to get the files from a folder everyday and store it in another folder using shell script?

I need to get the files present in a FTP location every day. other condition is I need the files only on that particular day, not in addition with older day files. then store the files in another folder using shell script
Updated FTP script from comment from OP
echo $HOST
echo $DATASET_A
echo $DATASET_B
ftp -n $HOST <<END_SCRIPT
USER $USER
PASS $PASSWD
bin
prompt
lcd $New_DATASETS
cd $DATASET_A
mget *
cd $DATASET_B
mget *
quit
END_SCRIPT
This might work:
# above heredoc
today=$(date +%Y%m%d) # adjust date format as required
# in heredoc
mget ${today}* # adjust filename pattern as required
Get out of the habit of using ALLCAPSVARS -- one day you'll accidentally overwrite $PATH and then wonder why your script is broken.

moving remote files inside ftp in shell script

I want to ftp file to my remote server and then move those files into another directory in the remote server
The ftp happens correctly but the movement is throwing an error like
550 RNFR command failed.
Can you please help?
My script is
#!/bin/sh
echo "Enter the version of the xml (eg:- v17.25)"
read version
HOST_FIRST='un01'
HOST_LAST='01'
USER='someuser'
PASSWD='somepassword'
HOST="$HOST_FIRST$FILE$HOST_LAST"
ftp -n $HOST <<-END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd /tmp
put myfile.xml
rename myfile.xml /tmp/test_ftp
quit
END_SCRIPT
exit 0
You have put myfile.xml to the tmp dir, why not just
edit
Change your script from
rename myfile.xml /tmp/test_ftp
TO
rename myfile.xml test_ftp
You have already put the file in the /tmp directory, which you have already done a cd /tmp .
That should work.
You can't specify a path in an ftp rename command and expect it to be moved as well.
And sorry, not what you want to here, but there is no move command in ftp. It is that way for security.
IHTH.

How to CD inside a SFTP connection where the connection is established using - Shell script

In my script - i create a sftp connection.
I read some directory value from user earlier and once the sftp connection is established, i try to cd to that dir which i got from the user.
But its not working, probably bec the prompt goes inside the server to which the SFTP connection was established.
In this case how to make it work ?
I also faced this problem and was able to find the solution. The solution is right there in the man page of sftp. In the man page, you will find where it is written the format of using sftp like this:
sftp [options] [user#]host[:dir[/]]
Actually, two formats are given there but this is the one I wanted to use and it worked.
So, what do you do? You simply supply the username#host as seen there, then, without any space followed by : and the path you want to change to in the client/remote server in your script and that's all. Here is a practical example:
sftp user#host:/path/
If your script does, as you state somewhere in this page,
sftp $user#$host cd $directory
and then tries to do something else, like:
sftp $user#$host FOO
That command FOO will not be executed in the same directory $directory since you're executing a new command, which will create a new connection to the SFTP server.
What you can do is use the "batchfile" option of sftp, i.e. construct a file which contains all the commands you'd like sftp to do over one connection, for example:
$ cat commands.txt
cd foo/bar
put foo.tgz
lcd /tmp/
get foo.tgz
Then, you will be able to tell sftp to execute those commands in one connection, by executing:
sftp -b commands.txt $user#$host
So, I propose your solution to be:
With user's input, create a temporary text file which contains all the commands to be executed over one SFTP connection, then
Execute sftp using that temporary text file as "batch file"
Your script would do something like:
echo "Directory in which to go:"
read directory
temp=$( mktemp /tmp/FOOXXX )
echo "" > $temp
echo "cd $directory" >> $temp
# other commands
sftp -b $temp $user#$host
rm $temp
If you are trying to change the directory of the machine, try lcd
In what way is it not working? To change directories on the remote server, you use the "cd" command. To change directories on the local server, you use the "lcd" command. Read "man sftp".

Resources