I have a requirement where I need to automate the transfer of a particular file from one location in the application server to the database server.

I can do it manually by using these steps but I need it in the script

#! /bin/ksh

directory path where the file need to be

ftp (hostname of the application server)



file location in the apps server

get filename


Several FTP clients allow you to specify username a password on the command line, two that I know of are ncftp and lftp. You can then use a single command, non-interactive, to retrieve the file inside the script.

  • Thanks john for you response , I really appreciate for your time..... I tried using this in a script but it is asking me to enter username and password.... how can i automate and can execute the script without the promt of username and password – sam Sep 20 '13 at 13:55
  • Don't use the standard ftp client. As I said, use lftp or ncftp or another client that lets you pass the username and password on the command line. When you do so, you don't get prompted. – John Sep 20 '13 at 14:27

I got it using the following:

ftp -inv <<EOF
open app.server.address
user username password
cd some/location
get filename

The ftp -i option turns off interactive prompting and -n disables auto-login along with the username and password prompts.

  • 1
    @sam The << was interpreted as an opening html tag, causing the main part of your post to be hidden. I think this is what caused Anthon to believe your post was merely a "thank you". Fixed it for you, but in the future you can avoid it by indenting code blocks by four spaces. – Thomas Nyman Sep 20 '13 at 17:43

If you want to avoid storing the username and password in the script itself you can create an entry for the target host in ~/.netrc with the following form:

machine <address>
login <username>
password <password>

where you replace <address>, <login> and <password> with the host address, username and password respectively. You should also make sure that ~/.netrc has proper permissions (i.e. is only readable and writable by the owner). This approach has the advantage that the credentials are stored in a single place which can be accessed from multiple scripts. It is also easier to make sure ~/.netrc has proper permissions than to manage permissions for all scripts which might need to use the credentials.

Then you can proceed to either to pass commands to ftp via a here document or use lftp with the -c option to pass the commands as command line arguments. Note that if you go for the former, you should not specify the -n option to ftp as this will disable auto-login using the credentials stored in ~/.netrc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.