sftp command in Linux is used to securely transfer files between local and remote systems using the SSH (Secure Shell) protocol. It provides a secure alternative to FTP (File Transfer Protocol) for file transfer operations.
Here’s an overview of how to use the
- Connecting to a Remote Server:
To establish an SFTP connection to a remote server, use the following command:
username with your remote server username and
remote_host with the hostname or IP address of the remote server. For example:
- Interactive SFTP Shell:
Once connected, you’ll enter an interactive shell where you can issue various SFTP commands. The shell prompt will change to
sftp>, indicating that you are in the SFTP environment.
- Basic SFTP Commands:
ls: List files and directories in the current remote directory.
cd directory: Change the current remote directory.
pwd: Display the current remote directory.
get file: Download a file from the remote server to the local system.
put file: Upload a file from the local system to the remote server.
mkdir directory: Create a new directory on the remote server.
rmdir directory: Remove a directory from the remote server.
rm file: Delete a file from the remote server.
quit: Exit the SFTP session and return to the local shell.
- File Transfers:
To upload a file from the local system to the remote server, use the
putcommand followed by the filename:
local_file with the path to the file you want to upload.
To download a file from the remote server to the local system, use the
get command followed by the filename:
remote_file with the path to the file you want to download.
- Specifying Remote Paths:
You can specify the remote path for file transfers by prefixing the remote filename or directory with the desired path. For example, to upload a file to a specific remote directory:
put local_file remote_directory/
- Recursive File Transfers:
To transfer directories and their contents recursively, use the
-roption with the
getcommand. This ensures that all subdirectories and files are transferred:
put -r local_directory
- Connect to a remote server:
- Upload a file to the remote server:
- Download a file from the remote server:
- Change the current remote directory:
- List files and directories on the remote server:
- Exit the SFTP session:
sftp command provides a secure and reliable method for transferring files between local and remote systems using the SSH protocol. It supports various file transfer operations and provides an interactive shell for managing remote files and directories.
For more information about the
sftp command and its options, you can refer to the manual page by typing
man sftp in your terminal.