0

How could I modify the script below to include the source directory so that it will know which location to retrieve the files required for transfer?

I have a bash script for the purpose of automatically transferring files to a remote server, which occurs once a week.

Here is the script:

#!/bin/bash

HOST=sftp.mydomain.com  
USER=user           
PASS=pass          

ftp -inv $HOST << EOF
$USER $PASS
cd /d D:\destination\directory
put file1.gz
put file2.gz
put file3.gz

bye

EOF

Files 1-3 are all in source/directory, so I'd like to transfer all files from source/directory to the destination path specified above.

However, I believed that because the script is running on the source VM, that I wouldn't need to specify a directory since the script could simply pull it from any folder (rookie mistake, I understand).

Alternatively: is there an easier way to use ftp for an entire directory as opposed to simply listing the entire content of the folder?

8
  • Given that the target host is named sftp might it be that you could use an ssh-based protocol instead of the ancient and insecure ftp? If this is the case you can look at commands using sftp such as sftp and lftp, and even consider scp with certificates, which are very much easier to manage than unsecured passwords. Commented Dec 31, 2019 at 15:25
  • The cd command only takes a single argument, no? To transfer the complete contents of a directory hierarchy using FTP, transfer it as a tar archive.
    – Kusalananda
    Commented Dec 31, 2019 at 15:26
  • 1
    @roaima I have attempted to use scp and other ssh-layered attempts previously, but I was met with considerable resistance and a plethora of errors which I was unable to resolve - I resigned myself to a much simpler method; however, I have configured the ftp server to use tls certificates for some extra protection Kusalananda - I have not attempted this, but I will try to do so - thank you Commented Dec 31, 2019 at 15:32
  • do you just need an lcd inside the ftp script, or a plain old cd before the ftp?
    – Jeff Schaller
    Commented Dec 31, 2019 at 15:35
  • @Jeff Schaller As this is a remote server, I believe that cd would be the more appropriate option, but if you're able to convince me otherwise, I am open to any changes. Thanks. Commented Dec 31, 2019 at 15:50

1 Answer 1

1

To change which directory the ftp process sees as the source, either cd there beforehand or lcd there within:

1.

cd /source/directory
ftp ...

2.

ftp ...
lcd /source/directory
...

lcd (from man ftp) is short for local change directory; it will:

Change the working directory on the local machine.

"Local" here means the system that you ran ftp from.

To put all files from the current local directory to the current remote directory in ftp, use:

prompt
mput *

The prompt FTP command disabled interactive prompting during "multiple" operations, such as mget or mput. Quoting from the FTP man page (or see your local man ftp):

Interactive prompting occurs during multiple file transfers to allow the user to selectively retrieve or store files.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .