FTP Commands

The ftp command that runs on a Mac is similar to that from any other platform, including Windows – and not much has changed with regard to FTP for a long, long time. When using FTP you will login to an FTP server, then issue some commands, one of which will kill your session to the host. The commands you issue during an FTP session are issued in an interactive mode of the shell, where you are actually running them against the target server

  • ls – list the contents of a directory on the FTP server
  • cd – change the working directory on the FTP server
  • pwd – show the current directory on the FTP server
  • get – download files from the FTP server
  • put – upload files to the FTP server
  • account – include a password with your login information
  • bye – terminate an ftp session and close ftp (or use disconnect to simply terminate a session)
  • bell – make a cute sound after each file transfer is done
  • chmod – change permissions
  • delete – your guess is as good as mine (OK, you got me, it’s to delete a file off the server)
  • glob – enable globbing
  • hash – only functional in Amsterdam
  • help – get help
  • lpwd – print the local working directory for transfers
  • mkdir – create folders on the FTP server
  • rmdir – delete folders from the FTP server
  • newer – only get a file if it’s newer (great for scripting synchronizations)
  • nmap – use positional parameters to set filenames
  • passive – use FTP passive mode
  • prompt – allows the use of letters to automate answers to prompts
  • rate – limit the speed of an upload or download

Notice the similarity between FTP commands and Mac OS X commands! This continues into repetition: in Mac OS X you can build commands to run automatically when you open a new shell. Using a netrc file you can do the same with opening an FTP session. The ~/.netrc file (or just netrc) is a text file that, like .bash_profile, lives in your home directory (aka – ~). The .netrc file allows FTP to perform automatic logins to FTP servers based on the name (not that there aren’t subcommands to do the same, but it allows you not to insert the password into your history or script).

Like .bash_profile, the .netrc will need to be created per user, which can be done with the following:

touch .netrc

Because you’re going to put password information in there, let’s also restrict who can look at it:

chmod 700 .netrc

Now open your shiny new .netrc file and create a block of settings for each FTP
server you want to automate access for. This information will be the ftp server (machine), the username (login) and the password (yup, that’s the password). For example, the entire file could be:

machine https://krypted.com/
login ftpkrypted
password kryptedsupersecrethax0rpassword

You can also connect to a server using the following format with each command with each command:

ftp://username:password@servername:port

Now that you can login without issue, let’s write a script to perform some routine tasks, such as keeping a web site up-to-date.

#!/bin/bash
ftp -d https://krypted.com/ << ftpEnd
prompt
cd /Library/WebServer/Documents
put “*.html”
put “*.php”
cd /Library/WebServer/Documents
put “*.png”
quit
ftpEnd

Or Downloading documents off of a website:

#!/bin/bash
ftp -d https://krypted.com/ << ftpEnd
prompt
cd /My/Documents
get “*.doc”
quit
ftpEnd

There are also some variables that you can use in the prompt:

  • %/ – the current working directory of the FTP server
  • %M – the hostname of the FTP server
  • %m – the hostname only up to the .
  • %n – the username used for the FTP server

Finally, you can also define macro’s in your .netrc file using macdef.