How to Use SFTP Command to Transfer Files
Introduction
Before we begin talking on how to use sftp command to transfer files. Let’s briefly understand - What is an SFTP command?
SFTP (Secure File Transfer Protocol), is a command-line tool used for secure file transfers between a local host and a remote server. It operates over the SSH (Secure Shell) protocol, providing encrypted and authenticated connections.
When using the SFTP command, you can perform various operations, including uploading and downloading files, navigating directories, creating directories, deleting files, and modifying file permissions.
In this tutorial, we will provide an overview of how to use the SFTP command to transfer files securely between your local system and a remote server. We will also address a few FAQs on how to use sftp command to transfer files.
Before you Begin
You need to write permission on the remote system in order to transfer files through SFTP.
The sftp
command should always be used inside a screen or tmux session when transferring large files.
The directory from which you run the sftp
command is called the local working directory.
Establishing an SFTP connection
SFTP is a client-server protocol. It is a component of SSH and is compatible with all of its authentication mechanisms.
Use the sftp
command along with the username for the remote server, the IP address, or the domain name to establish an SFTP connection to a remote system:
sftp remote_username@server_ip_or_hostname
You will be prompted to enter the user password if you are connecting to the host using password authentication.
Once connected, the sftp
prompt will show up, allowing you to interact with the remote server:
Output
Connected to remote_username@server_ip_or_hostname.
sftp>
If the remote SSH server is not listening on the standard port 22, use the -P
option to set the SFTP port:
Output
sftp -P custom_port remote_username@server_ip_or_hostname
SFTP Commands
The majority of SFTP commands are similar to or equivalent to Linux shell commands.
Type help
or ?
to view a list of all the SFTP commands that are available.
help
This will produce a lengthy list of all commands that are accessible, along with a brief description of each command:
Output
Available commands:
bye Quit sftp
cd path Change remote directory to 'path'
...
...
version Show SFTP version
!command Execute 'command' in local shell
! Escape to local shell
? Synonym for help
Navigating with SFTP
The remote user's home directory is your current working directory when you are logged into the remote server. You can verify that by typing:
pwd
Output
Remote working directory: /home/remote_username
Use the ls
command to list the files and directories:
ls
Use the cd
command to go to another directory. For instance, you would enter the following to navigate to the /tmp
directory:
cd /tmp
The commands listed above are used to navigate and work on the remote location.
The SFTP shell also includes commands for managing files, information, and local navigation. Local commands begin with the letter l
.
lpwd
Output
Local working directory: /home/local_username
Transferring Files with SFTP
You would type the following, for instance, to print the local working directory:
You can send files between two machines safely using SFTP.
When working on a desktop system, you can connect to the remote server and download or upload files using a GUI SFTP client like WinSCP or FileZilla.
When working on a server without a GUI and wanting to transfer files or carry out other operations on remote files, the sftp
command is helpful.
Downloading Files with the SFTP Command
Use the get
command to download a single file from the remote server:
get filename.zip
The output would appear as follows:
Output
Fetching /home/remote_username/filename.zip to filename.zip
/home/remote_username/filename.zip 100% 24MB 1.8MB/s 00:13
When using sftp
to download files, the files are downloaded to the directory from which the sftp
command was typed.
If you want to save the downloaded file under a different name, specify the new name as the second argument:
get filename.zip local-filename.zip
Use the recursive -r
option to download a directory from the remote system:
If a file transfer fails or is interrupted, use the reget
command to resume it.
get -r remote_directory
The syntax for both get
and reget
is the same:
reget filename.zip
Uploading Files with the SFTP Command
Use the put
command to upload a file from the local system to the remote SFTP server:
put filename.zip
The output should resemble this:
Output
Uploading filename.zip to /home/remote_username/filename.zip
filename.zip 100% 12MB 1.7MB/s 00:06
Use the absolute path of the file you want to upload if it is not located in your current working directory.
You can use the same options provided with the get
command when working with put
.
Type the following to upload a local directory:
put -r locale_directory
To continue an interrupted upload:
reput filename.zip
File Manipulations with SFTP
Normally, you would connect to a remote server through SSH and carry out your tasks using the shell terminal. However, in other cases, the user may only have SFTP access to the remote server.
You can run a few simple file manipulation commands using SFTP. Here are a few examples of how to use the SFTP shell:
- Obtain information about the disk usage by the remote system:
- Make a new directory on the remote server:
- Change the name of a file on a distant server:
- Remove a file from the remote server:
- Remove a directory from the remote server:
- Changing a file's permissions on a remote system:
- Changing a file's owner on a remote system:
df
Output
Size Used Avail (root) %Capacity
20616252 1548776 18002580 19067476 7%
mkdir directory_name
rename file_name new_file_name
rm file_name
rmdir directory_name
chmod 644 file_name
chown user_id file_name
The user ID is required when using the chown
and chgrp
commands.
- Change a remote file's group owner by using:
chgrp group_id file_name
Type bye
or quit
to end the connection when you are done.
FAQs to Install FFmpeg on CentOS 7
How does SFTP ensure security during file transfers?
SFTP uses SSH encryption and authentication protocols to secure the connections and protect file transfers from unauthorized access or interception.
How do I access SFTP on my local system?
Open a terminal or command prompt and type sftp [user@]host
where "user" is your username and "host" is the remote server's hostname or IP address.
How do I navigate directories using SFTP?
Once connected, you can use commands like cd
to change directories, ls
to list files, and pwd
to show the current directory on the remote server.
Can I transfer multiple files at once using SFTP?
Yes, you can transfer multiple files by specifying their names after the put
or get
command, separated by spaces.
How do I delete files from the remote server using SFTP?
Within the SFTP session, use the rm
command followed by the file's name or path to delete files from the remote server.
Is it possible to automate file transfers with SFTP?
Yes, you can create script files using the SFTP commands and automate file transfers by running the script.
Can I use SFTP to transfer files between remote servers without involving my local system?
Yes, if you have SSH access to both remote servers, you can use SFTP to transfer files directly between them without involving your local system.
Conclusion
In this tutorial, you have learned how to use the sftp
command to download and upload files to your remote SFTP server.
You may also want to configure SSH key-based authentication so that you can login to your Linux servers without typing a password. When connecting to the same systems frequently, you can streamline your workflow by listing all of your connections in the SSH config file.
If you have any queries, feel free to drop a comment below, and we'll be happy to help.