How To Connect Via SFTP Within An SSH Client
Perhaps one of the least known tricks in migrating a website or a bunch of files to a new server is the ability to connect to a remote server via SFTP while inside of an active SSH session. The reason this is so handy is because many times a customer doesn’t have SSH access to another server, and also using alternative migration methods such as manually downloading/uploading or using a WordPress migration plugin is out of the question (large files, etc).
First, make sure you are inside an active SSH session on the “new” server that is pulling files. Then:
sudo sftp -P 2222 email@example.com
After establishing a connection for the first time, you will see the typical warning message that encrypted SSH/SFTP connctions always spit out, and simply type “yes” at the prompt and hit ENTER:
The authenticity of host '18.104.22.168 (22.214.171.124)' can't be established. ECDSA key fingerpring is 12:34:45:1a:aa:12:33:4c:87:65:43:21:bb:3c:fa:c0. Are you sure you want to continue connecting (yes/no)?
After you’ve successfully connected, there is the issue of browsing the remote server’s directories and just “getting around” the server itself. Thankfully, some of the basic SFTP commands are exactly the same as SSH commands, such as “list directory” or “change to directory” or “change to parent directory” as follows:
ls cd /directory cd ..
But things quickly get a bit goofy after that. Common SSH (shell) commands such as WGET, TAR, MV, etc simply will not work while browsing a remote server over SFTP. And since we have no benefit of a UI client such as FileZilla, knowing some SFTP commands is an absolute must.
Usually the best option is first change into the “destination” directory on the “new” server, such as /wp-content/ and then via the SFTP command known as get we can recursively pull over an entire directory full of files:
get -r uploads
After hitting this command you will see verbose list of each file coming across. When its finished, the SFTP prompt will return to normal, at which point you can exit but typing “exit” and ENTER.