SFTP stands for secure File Transfer Protocol, which help to transfer files over network in a secure / encrypted fashion. It is mainly used when you have to upload or download a file to a remote machine on network. It is similar to SCP, however it gives you more options to navigate ie cd , mkdir , changing file permission, ls etc while working with file.
$ sftp 1.244.344.444
Now you are connected to remote server and do ls and list remote file (can use other commands as well)
Append “l” in front of command to list local files
eg. $ lls => local list
$lpwd => local present working directory
$lmkdir => make directory in local box
$ get filename => will download from remote to local directory
$ put filename => will upload from local to remote directory
Detailed Example :
1. How to Connect to SFTP
By default, same SSH protocol is used to authenticate and establish a SFTP connection. To start an SFTP session, enter the username and remote hostname or IP address at the command prompt. Once authentication successful, you will see a shell with an sftp> prompt.
[root@tecmint ~]# sftp email@example.com Connecting to 220.127.116.11... firstname.lastname@example.org's password: sftp>
2. Getting Help
Once, you in the sftp prompt, check the available commands by typing ‘?‘ or ‘help‘ at command prompt.
sftp> ? Available commands: cd path Change remote directory to 'path' lcd path Change local directory to 'path' chgrp grp path Change group of file 'path' to 'grp' chmod mode path Change permissions of file 'path' to 'mode' chown own path Change owner of file 'path' to 'own' help Display this help text get remote-path [local-path] Download file lls [ls-options [path]] Display local directory listing ln oldpath newpath Symlink remote file lmkdir path Create local directory lpwd Print local working directory ls [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory put local-path [remote-path] Upload file pwd Display remote working directory exit Quit sftp quit Quit sftp rename oldpath newpath Rename remote file rmdir path Remove remote directory rm path Delete remote file symlink oldpath newpath Symlink remote file version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell ? Synonym for help
3. Check Present Working Directory
The command ‘lpwd‘ is used to check the Local present working directory, whereas ‘pwd‘ command is used to check Remote working directory.
sftp> lpwd Local working directory: / sftp> pwd Remote working directory: /tecmint/
- lpwd – print the current directory on your system
- pwd – print the current directory on the ftp server
4. Listing Files
Listing files and directories in local as well as remote system.
5. Upload File
Put single or multiple files in remote system.
sftp> put local.profile Uploading local.profile to /tecmint/local.profile
6. Upload Mutiple Files
Putting multiple files on in remote system.
sftp> mput *.xls
6. Download Files
Getting single or multiple files in local system.
sftp> get SettlementReport_1-10th.xls Fetching /tecmint/SettlementReport_1-10th.xls to SettlementReport_1-10th.xls
Get multiple files on a local system.
sftp> mget *.xls
Note: As we can see by default with get command download file in local system with same name. We can download remote file with different name specifying the name at the end. (This applies only while downloading single file).
7. Switching Directories
Switching from one directory to another directory in local and remote locations.
sftp> cd test sftp>
sftp> lcd Documents
8. Create Directories
Creating new directories on local and remote locations.
sftp> mkdir test
sftp> lmkdir Documents
9. Remove Directories
Remove directory or file in remote system.
sftp> rm Report.xls
sftp> rmdir sub1
Note: To remove/delete any directory from remote location, the directory must be empty.
10. Exist sFTP Shell
The ‘!‘ command drop us in local shell from where we can execute Linux commands. Type ‘exit‘ command where we can see sftp> prompt return.
sftp> ! [root@sftp ~]# exit Shell exited with status 1 sftp>