How to use SFTP to transfer files to a remote server
In the article "How to use SFTP to interact with a remote server" , we introduced how to connect and interact with a server using SFTP. Now let's take a look at how to perform the main operation - file transfer - after the connection is established.
Transferring files using SFTP
If we want to download a file from a remote host, we can use the get command:
sftp> get remoteFile
The download process and the output of successful download are as follows
Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile 100% 37KB 36.8KB/s 00:
As you can see, by default, the get command downloads the remote file to a file of the same name on the local file system.
We can copy the remote file to a different name by specifying the name afterwards:
sftp> get remoteFile localFile
The get command can also be followed by some parameters. For example, we can copy a directory and all its contents by specifying the recursive option ( -r ):
sftp> get -r someDirectory
We can tell SFTP to preserve the proper permissions and access times using the -P or -p flag:
sftp> get -Pr someDirectory
Transfer local files to remote systems (upload files)
Transferring files to a remote system works the same way as downloading, but we use put
the command:
sftp> put localFile
The upload process and the output content of successful upload are as follows
Uploading localFile to /home/demouser/localFile
localFile 100% 7607 7.4KB/s 00:00
The options used with get also apply to put. So to copy an entire local directory, you would run put -r
:
sftp> put -r localDirectory
A familiar tool that is helpful when downloading and uploading files is df
the command, which works similarly to what it does on the command line. Using it, we can check if there is enough space to complete our transfer:
sftp> df -h
The output is as follows
Size Used Avail (root) %Capacity
19.9GB 1016MB 17.9GB 18.9GB 4%
Note that this command cannot be used directly to view the local space, but we can use
!
the command.
!
The command drops us into a local shell where we can run any command available on the local system. We can check our disk usage by typing:
sftp> !
Then use the following command
$ df -h
Now we can check the space of our local system.
Filesystem Size Used Avail Capacity Mounted on
/dev/disk0s2 595Gi 52Gi 544Gi 9% /
devfs 181Ki 181Ki 0Bi 100% /dev
map -hosts 0Bi 0Bi 0Bi 100% /net
map auto_home 0Bi 0Bi 0Bi 100% /home
Any other local commands will work fine. To return to our SFTP session, use the following command:
$ exit
You should now see the SFTP prompt, indicating that we are back.
Use SFTP for simple file operations
SFTP allows us to perform certain types of file system management. For example, we can change the owner of a file on the remote system using the following command:
sftp> chown userID file
Note that unlike the system chmod command, the SFTP command does not accept a username, but rather a UID. Unfortunately, there is no built-in way to learn the appropriate UID from within the SFTP interface.
As a workaround, you can /etc/passwd
read from the file that associates usernames with UIDs in most Linux environments:
sftp> get /etc/passwd
sftp> !less passwd
The output is as follows
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .
Note that in the second command above, instead of using the ! command on its own, we use it as a prefix to a local shell command. This will run any command available on our local machine, and can be used with the local df command as before.
The UID will be in the third column of the file and is represented by a colon character.
Likewise, we can change the group owner of a file by:
sftp> chgrp groupID file
Again, there is no built-in way to get a list of remote system groups. We can work around this with the following command:
sftp> get /etc/group
sftp> !less group
The output is as follows
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
. . .
The third column contains the ID of the group associated with the name in the first column. This is what we are looking for.
chmod
SFTP commands work fine on remote file systems:
sftp> chmod 777 publicFile
Changing mode on /home/demouser/publicFile
There is no equivalent command for manipulating local file permissions, but you can set the local umask so that any files copied to the local system will have the appropriate permissions.
This can be lumask
done with the command:
sftp> lumask 022
Local umask: 022
Now all regular files downloaded (as long as the -p flag is not used) will have 644 permissions.
SFTP also allows us to create directories on local and remote systems using lmkdir
and respectively.mkdir
The remaining file commands target remote file systems only:
sftp> ln
sftp> rm
sftp> rmdir
These commands duplicate the core functionality of their shell equivalents. If you need to perform these operations on your local file system, remember that you can enter a shell by using the following command:
sftp> !
!
Or you can execute a single command on the local system by
prefixing the command like this:
sftp> !chmod 644 somefile
When you are finished with the SFTP session, use exit or bye to close the connection.
sftp> bye
Summarize
Although SFTP syntax is far less comprehensive than modern shell tools, it can be useful for providing compatibility with traditional FTP syntax or for carefully limiting the functionality available to remote users in certain environments.
For example, we can use SFTP to enable specific users to transfer files without SSH access.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Getting headers in PHP
Publish Date:2025/04/13 Views:103 Category:PHP
-
HTTP headers carry data between web servers and browsers for communication. Whenever we enter a URL in the address bar of our browser, it sends an HTTP request to the server; it contains a header. In PHP use get_headers() get the headers fo
Get the first row of Dataframe Pandas
Publish Date:2025/04/12 Views:78 Category:Python
-
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get
How to get the time difference in minutes in PHP
Publish Date:2025/04/12 Views:83 Category:PHP
-
In this article, we will describe the method of getting the time difference in minutes in PHP. Using date_diff() Functions Using Mathematical Formulas Use the function in PHP date_diff() to get the time difference in minutes We will use the
Get JSON object from URL in PHP
Publish Date:2025/04/12 Views:64 Category:PHP
-
This article explains how to get a JSON object from a URL in PHP. file_get_contents() Get JSON object from URL in PHP using function We can use the README.mdl file_get_contents() and README.mdl json_decode() to get a JSON object from a URL.
Getting and Setting Pandas DataFrame Index Names
Publish Date:2025/04/12 Views:202 Category:Python
-
This tutorial explains how to set and get the name of the index column in a Pandas DataFrame. We will use the following DataFrame example in the article. import pandas as pd my_df = pd . DataFrame( { "Applicant" : [ "Ratan" , "Anil" , "Muke
Getting Random Values in MySQL
Publish Date:2025/04/10 Views:123 Category:MySQL
-
This tutorial aims to understand how to shuffle or sort the values or records of a table in MySQL. Most businesses and organizations that use MySQL for data analysis or visualization need to sort different table values of their
Get a summary version of the man pages
Publish Date:2025/04/07 Views:126 Category:OPERATING SYSTEM
-
I rarely use it man pages , because searching for the syntax or specific flags of a command is much faster than browsing through a lot of information we may not need. However, I found a package that summarizes the output of the man pages an
Get a list of mounted file systems in Linux
Publish Date:2025/04/06 Views:61 Category:OPERATING SYSTEM
-
The Linux operating system consists of layered file systems. There can be many different types of file systems, ext4 , tmpfs , securityfs , configfs etc. This article will explain how to get a list of mounted file systems in Linux. In Linux
How to use SCP to securely transfer files and directories
Publish Date:2025/04/05 Views:105 Category:OPERATING SYSTEM
-
SCP , also known as 安全拷贝 , is a command line utility used to transfer files and directories from local to remote system and vice versa. It also allows us to transfer files and directories between two remote systems. During the trans