# How to Use the Linux sftp Command Guide with Examples

> URL: https://www.atlantic.net/dedicated-server-hosting/how-to-use-the-linux-sftp-command-guide-with-examples/ | Published: 2024-11-25 | Updated: 2024-12-03 | Author: Hitesh Jethva

SFTP, or Secure File Transfer Protocol, is a network protocol that allows secure file access, transfer, and management over an encrypted SSH connection. SFTP is commonly used to transfer files between servers securely, often to or from a Linux server. It combines the ease of FTP with the security of SSH, making it suitable for use in environments that require encryption.

In this guide, we’ll explore how to use the sftp command effectively with practical examples.

## Connecting to a Remote Server

To connect to a remote server using sftp, you can use the following command:

```bash
sftp user@remote_server_ip
```

For example:

```bash
sftp john@192.168.1.10
```

After entering the command, you’ll be prompted for the user’s password**.** Once authenticated, you will see the **sftp>** prompt, indicating that you have successfully connected.

## Basic Commands in sftp

When connected via sftp, you can use various commands to navigate and manipulate files, similar to the commands used in a regular shell.

**1. Listing Files**

To list the files and directories in the current directory on the remote server, use:

```bash
ls
```

You can also use **lls** to list files in your local working directory:

```bash
lls
```

**2. Changing Directories**

To change the current directory on the remote server:

```bash
cd /path/to/directory
```

To change the directory on the local system:

```bash
lcd /path/to/local/directory
```

**3. Uploading Files**

To upload a file from your local system to the remote server, use the **put** command:

```bash
put local_file.txt /remote/directory
```

For example:

```bash
put document.txt /home/john/documents
```

**4. Downloading Files**

To download a file from the remote server to your local system, use the **get** command:

```bash
get /remote/directory/file.txt
```

For example:

```bash
get /home/john/documents/report.pdf
```

**5. Uploading/Downloading Directories**

To recursively upload a directory:

```bash
put -r local_directory /remote/directory
```

To download a remote directory recursively:

```bash
get -r /remote/directory local_directory
```

**6. Renaming Files**

To rename a file on the remote server:

```bash
rename old_name.txt new_name.txt
```

**7. Deleting Files**

To delete a file on the remote server:

```bash
rm /path/to/file.txt
```

## Checking Remote Directory

To display the current remote working directory:

```bash
pwd
```

To display the current local working directory:

```bash
lpwd
```

## Creating and Removing Directories

To create a new directory on the remote server:

```bash
mkdir /path/to/new_directory
```

To remove a directory on the remote server:

```bash
rmdir /path/to/directory
```

## Using sftp in Batch Mode

If you need to automate file transfers, you can use sftp in batch mode by providing a file containing a list of commands.

Create a batch file, commands.txt, with the following content:

```bash
lcd /local/directory
cd /remote/directory
put file1.txt
get file2.txt
```

Then run sftp in batch mode:

```bash
sftp -b commands.txt user@remote_server_ip
```

This allows you to automate repetitive file transfer tasks without manually typing each command.

## Using sftp in Non-Interactive Mode

To run sftp non-interactively, echo commands to sftp or use here documents. This is particularly useful for integrating sftp commands into scripts.

Example with here document:

```bash
sftp user@hostname <<EOF
put /local/path/file1.txt /remote/path/
get /remote/path/file2.txt /local/path/
EOF
```

## Conclusion

The sftp command is an essential tool for secure file transfers in Linux environments. Whether you’re transferring individual files or automating backups, sftp provides the flexibility and security needed to handle data in a reliable manner. Unlock the full potential of secure file transfers with SFTP on Atlantic.Net’s [dedicated server hosting](https://www.atlantic.net/dedicated-server-hosting/).
