SCP and SFTP: how to transfer files over SSH
Once you have SSH access to a server, you usually also have a way to transfer files. That access comes through two protocols: SCP (Secure Copy Protocol) and SFTP (SSH File Transfer Protocol). Both run over the same SSH connection, so no separate firewall rules or setup is required.
SCP: the simple one
SCP works like cp but for remote paths. The syntax is:
scp source destination
Remote paths use user@host:/path/to/file. To copy a file from your local machine to a server:
scp myfile.txt user@192.168.1.100:/home/user/
To copy from a server to your local machine:
scp user@192.168.1.100:/home/user/myfile.txt ./
To copy a directory, add -r:
scp -r myproject/ user@192.168.1.100:/var/www/
SCP is fast and requires no setup beyond an SSH connection. The downside is that it does not let you browse, rename, delete, or manage files. It is a one-shot copy tool.
It's also worth knowing that SCP uses an older underlying protocol that has been deprecated in OpenSSH 9.0 in favor of SFTP internally. The scp command still works, but on very new OpenSSH versions it uses SFTP under the hood anyway.
SFTP: the flexible one
SFTP opens an interactive session where you can navigate directories, transfer files in both directions, rename things, change permissions, and delete files.
sftp user@192.168.1.100
Once connected, you get an sftp> prompt. Common commands:
ls # list remote files
lls # list local files
cd /var/www # change remote directory
lcd ~/Downloads # change local directory
get remotefile.txt # download a file
put localfile.txt # upload a file
rm oldfile.txt # delete a remote file
exit # close the session
For a GUI SFTP client on Windows, FileZilla and WinSCP both work well. On macOS, Cyberduck is a popular option. These give you a file browser you can drag and drop between.
Choosing between them
If you need to move a specific file or directory and you know exactly what you want: use SCP. It is one command and you are done.
If you need to browse the server's filesystem, compare what's there with what you have locally, or do any kind of file management: use SFTP or a GUI SFTP client.
For scripting and automation, SFTP supports batch mode:
sftp -b batchfile.txt user@host
Where batchfile.txt contains SFTP commands, one per line.
rsync for large or repeated transfers
If you are syncing large directories or running the same transfer repeatedly, rsync is faster than either SCP or SFTP because it only transfers changed parts of files:
rsync -avz myproject/ user@server:/var/www/myproject/
rsync also runs over SSH by default (-e ssh is implicit in most setups). For things like deploying a website or backing up a directory to a remote server, it is the right tool.
File transfers and browser-based SSH
Tools like SSHDock provide terminal access over SSH in the browser. Direct SCP and SFTP file transfer from a browser is more limited than from a native client, but if you primarily need terminal access and occasionally need to move a file, the combination of a browser terminal plus a native SCP command from a local terminal covers most scenarios.
For an introduction to securing the connection you use for all of this, see our guide on how to lock down your SSH server.