10 single line SFTP commands to transfer files in Unix/Linux


Written by - Deepak Prasad

SFTP stands for secure file transfer program similar to ftp. The only difference is that Unix or Linux SFTP command performs all operations over an encrypted SSH transport. It may also use many features of ssh, such as public key authentication and compression.

I have already written another article with the steps to setup SFTP server with passwordless login using authorized_keys in Unix and Linux. So I will use the same SFTP server to demonstrate single line SFTP commands to transfer files and directory from local to remote and remote to local server.

 

Some more articles on related topic to transfer files and folders between two servers:

 

Lab Environment

SFTP Server:
Hostname: sftp-server
IP Address: 192.168.43.83

SFTP Client:
Hostname: sftp-client
IP Address: 192.168.43.82

 

1. Copy file from remote server to local machine windows

We can use connect to our SFTP Server from Windows machine using any SFTP Client such as WinSCP, FileZilla etc. The SFTP syntax for destination may be specified either as [user@]host[:path] or as a URI in the form

sftp://[user@]host[:port][/path]

 

I will use WinSCP on my Windows Laptop to connect to our Linux SFTP Server. Provide the IP Address and other User credential to login to SFTP server using WinSCP
10 single line SFTP commands to transfer files in Unix/Linux

 

Once connected you can now copy file from remote server to local machine windows or vice versa.
10 single line SFTP commands to transfer files in Unix/Linux

Assuming the user deepak has enough privilege you should be able to copy file from remote server to local machine windows or vice versa.

 

2. Single line SFTP get command to download file from remote to local server

Use below SFTP syntax to download file from remote to local server using SFTP get command.

sftp [user]@host[:port][/dest_path/file] [/local_path]

 

I have dest_dir folder on sftp-server on which I will create a new file (sftp-server_file) and then we will download this file on sftp-client using SFTP get command.

[root@sftp-server dest_dir]# pwd
/opt/sftp-jails/deepak/dest_dir

[root@sftp-server dest_dir]# touch sftp-server_file

Connect to sftp-server using sftp-client and download sftp-server_file to /tmp on sftp-client using SFTP get command.

[root@sftp-client ~]# sftp deepak@sftp-server:dest_dir/sftp-server_file /tmp/
Connected to sftp-server.
Fetching /dest_dir/sftp-server_file to /tmp/sftp-server_file

Since I had configured passwordless sftp, we did not got any SFTP command line password prompt

 

3. Single line SFTP put command to upload file from local to remote server

Use below SFTP syntax with to upload file from local to remote server using SFTP put command.

sftp [user]@host[:port][/dest_path] <<< $'put /local_path/file'

 

I will create a file sftp-client_file under /tmp on sftp-client

[root@sftp-client ~]# touch /tmp/sftp-client_file

Upload /tmp/sftp-client_file to sftp-server using single line SFTP put command

[root@sftp-client ~]# sftp deepak@sftp-server:dest_dir/ <<< $'put /tmp/sftp-client_file'
Connected to sftp-server.
Changing to: /dest_dir/
sftp> put /tmp/sftp-client_file
Uploading /tmp/sftp-client_file to /dest_dir/sftp-client_file
/tmp/sftp-client_file

As you see the one line SFTP command line file upload was successful and since we are using SFTP authorized_keys, we did not got any SFTP command line password prompt

 

4. Single line SFTP commands to download directory from remote to local server

We will use the same SFTP syntax as we used for SFTP get command to download file from remote to local server with an additional SFTP argument

sftp -r [user]@host[:port][/dest_path] [/local_path]

 

I have created sftp-server_dir on sftp-server

[root@sftp-server dest_dir]# pwd
/opt/sftp-jails/deepak/dest_dir

[root@sftp-server dest_dir]# mkdir sftp-server_dir

Next we will use single line SFTP get command line to download directory from remote to local server under /tmp

[root@sftp-client ~]# sftp -r deepak@sftp-server:dest_dir/sftp-server_dir /tmp/
Connected to sftp-server.
Fetching /dest_dir/sftp-server_dir/ to /tmp/sftp-server_dir
Retrieving /dest_dir/sftp-server_dir

As you see the single line SFTP command line to download directory from remote to local server was successful and since we are SFTP authorized_keys, we did not got any SFTP command line password prompt.

Verify the directory on sftp-client we just downloaded under /tmp using SFTP command:

[root@sftp-client ~]# ls -ld /tmp/sftp-server_dir/
drwx------ 2 root root 4096 Apr 2 17:42 /tmp/sftp-server_dir/

 

Where does SFTP download to?

In the above SFTP usage example we had explicitly provided the path to download files and directory. By default the local directory under which you have executed SFTP will be the "local path" where SFTP will by default download the files to if you have not given any source directory.

 

SFTP Usage Example:

Below SFTP usage example will clear your doubt on "where does sftp download to"

[root@sftp-client ~]# sftp deepak@sftp-server
Connected to deepak@sftp-server. <-- Connected to sftp-server
sftp> cd exchange/ <-- Change destination directory on sftp-server to exchange
sftp> get file1 <-- SFTP get command to download sftp-server:exchange/file1 to sftp-client 
Fetching /exchange/file1 to file1  <-- This will get sftp-server:exchange/file1 under /root/ on sftp-client as we executed sftp from /root/

sftp> get file1 /tmp/ <-- SFTP get command to download exchange/file1 from sftp-server to /tmp/ on sftp-client
Fetching /exchange/file1 to /tmp/file1

sftp> lcd /home/rahul/ <-- SFTP lcd command to change local directory path to /home/rahul on sftp-client

sftp> get file1 <-- SFTP get command. This will download sftp-server:exchange/file1 under /home/rahul on sftp-client
Fetching /exchange/file1 to file1

sftp> lmkdir test_dir <-- SFTP lmkdir command to create directory on local server

sftp> mkdir sftp-server_dir <-- SFTP mkdir command to create directory on sftp-server

sftp> ls -l <-- List the files and directories on sftp-server
-rw-------    1 deepak   1003          800 Mar 31 17:20 checksum_datababse.out
-rw-r--r--    1 deepak   1003            0 Apr  2 16:10 file1
-rw-r--r--    1 deepak   1003            0 Apr  2 16:10 file2
-rw-r--r--    1 deepak   1003            0 Apr  2 16:08 file6
-rw-r--r--    1 deepak   1003            0 Apr  2 16:08 file7
-rw-------    1 deepak   1003           24 Mar 31 17:20 hello.txt
drwx------    2 deepak   1003         4096 Apr  3 17:27 sftp-server_dir
-rw-------    1 deepak   1003           59 Mar 31 17:20 upload_dir.ba

sftp> lpwd  <-- SFTP lpwd command to print present working directory on local server
Local working directory: /home

sftp> pwd <-- SFTP pwd command to print present working directory on remote server
Remote working directory: /exchange

sftp> df -h  <-- SFTP df command to check disk usage for shared path on remote server
    Size     Used    Avail   (root)    %Capacity
  13.3GB    6.3GB    6.3GB    7.0GB          47%

sftp> exit

We have used below commands in this SFTP usage example:

lcd    -> To change directory on local seever i.e. SFTP Client
lpwd   -> List present working directory on local server i.e. SFTP Client
lmkdir -> Create directory on local server i.e. SFTP Client
cd     -> Change directory on remote server i.e. SFTP Server
get    -> Download file from remote to local server i.e. SFTP Server
ls     -> List files and directories on remote server i.e. SFTP Server
pwd    -> Print present working directory on remote server i.e. SFTP Server
df     -> Print disk usage for destination directory on remote server i.e. SFTP Server

 

5. SFTP commands to upload directory from local to remote server

I could not find a single line SFTP put command to upload directory from local to remote server, if you come across any such one liner SFTP put command do let me know via comment section

I have used an alternate approach to combine all the SFTP commands by using EOF tag.

IMPORTANT NOTE:
While trying to upload directory from local to remote server it is important that the directory name which you plan to upload, a directory with the same name also must exist on your SFTP server. For example I have mydir on sftp-client, before I attempt to transfer I also have to create "mydir" on sftp-server before I attempt the upload from local to remote server.
[root@sftp-client ~]# sftp deepak@sftp-server <<EOF
> cd dest_dir
> mkdir sftp-client_dir
> put -r /tmp/sftp-client_dir <-- SFTP put command to upload /tmp/sftp-client_dir to sftp-server
> quit
> EOF
Connected to deepak@sftp-server.
sftp> cd dest_dir
sftp> mkdir sftp-client_dir
sftp> put -r /tmp/sftp-client_dir
Uploading /tmp/sftp-client_dir/ to /dest_dir/sftp-client_dir
Entering /tmp/sftp-client_dir/
sftp> quit
NOTE:
It is possible you may get "Couldn't canonicalize: No such file or directory; Unable to canonicalize path XXX". This means that the directory does not exist on SFTP server which you are planning to upload. Using SFTP CLI this is a pre-requisite to have a directory already exist with the same name before uploading directory.
sftp> put -r /tmp/sftp-client_dir sftp-client_dir
Uploading /tmp/sftp-client_dir/ to /sftp-client_dir
Couldn't canonicalize: No such file or directory
Unable to canonicalize path "/sftp-client_dir"

 

6. SFTP commands to transfer file using batch file

You can automate SFTP file transfer in Unix and Linux using batch file. I have explained more about batch file in another article so I will not cover this part again. use the below SFTP Syntax to use batch file and automate file transfers

sftp -b <batch_file> [user]@server:[port]

 

I have created below SFTP batch file to upload directory (sftp-client_dir) from local to remote server.

[root@sftp-client ~]# cat upload_dir.ba
cd dest_dir
mkdir sftp-client_dir
put -r /tmp/sftp-client_dir
quit

This batch file will create sftp-client_dir on sftp-server and then upload directory sftp-client_sir from sftp-client to the SFTP server.

[root@sftp-client ~]# sftp -b upload_dir.ba deepak@sftp-server
sftp> cd dest_dir
sftp> mkdir sftp-client_dir
sftp> put -r /tmp/sftp-client_dir
Entering /tmp/sftp-client_dir/
sftp> quit

Verify if the sftp-client_dir was successfully uploaded to sftp-server. Since we have configured SFTP authorized_keys to perform passwordless sftp, did not got any SFTP command line password prompt

[root@sftp-server dest_dir]# ls -l
total 4
drwxr-xr-x 2 deepak deepak 4096 Mar 31 20:14 sftp-client_dir

 

7. Single line SFTP commands to remove directory on remote server

The SFTP syntax to remove directory on sftp-server using one liner SFTP command line is same as we used to upload file from local to remote server

Using the below one liner SFTP rmdir command we will remove sftp-client_dir on sftp-server. Since this directory is under exchange folder of sftp-server hence I have provided this path while connecting to sftp-server.

[root@sftp-client ~]# sftp deepak@sftp-server:exchange/ <<< $'rmdir sftp-client_dir'
Connected to sftp-server.
Changing to: /exchange/
sftp> rmdir sftp-client_dir

We have successfully removed sftp-client_dir from our sftp-server which was inside exchange folder. Since we have configured SFTP authorized_keys to perform passwordless sftp, did not got any SFTP command line password prompt

 

8. Transfer files with SFTP commands using ssh_config

You can create a config file with SSH Client configuration values which will be used by SFTP command to transfer files. Use below SFTP syntax to use ssh_config

sftp -F <config_file> [user]@server:[port]

 

I have created below ssh config files with some values which will be used by our sftp-client

[root@sftp-client ~]# cat /tmp/ssh_config
AddressFamily inet
ConnectionAttempts 10
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
GatewayPorts no
HostBasedAuthentication no
PubkeyAuthentication yes
HostKeyAlias sftp-alias
PasswordAuthentication no
Compression yes
ServerAliveCountMax 3
ServerAliveInterval 15
TCPKeepAlive no
User deepak

We will use this ssh_config file with SFTP command line in combination with -F argument as shown below

[root@sftp-client ~]# sftp -F /tmp/ssh_config sftp-server
Connected to sftp-server.
sftp> exit

You can combine this with batch file which I used in above SFTP usage examples to automate SFTP transfer files between local and remote servers. Since we have configured SFTP authorized_keys to perform passwordless sftp, did not got any SFTP command line password prompt.

 

9. Use SSH options with SFTP single commands

If you do not wish to create additional SSH config file then you can also pass those arguments using -o argument with SFTP command. Use below SFTP syntax:

sftp [-o <ssh_option_1> -o <ssh_option_2> -o <ssh_option_3> ..] [user]@server:[port]

 

I have used some of the SSH client options in the below SFTP usage example to use Public Key Authentication with our SFTP.

[root@sftp-client ~]# sftp -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -o "PreferredAuthentications=publickey" -o "StrictHostKeyChecking=no" -o User=deepak sftp-server
Connected to sftp-server.
sftp> exit

Since we have configured SFTP authorized_keys to perform passwordless sftp, we did not got any SFTP command line password prompt

 

10. Use different Port with SFTP Commands to transfer files

By default SFTP uses the same port as SSH i.e. 22. But you can also configure SSH and SFTP to use a different port by using Port=<value> in /etc/ssh/sshd_config on SFTP server. In this SFTP usage example I have setup SFTP to use port 2200 instead of port 22.

We can connect using a custom SFTP port using -o Port=<value> as shown below

[root@sftp-client ~]# sftp -o Port=2200 -o User=deepak sftp-server
Connected to sftp-server.
sftp> exit

or alternatively we can also use SFTP argument -P <value> to define a custom port for SFTP connection.

[root@sftp-client ~]# sftp -P 2200 -o User=deepak sftp-server
Connected to sftp-server.
sftp> exit

 

Lastly I hope the SFTP usage examples from the article to use one liner SFTP commands in Unix and Linux to transfer files was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:
man page for sftp

 

Related Searches: linux sftp command line example, sftp get command, sftp usage, sftp syntax, sftp put command, sftp upload files, copy file from remote server to local machine windows, Where does sftp download to, sftp commands

 

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

1 thought on “10 single line SFTP commands to transfer files in Unix/Linux”

Leave a Comment

X