How do I execute FTP commands on one line?

I found this thread when I was searching for a way to have a single ftp command execute a file transfer from this machine to the ftp server. Here is how:

Create a file with the ftp commands in it: (call it 'ftpcommands.txt')

 open YourFtpServer.com
 user YourUserName YourPassword
 put localfilename remotefilename
 bye

Then run the ftp command and feed the file into it:

 ftp -n < ftpcommands.txt

The -n option keeps ftp from trying to log in automatically when it receives you 'open' command.

Hope THAT helps someone. I couldn't find anything online that was this solution, so I had to figure it out myself.


That really is more of a job for SSH (as others have pointed out), but if you're determined to use ftp, try lftp. It's freely available for all currently supported versions of Ubuntu. You can install it with the command sudo apt install lftp

lftp -u username,password -e "your command;quit" ftp.site.com

lftp documents a -c switch that runs the command and then quits, but it appears to be broken in most distributions. -e will keep you connected unless you issue a quit.


You can't use FTP for executing commands remotely. It stands for File Transfer Protocol. What you actually need is SSH.

All you need to do is install the package ssh on both machines and then follow this guide to set up password-less logins.

And now how to call it:

ssh username@host echo "Hello World\!"

For example, this is me doing it to myself:

nick@AccessDenied:~$ ssh nick@localhost echo "Hello World\!"
Hello World!