How To Transfer File With Stfp Command.
In this tutorial, we will show you how to use the Linux sftp command.
What is SFTP:
SFTP (SSH File Transfer Protocol) is a secure file protocol that is used to access, manage, and transfer files over an encrypted SSH transport.
Before you Begin.
To be able to transfer files via SFTP you must have write permission on the remote system.
When transferring large files, it is recommended to run the sftp command inside a screen or tmux session.
The directory from where you run the sftp command is the local working directory.
Don’t confuse SFTP with FTPS. Both protocol serve the same purpose. However, FTPS stands for FTP Secure, and it is an extension to the standard FTP protocol with support for TLS.
Establishing an SFTP connection
SFTP works on a client-server model. It is a subsystem of SSH and supports all SSH authentication mechanisms.
To open an SFTP connection to a remote system, use the sftp command followed by the remote server username and the IP address or domain name:
If you are connecting to the host using password authentication, you will be prompted to enter the user password.
Once connected, you will be presented with the sftp prompt, and you can start interacting with the remote server:
If the remote SSH server is not listening on the default port 22 , use the -P option to specify the SFTP port:
Most of the SFTP commands are similar or identical to the Linux shell commands.
To get a list of all available SFTP commands, type help, or ?.
This will output a long list of all available commands, including a short description of each command:
Navigating with SFTP
When you are logged in to the remote server, your current working directory is the remote user home directory. You can check that by typing:
To list the files and directories, use the ls command:
To navigate to another directory, use the cd command. For example, to switch to the /tmp directory you would type:
The above commands are used to navigate, and work on the remote location.
The SFTP shell also provides commands for local navigation, information and file management. The local commands are prefixed with the letter l.
For example, to print the local working directory, you would type:
Transferring Files with SFTP
SFTP allows you to transfer files between two machines securely.
If you are working on a desktop machine, you can use a GUI SFTP client like WinSCP or FileZilla to connect to the remote server and download or upload files.
The sftp command is useful when you work on a server without GUI, and you want to transfer files or perform other operations on the remote files.
Downloading Files with the SFTP Command
To download a single file from the remote server, use the get command:
The output should look something like this:
When downloading files with sftp, the files are downloaded to the directory from which you typed the sftp command.
If you want to save the downloaded file with a different name, specify the new name as the second argument:
To download a directory from the remote system, use the recursive -r option:
If a file transfer fails or is interrupted, you can resume it using the reget command.
The syntax of reget is the same as the syntax of get:
Uploading Files with the SFTP Command
To upload a file from the local machine to the remote SFTP server, use the put command:
The output should look something like this:
If the file you want to upload is not located in your current working directory, use the absolute path to the file.
When working with put you can use the same options that are available with the get command.
To upload a local directory, you would type:
To resume an interrupted upload:
File Manipulations with SFTP
Typically, to perform tasks on a remote server, you would connect to it via SSH and do your work using the shell terminal. However, in some situations, the user may have only SFTP access to the remote server.
SFTP allows you to perform some basic file manipulation commands. Below are some examples of how to use the SFTP shell:
Get information about the remote system’s disk usage :
Create a new directory on the remote server:
Rename a file on the remote server:
Delete a file on the remote server:
Delete a directory on the remote server:
Change the permissions of a file on the remote system:
Change the owner of a file on the remote system:
You must supply the user ID to the chown and chgrp commands.
Change the group owner of a remote file with:
Once you are done with your work, close the connection by typing bye or quit.
In this tutorial, we have shown you how to use the sftp command to download and upload files to your remote SFTP server.