Emacs, scripting and anything text oriented.

Stuff about command-line ftp

Kaushal Modi

To connect to a machine with ip MACHINE, you simply need to enter the below at command-line:

ftp MACHINE

See below for a list of commands you would typically use once you ftp to a machine.

Basic FTP Commands #

CommandDescription
? or helpGet help on FTP commands
bye or quitExit the FTP session
binarySet the mode of file transfer to binary
(Provides less chance of transmission error)
cd, ls, mkdir, pwd, rmdirUnix operations on remote machine
deleteUnix rm operation on remote machine
lcdUnix cd operation on local machine
put FILECopy 1 file from local to remote
mput FILES (ex: mput *)Copy multiple files from local to remote
get FILECopy 1 file from remote to local
mget FILES (ex: mget*)Copy multiple files from remote to local

Automating FTP logins for specific IPs #

If you find yourself logging into a specific MACHINE too frequently, and if you want to save yourself from having to enter the username and password each time, you can save those credentials to your ~/.netrc file. See the example at the end of this post.

If that file does not exist, then create it, and remember to set its permission to 600.

touch ~/.netrc
chmod 600 ~/.netrc

If the permissions are not set so, you will get this ftp error:

Error - .netrc file not correct mode.
Remove password or correct mode.

Ignore the credentials in .netrc #

But at times, you might need to manually log into a machine with a different set of credentials than the one saved in the ~/.netrc. To do so, run ftp with the -n option. And then use the quote USER and quote PASS commands to pass in the username and password.

> ftp -n <machine>
ftp> quote USER <username>
ftp> quote PASS <password>

Commenting out stuff in .netrc #

Unfortunately, .netrc does not seem to have a comment syntax. But this hack works ..

Using the # character quickly followed by an FTP command foo (no space, ex: #foo) is one way to “comment out” lines with valid .netrc keywords.

You cannot have such ‘comments’ after valid .netrc keywords are used.

So if you need to comment out a set of logins and passwords, do that before the first valid set of uncommented login credentials. See below for an example of that.

An Example ~/.netrc #

#machine foo.bar.com
#login username1
#password pAsSw0rd1

machine foo.bar.com
login username2
password pAsSw0rd2

References #