Linux and Unix sftp command

Quick links

About sftp
Related commands
Linux and Unix main page

About sftp

sftp is a program for transferring files securely.


sftp [-1246Cpqrv] [-B buffer_size] [-b batchfile] [-c cipher] 
     [-D sftp_server_path] [-F ssh_config] [-i identity_file] 
     [-l limit] [-o ssh_option] [-P port] [-R num_requests] 
     [-S program] [-s subsystem | sftp_server] host
sftp [user@]host[:file ...]
sftp [user@]host[:dir[/]]
sftp -b batchfile [user@]host 


sftp is an interactive file transfer program, similar to ftp, which performs all operations over an encrypted ssh transport. It may also use many features of ssh, such as public key authentication and compression. sftp connects and logs into the specified host, then enters an interactive command mode.

The second usage format below will retrieve files automatically if a non-interactive authentication method is used; otherwise it will do so after successful interactive authentication.

The third format allows sftp to start in a remote directory.

The final usage format allows for automated sessions using the -b option. In such cases, it is necessary to configure non-interactive authentication to obviate the need to enter a password at connection time (see sshd and ssh-keygen for details).

Since some usage formats use a colon (':') to delimit host names from path names, IPv6 addresses must be enclosed in square brackets ('[]') to avoid ambiguity.


-1 Specify the use of protocol version 1.
-2 Specify the use of protocol version 2.
-4 Forces sftp to use IPv4 addresses only.
-6 Forces sftp to use IPv6 addresses only.
-B buffer_size Specify the size of the buffer that sftp uses when transferring files. Larger buffers require fewer round trips at the cost of higher memory consumption. The default is 32768 bytes.
-b batchfile Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-character (for example, -rm /tmp/blah*).
-C Enables compression (via ssh's -C flag).
-c cipher Selects the cipher to use for encrypting the data transfers. This option is directly passed to ssh.
-D sftp_server_path Connect directly to a local sftp server (rather than via ssh). This option may be useful in debugging the client and server.
-F ssh_config Specifies an alternative per-user configuration file for ssh. This option is directly passed to ssh.
-i identity_file Selects the file from which the identity (private key) for public key authentication is read. This option is directly passed to ssh.
-l limit Limits the used bandwidth, specified in Kbit/s.
-o ssh_option Can be used to pass options to ssh in the format used in ssh_config. This is useful for specifying options for which there is no separate sftp command-line flag. For example, to specify an alternate port use: sftp -oPort=24. For full details of the options listed below, and their possible values, see ssh_config.




















































-P port Specifies the port to connect to on the remote host.
-p Preserves modification times, access times, and modes from the original files transferred.
-q Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh.
-R num_requests Specify how many requests may be outstanding at any one time. Increasing this may slightly improve file transfer speed but will increase memory usage. The default is 64 outstanding requests.
-r Recursively copy entire directories when uploading and downloading. Note that sftp does not follow symbolic links encountered in the tree traversal.
-S program Name of the program to use for the encrypted connection. The program must understand ssh options.
-s subsystem | sftp_server Specifies the SSH2 subsystem or the path for an sftp server on the remote host. A path is useful for using sftp over protocol version 1, or when the remote sshd does not have an sftp subsystem configured.
-v Raise logging level. This option is also passed to ssh.

Interactive Commands

Once in interactive mode, sftp understands a set of commands similar to those of ftp. Commands are case insensitive. Pathnames that contain spaces must be enclosed in quotes. Any special characters contained within pathnames that are recognized by glob must be escaped with backslashes (‘\’).

bye Quit sftp.
cd path Change remote directory to path.
chgrp grp path Change group of file path to grp. path may contain glob characters and may match multiple files. grp must be a numeric GID.
chmod mode path Change permissions of file path to mode. path may contain glob characters and may match multiple files.
chown own path Change owner of file path to own. path may contain glob characters and may match multiple files. own must be a numeric UID.
df [-hi] [path] Display usage information for the filesystem holding the current directory (or path if specified). If the -h flag is specified, the capacity information will be displayed using "human-readable" suffixes. The -i flag requests display of inode information in addition to capacity information. This command is only supported on servers that implement the “” extension.
exit Quit sftp.
get [-Ppr] remote-path [local-path] Retrieve the remote-path and store it on the local machine. If the local path name is not specified, it is given the same name it has on the remote machine. remote-path may contain glob characters and may match multiple files. If it does and local-path is specified, then local-path must specify a directory.

If either the -P or -p flag is specified, then full file permissions and access times are copied too.

If the -r flag is specified then directories will be copied recursively. Note that sftp does not follow symbolic links when performing recursive transfers.
help Display help text.
lcd path Change local directory to path.
lls [ls-options [path]] Display local directory listing of either path or current directory if path is not specified. ls-options may contain any flags supported by the local system's ls command. path may contain glob characters and may match multiple files.
lmkdir path Create local directory specified by path.
ln [-s] oldpath newpath Create a link from oldpath to newpath. If the -s flag is specified the created link is a symbolic link, otherwise it is a hard link.
lpwd Print local working directory.
ls [-1afhlnrSt] [path] Display a remote directory listing of either path or the current directory if path is not specified. path may contain glob characters and may match multiple files.

The following flags are recognized and alter the behaviour of ls accordingly:

-1: Produce single columnar output.

-a: List files beginning with a dot (‘.’).

-f: Do not sort the listing. The default sort order is lexicographical.

-h: When used with a long format option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte, and Exabyte in order to reduce the number of digits to four or fewer using powers of 2 for sizes (K=1024, M=1048576, etc.).

-l Display additional details including permissions and ownership information.

-n Produce a long listing with user and group information presented numerically.

-r Reverse the sort order of the listing.

-S Sort the listing by file size.

-t Sort the listing by last modification time.
lumask umask Set local umask to umask.
imkdir path Create remote directory specified by path.
progress Toggle display of progress meter.
put [-Ppr] local-path [remote-path] Upload local-path and store it on the remote machine. If the remote path name is not specified, it is given the same name it has on the local machine. local-path may contain glob characters and may match multiple files. If it does and remote-path is specified, then remote-path must specify a directory.

If either the -P or -p flag is specified, then full file permissions and access times are copied too.

If the -r flag is specified then directories will be copied recursively. Note that sftp does not follow symbolic links when performing recursive transfers.
pwd Display remote working directory.
quit Quit sftp.
rename oldpath newpath Rename remote file from oldpath to newpath.
rm path Delete remote file specified by path.
rmdir path Remove remote directory specified by path.
symlink oldpath newpath Create a symbolic link from oldpath to newpath.
version Display the sftp protocol version.
!command Execute command in local shell.
! Escape to local shell.
? Synonym for help.



Typing the above command would connect to a secure connection for transferring files. If the host you're using supports a secure login you would then be connected to the host. Below is an example of what would be seen:

Connecting to FTP server ready. 


If your user name and password are valid and entered correctly, you will be successfully logged in:

Remote system type is UNIX. 
Using ASCII mode to transfer files. 


Once at the sftp> prompt, you will be in the default directory for the user you logged in as. The first thing you'd probably want to do is see what directory that is. To see the present working directory, use the pwd command just like in Linux:

sftp> pwd
257 "/ftpdefaultdir" is current directory. 

The number 257 is a numerical code. All FTP messages have a code number associated with them, and for technical reasons they are included with the messages from the server. The server lets you know you're in the /ftpdefaultdir directory. Let's see what files are in there, using the ls command:

sftp> ls 

This will produce a file listing, just like in Linux. You can change remote directories with cd. If you want to change what directory you're using on your local computer, you can use lcd for "local change directory." Let's say you want to get a file from the server named awesome.jpg, and download it to your local directory /home/myuser/images:

sftp> lcd /home/myuser/images
Local directory now /home/myuser/images
sftp> get awesome.jpg
local: awesome.jpg 
remote: awesome.jpg 
200 PORT command successful. 
150 Opening ASCII mode data connection for awesome.jpg (352271 bytes). 

Oops! That's not quite right. JPEG images are binary files, not ASCII (text) files.

FTP supports two different types of file transfers, ASCII and binary. At login, the server told us it was currently in ASCII mode. Let's change that to binary:

sftp> bin
Using binary mode to transfer files. 

bin is short for binary, and either command will switch to binary mode. We can now do the same file transfer and the file will come through correctly.

Now let's switch to the remote directory all-images and download every JPEG file using a wildcard.

sftp> cd all-images
250-README for all-images 
250-This folder contains all the JPEG images for our project. 
250 CWD command successful. 

This directory had a "README" message which is displayed by the FTP server every time you change it to your current directory. The server then let you know that your cd command was successful. We can now use the mget command to get multiple files:

sftp> mget *.jpg *.jpeg *.JPG *.JPEG 

We will now get all the jpeg files with the extensions JPG, JPEG, jpg, or jpeg.

If we have any files to upload to the server, we can use the commands put or mput to upload them. When we're done, we can logout using the exit command.

Related commands