Couchdrop Blog

How to move files using SFTP in the command line

Written by Dallen Clark | Jan 28, 2025 9:28:08 PM

Moving files through SFTP involves three key commands: get, put, and rename. With SFTP clients, these commands can be selected through the GUI, but you can also run them from the command line. And sometimes, the command line is your best or only option.    

What is SFTP and why should you use it?

SFTP (Secure File Transfer Protocol) moves files between a local directory and a remote directory using an encrypted SSH tunnel. Unlike FTP--which is unencrypted--SFTP protects your data during transmission, making it essential for business file transfers.

Think of it like a bridge between two locations. With FTP, the bridge is open, and anyone looking at it can see what's moving across, and with the right tools, intercept and take it. SFTP is more like an enclosed tunnel with a password-protected gate. Anyone without the right access can't intercept what's moving, let alone see what it is, unless they have the right code. 

Moving files via SFTP is one of the most secure methods, as well as having some of the widest available support. Most electronic devices with network capabilities can connect using SFTP in some way, meaning you can move files from thousands of platforms and devices in a secure method. 

For more details about SFTP and what it is, see our article What is SFTP and how are FTP, SFTP, SCP, and Rsync different?

Moving files with the SFTP command

Using a command line program, you can access an SFTP server using the SFTP command like this: 

sftp username@hostname 

You'll then be asked for the password. Enter the password when prompted to connect. After successfully connecting, you'll see a prompt like this:

sftp>

This means you have a successful connection and can now use SFTP command to move files. Using this, you can download, upload, and rename files on both the local and remote hosts from only the command line. 

 

How do you download files using SFTP Get?

The get command downloads files from a remote server to your local system.

Basic syntax:

get remoteFile localFile

This will download a copy of the file [remoteFile] and rename it to [localFile]. 

Common get operations:

Command Example Description Actions
get file.txt Download single file Downloads 'file.txt' to the current directory
get -r /Documents Download entire directory Downloads all items in remote 'Documents' directory into the active local directory
get -Pr /Documents Download entire directory + permissions Downloads all items in remote 'Documents' directory into the active local directory and retains permissions
mget *.txt Download multiple files Downloads all .txt files in current remote directory

 

How do you upload files using SFTP Put?

The put command works opposite of the get command. It uploads files from your local system to the remote server.

Basic syntax:

put localFile remoteFile

Common put operations:

Command Example Description Actions
put file.txt Upload single file Uploads 'file.txt' to current remote directory
put -r /Documents Upload entire directory Uploads entire 'Documents' library and its contents to current remote directory
mput *.pdf Upload multiple files Uploads all PDF files in current local directory to current remote directory

 

How do you move files using SFTP Rename?

While at first glance, the rename command might not seem like it has anything to do with moving files, you can move files by using the command to specify a new path. This command operates on the remote directory. 

Basic syntax:

rename oldpath newpath

Common rename operations:

Command Example Description Actions
rename old.txt new.txt Rename file Changes file name of 'old.txt' to 'new.txt' in the active remote directory
rename ./docs/file.txt ./archive/newfile.txt Rename file and move it between directories Relocates 'file.txt' to new directory and renames it to 'newfile.txt'
rename folder1 folder2 Rename directory Renames 'folder1' in current directory to 'folder2'

How do you move directories in SFTP?

Moving directories requires special attention to preserve structure and permissions, and you can use or combine the above commands to move entire directories. 

Using SFTP Get for directory downloads

Download entire directories with the recursive flag (-r):

get -r /remote/directory/ /local/destination/

Common get directory operations:

Command Example Description Actions
get -r docs Basic directory download Download entire 'docs' folder
get -rp reports Download with permissions Download entire 'reports' folder and preserve file permissions
get -r * Download remote directory and all sub-directories Downloads the active remote directory and all subfolders into the current active directory
get -r * backup Download remote directory and all sub-directories to a specified directory Downloads the active remote directory and all subfolders into a folder called 'backup' 

Using SFTP Put for directory uploads

You can upload directory structures with the recursive flag too:

put -r /local/directory/ /remote/destination/

Common put directory operations:

Command Example Description Actions
put -r project Upload entire directory Uploads active local directory into remote 'project' folder
put -rp config Upload with permissions Uploads active local directory and its permissions into remote 'config' directory
put reports/*  Upload contents only Uploads contents of the 'reports' folder within the active local directory into  active remote folder

Using SFTP Rename for directory movement

Rename or move directories on the remote server:

rename oldpath newpath

Key directory movement patterns:

Command Example Description Actions
rename docs-old archive Rename object  Renames file/folder called 'docs-old' to 'archive' in active remote directory
rename video/project_v3 finals/video_final Renaming specified directory when destination directory exists Deletes folder 'project_v3' and moves contents to  'video_final' folder in 'finals'
rename video/project_v3 finals/new_folder Renaming specified directory when destination directory does not exist Deletes folder 'project_v3' and creates folder with contents in 'new_folder' within 'finals'

File moving automation

Automations can technically be done in the command line, but the setup is more complex and requires some technical skills such as scripting. For complex automation, it's generally better to use an interface to get them set up quickly and more reliably with a platform such as Couchdrop. 

How do you handle common SFTP errors?

Running into problems when moving files with SFTP using the command line can be especially frustrating because you aren't always told a reason why an operation didn't work. A lot of times, you'll simply get a 'Failure' message and that's it. 

If your transfers aren't going through, there are a few things you can check. For any of these errors, you can check the transfer logs to find out about what the specific issue is. 

Permission issues

You can't move a file if you don't have the right permissions. Check that the User you're using to connect to the server has valid permissions. The User should have read and write access to be able to see folders and then move or rename them. 

Connection problems

A bad connection can also stop you from being able to move files. Network problems are the most straightforward, but you can also be blocked by not having access to the required ports (port 22 by default) or you could be blocked from access by firewall rules. 

Space and transfer limits

Another common problem is that there are transfer limits in place or you've run out of disk space. You can check disk space using the df -h  command (-h makes it a human-readable format). If you have enough space, you could be hitting a maximum transfer quota or working with a file that is too large. 

Use Couchdrop and avoid SFTP command line entirely


While the SFTP command line can handle basic file and directory transfers, it's difficult to scale and involves a lot of manual effort. This isn't an issue for single transfers where you already have an SFTP server to connect to, but if you need to do automation with several steps or need an SFTP server, Couchdrop is a much stronger option. 

With easy setup and the ability to connect to your existing servers and storage, Couchdrop simplifies SFTP by providing a fast and secure way to transfer files. Automations require no code and you can easily use SFTP to move files and transform them in the process, such as adding powerful PGP encryption.  You'll also have access to detailed but simple reports without having to comb through logs to troubleshoot an issue if something comes up. 

Register for a free 14-day trial today to see how Couchdrop can work for you.