Open menu

Learn

SFTP put Command

The world of internet file transfers can be complex. Still, it becomes a lot simpler once you understand the tools and protocols at your disposal. One protocol that is especially important for file transfers is SFTP, and today, we'll be focusing specifically on the put command in SFTP.

What is the SFTP PUT Command?

The "put" command is one of the most commonly used commands in SFTP. This command allows a user to upload a file from the local machine (client) to a remote server. The syntax for the put command is simple:
put local-file-path [remote-file-path]
Here, local-file-path is the path to the file on your local machine that you want to upload, and remote-file-path is the path where you want to place the file on the remote server. The [remote-file-path] is optional. If it's not provided, the file will be uploaded to the current working directory on the remote server.

How to Use SFTP PUT

To use the put command, you first need to establish a connection to the SFTP server. This can be done using the sftp command followed by your username and the address of the server you want to connect to:
sftp username@server-address
This command will prompt you to enter your password. After providing your password, you'll be logged into the SFTP server. To upload a file, navigate to the local directory containing the file you want to upload using the lcd command:
lcd /path/to/local/directory
Then, use the put command to upload the file:
put filename.extension
This will upload the file to your current directory on the server. If you want to specify a different directory or rename the file on the server, provide a second argument to the put command:
put filename.extension /path/to/remote/directory/newname.extension
This will upload the file to the specified directory and give it the new name.

Advanced Usage of the SFTP PUT Command

While the basic put command is straightforward, SFTP also allows more complex usage. Below are two examples.

Recursive upload with -r

If you want to upload an entire directory, including all its files and subdirectories, you can use the -r (recursive) option:
put -r localdirectory
This command will create a directory with the same name on the remote system and upload all the files and subdirectories within it.

Preserving original file attributes with -p

To retain the original file's permissions and timestamps during the upload, use the -p (preserve) option:
put -p localfile