SFTP Command with Examples to Transfer Files on Remote Server in Linux
Disclaimer: Accessing the information on this page means you agree to the Sites Privacy Policy & Terms of Use Agreement.
SSH File Transfer Protocol shortly called SFTP. This works with ssh protocol (default port 22) for transfer files securely between two systems. I will explain in this article that how to use SFTP command line on Linux operating system.
How to connect the SFTP server/system ?
Please open your terminal and run below command to connect sftp server. To connect remote host using sftp, you need sftp user and hostname or IP Address of the remote server (host).
sftp user@remotehost.IP
e.g. sftp rootlinux@192.168.1.20
Navigate the current working directory:
To navigate the directories on SFTP, use command “pwd”. This will display the current working directory of SFTP server.
sftp> pwd
Remote working directory: /
If you want to see the local working directory then run below command:
sftp> lpwd
Local working directory: /home/ubuntu/
Listing Files
You can use “ls” command to list the files and directory of Remote server.
sftp> ls
To display local files and directory use below command:
sftp> lls
Switching Directories
You can change then directory using “cd” command.
sftp> cd work
You can change the parent directory using “cd ..” command.
sftp> cd ..
How to upload files to the SFTP Server?
If we want to upload same files under work directory then first change the current working directory followed by the destination directory as below:
sftp> cd works
sftp> pwd
Remote working directory: /works
If you want to change then local directory path then run below command:
sftp> lcd /home/ubuntu
sftp> pwd
Local working directory: /home/ubuntu/
How to create directory in SFTP?
Creating directory on remote server:
sftp> mkdir /works/test
Create directory on local system:
sftp> lmkdir /home/ubuntu/documents
How to remove files or directory on the remote system using SFTP?
To remove file run below command:
sftp> rm documents.docs
To remove file run below command:
sftp> rmdir works
How to copy/upload files on SFTP server ?
Now if you want to upload some files, for example, we have help.txt on Local Computer then run below command:
sftp> put help.txt
To put multiple files run below command:
sftp> mput *.txt
We can provide the absolute path of the local file and remote directory without navigating directories like below:
sftp> put /home/ubuntu/help.txt /works/
Help : put <source/local path> <Destination/remote path>
How to download files from the SFTP Server?
To download the files from the sftp server then use “get” command. Please see below example for the “get” command of SFTP:
sftp> get help_remote.txt
To download multiple files on local computer run below command:
sftp> mget help_remote.txt
Similarly, we can provide and absolute path to download files from SFTP server.
sftp> get /works/help_remote.txt /home/ubuntu/
Help : get <source path> <Destination/local path>
How to exit the SFTP connection ?
There are 3 commands to exit the SFTP connection.
sftp> bye
sftp> quit
sftp> exit
This article is contributed by RootLinuxBlog. If you
like RootLinuxBlog and would like to contribute, you can submit an article
using contact us from. See your article appearing on the RootLinuxBlog main
page and help other Techies.
Please write comments if you found any error
with the above article would really help us to serve you a better way. Thank
you…