How to transfer files to the server with SFTP command-line on Mac

FTP (File Transfer Protocol) is a network communication protocol used to transfer files between two computers (client-server mode). The following shows how to transfer files between your local computers and the linux server using secured FTP called SFTP (a variant of FTP), which comes with Terminal on Mac.

For example, to transfer html files from your computer to the directory /home/jdoe/public_html on the server, open a Terminal. Note "jdoe" is a sample username on the server.

"sftp -i "private_key_file" jdoe@cs1.txwes.edu" to establish a connection to the server and then enter your private key. Below is a sample for the connection. Note that if the private key was genereated with PuttyGen on Windows OS, it needs to be converted into OpenSSH format with PuttyGen for login from Mac.

TWU018870-MAC:Documents jdoe$ sftp -i "newServerPrivateKeyEdDSAOpenSSHFormat-2023-12-9.ppk" jdoe@cs1.txwes.edu
Enter passphrase for key 'newServerPrivateKeyEdDSAOpenSSHFormat-2023-12-9.ppk':
Connected to cs1.txwes.edu.
sftp> dir
courses         public_html     shell_scripts   tmp
sftp> pwd
Remote working directory: /home/jdoe/public_html/mcs/images
sftp> lpwd
Local working directory: /Users/jdoe
sftp> lcd Documents
sftp> lpwd
Local working directory: /Users/jdoe/Documents
sftp> !ls
s37.png							s47.png
s45.png							txwes
sftp> quit
TWU018870-MAC:Documents jdoe$
By default, you are directed to your home directory on the server. When transfering files between two computers, it is important to specify a source directory where files will be transfered from and a destination directory where files will be transfered to, that is, commands need to know where to transfer files from and to. To transfer files to the web directory "public_html," you need to set it as the destination directory by typing a command "cd public_html" and use command "lcd" to set a source directory on your local computer where the files will be transferred from. "l" in the command "lcd" means local (your local computer). The commands on this PSFTP console window can apply to either local or remote computers.

If you want to transfer a file named "index.html" from C:\Users\Jdoe\Documents on your local machine to the web directory "public_html" on the server. First, navigate to the source directory using "lcd" as below.

lcd \Users\jdoe\Documents

Use command "put" to transfer files to the server.

sftp>put index.html

If you want to transfer multiple files with a single command, you can use

sftp> mput *.png
Uploading s37.png to /home/jdoe/public_html/mcs/images/s37.png
s37.png                                                                                             100%  205KB 634.4KB/s   00:00
Uploading s41.png to /home/jdoe/public_html/mcs/images/s41.png
s41.png                                                                                             100% 1065KB   1.8MB/s   00:00
to transfer all the files with extension of html. * is a wild card meaning "don't care" the file names. If you want to download files to your local machine from the remote server, you can use
sftp>get index.html
"mget" is for getting multiple files. For help, type "help"
sfpt>help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
chown [-h] own path                Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-afpR] remote [local]         Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-afpR] local [remote]         Upload file
pwd                                Display remote working directory
quit                               Quit sftp
reget [-fpR] remote [local]        Resume download file
rename oldpath newpath             Rename remote file
reput [-fpR] local [remote]        Resume upload file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

In a netshell, the sftp command-line tool is very powerful and useful for file transfer between client and server machines. Once you specify a source directory and a destination directory, you can use a single command to transfer multiple files easily, especially for uploading or downloading an entire website with thousands of web files.