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 started with FastCGI
Publish Date:2025/03/18 Views:164 Category:NETWORK
-
In "First Contact with CGI", we mentioned the operating mechanisms of CGI and Server APIs, as well as their respective advantages and disadvantages. In this chapter, we will learn about FastCGI, which combines the advantages of CGI and Serv
How to use SFTP to interact with a remote server
Publish Date:2025/03/17 Views:61 Category:NETWORK
-
FTP , or File Transfer Protocol, is a popular, unencrypted method of transferring files between two remote systems. As of 2022, it has been deprecated by most modern software due to its lack of security, and is mostly only used for legacy a
React uses Router to get the current route
Publish Date:2025/03/15 Views:100 Category:React
-
Use the `useLocation()` hook to get the current route through React Router, such as `const location = useLocation()`. The hook returns the current location object. For example, we can access the pathname as location.pathname.
Get element by ID in React
Publish Date:2025/03/14 Views:102 Category:React
-
To get an element by ID in React: Set the ref attribute on the element. Use the current property to access the element in the useEffect hook.
Get the width and height of the window in React
Publish Date:2025/03/12 Views:174 Category:React
-
To get the width and height of the window in React: Use the innerWidth and innerHeight properties of the window object. Add an event listener for the resize event in the useEffect hook. Save the changes in the window width and height in state variable
How to get the current year in React
Publish Date:2025/03/12 Views:135 Category:React
-
Get the current year in React using the Date() constructor, for example, new Date().getFullYear(). The getFullYear() method will return a number corresponding to the current year.
Get the width of an element in React
Publish Date:2025/03/12 Views:190 Category:React
-
To get the width of an element in React: Set a ref attribute on the element. In the useLayoutEffect hook, update the state variable for the width. Use the offsetWidth attribute to get the width of the element.
Get the height of an element in React
Publish Date:2025/03/11 Views:174 Category:React
-
To get the height of an element in React: Set a ref attribute on the element. In the useLayoutEffect hook, update the state variable for the height. Use the clientHeight property to get the height of the element.
Get data on button click in React
Publish Date:2025/03/11 Views:67 Category:React
-
To fetch data on button click in React: Set the onClick attribute on the button element. Every time the button is clicked, an HTTP request is made. Update the state variable and render the data.