ftp - macOS


Overview

The ftp command in macOS is used to transfer files between a client and a server using the File Transfer Protocol (FTP). It allows users to upload, download, and manage files on a remote server. This command is useful in situations where secure file transfer is not required, such as transferring public files or accessing archived data.

Syntax

The basic syntax of the ftp command is:

ftp [options] [hostname]
  • hostname specifies the FTP server to connect to.

Users can interact with the ftp program through a variety of commands once connected. To initiate the connection in an automated script, you could use:

ftp -inv [hostname] << EOF
command1
command2
...
EOF

Where -i disables interactive prompting, -n restricts automatic login, and -v enables verbose mode.

Options/Flags

  • -v: Verbose mode; displays all server responses which can be useful for debugging.
  • -d: Enables debugging mode, showing all the FTP commands passed between the client and the server.
  • -i: Turns off interactive prompting during multiple file transfers.
  • -n: Suppresses the automatic login upon initial connection.
  • -g: Disables filename globbing, which permits the use of wildcard characters in local file and path names.

Examples

1. Connecting to an FTP server:

ftp example.com

This command connects to the FTP server at example.com.

2. Downloading a file:

Once connected to the FTP server:

get filename.zip

This retrieves filename.zip from the server to your local machine.

3. Uploading a file:

After logging into the FTP server:

put localfilename.zip

This command uploads a file named localfilename.zip from your local machine to the server.

4. Using ftp in a non-interactive shell script:

ftp -inv example.com << EOF
user username password
cd /path/to/directory
get filename.zip
bye
EOF

This script logs in to example.com, changes the directory, downloads filename.zip, and then terminates the connection.

Common Issues

  • Plain Text Transmission: FTP does not encrypt its traffic; all transmissions are in plain text, including usernames, passwords, commands, and data. Consider using SFTP or FTPS for secure transmissions.
  • Connection Failures: Incorrect hostname, username, or password and network issues can lead to connection failures. Double-check credentials and network connectivity.
  • Firewall or Antivirus Interference: Ensure that FTP ports (usually 20 and 21) are open on all involved firewalls.

Integration

Combining with cron for scheduled backups:

For automating backups to an FTP server, schedule a cron job:

0 2 * * * /usr/bin/ftp -inv example.com << EOF
user username password
cd backups
put backupfile.tar.gz
bye
EOF

This cron job uploads backupfile.tar.gz to the server every day at 2 a.m.

Combining with grep for filtering files:

To download only certain files:

ftp -inv example.com << EOF
user username password
cd /path/to/files
ls | grep 'pattern' | awk '{print $9}' | xargs -I {} get {}
bye
EOF

This script logs into example.com, lists files, filters them by ‘pattern’, and downloads the filtered files.

  • sftp: Secure File Transfer Protocol, used for secure file transfer.
  • scp: Secure copy, used for copying files between hosts on a network.
  • curl: Command line tool and library for transferring data with URLs. It supports FTP, FTPS, HTTP, HTTPS, and more.

For more detailed information, you can consult the FTP man page by typing man ftp in your terminal or visit online FTP documentation.