sftp commands: get, put, ls, lcd and batch mode

Run sftp user@server to open a prompt. Use get file to download, put file to upload, ls and cd on the server, lls and lcd on your machine, and bye to leave. For scripts, list the commands in a file and run sftp -b commands.txt user@server, which needs key login because batch mode cannot ask for a password.

Updated . OpenSSH 10.3 sftp on macOS to OpenSSH 10.0 on Debian 13 in a throwaway container. The cp command and the refused rm -r were run on OpenSSH 10.0 in Debian 13.

Connect

sftp user@server
sftp -P 2222 user@server
sftp user@server:/var/log/

The port option is a capital -P. The last form opens the prompt in that folder.

Commands at the prompt

Commands that start with l act on your machine. The rest act on the server.

ServerYour machineWhat it does
pwdlpwdShow the current folder
cd /tmplcd dlChange folder
ls -lallsList files
mkdir uploadlmkdir uploadCreate a folder

Moving files

CommandWhat it does
get report.pdfDownload to the local folder
get report.pdf copy.pdfDownload with another name
get -r site dl/site2Download a folder
put report.pdfUpload to the remote folder
put -r siteUpload a folder
reget big.binContinue a partial download
reput big.binContinue a partial upload

Managing files on the server

CommandWhat it does
rename old.pdf new.pdfRename or move
cp report.pdf copy.pdfCopy a file on the server, if it runs OpenSSH 9.0 or later
rm old.pdfDelete a file
rmdir siteDelete an empty folder
chmod 640 fileChange permissions
ln -s target nameCreate a symbolic link
df -hShow free space
!lsRun a command on your machine
helpList commands
byeLeave

There is no recursive delete: rm -r stops with rm: Invalid flag -r. rm removes one file and rmdir only removes an empty folder, so a full folder has to be emptied first.

One file without the prompt

sftp user@server:/etc/hostname local-copy

For quick one-off copies, scp examples are often shorter. The full comparison is in scp vs SFTP vs rsync.

Batch mode for scripts

Put the commands in a file:

cd /tmp
put report.pdf
bye

Then run it:

sftp -b commands.txt user@server

Or read from standard input with -:

printf 'put report.pdf /tmp/report.pdf\nbye\n' | sftp -b - user@server

It needs a key login

-b turns off password prompts. With a password-only account it stopped in our test with:

user@server: Permission denied (publickey,password).
Connection closed

Set up a key first. See how to generate an ed25519 key.

It stops at the first error

This is the point of -b. A command that fails ends the run with exit code 1, so your script can react:

$ sftp -b b2.txt user@server
sftp> cd /nosuch
stat remote: No such file or directory
$ echo $?
1

To allow a failure on one line, start it with -. To hide the echo of a line, start it with @:

-cd /nosuch
@pwd

With the - line, the run in our test continued and ended with exit code 0.

Piping commands without -b

sftp user@server < commands.txt does not stop for errors. In our test it continued after a failed cd and ended with exit code 0. It can still ask for a password from your terminal. Use it for a quick manual job. Use -b in scripts.

Questions

How do I download a folder with sftp?

Use get -r, as in get -r site dl/site. Without -r, sftp does not download a folder.

How do I change the local folder in sftp?

Use lcd, for example lcd ~/Downloads. Commands that start with l, such as lls and lpwd, act on your machine, and the others on the server.

Why does sftp -b fail with Permission denied?

Batch mode cannot ask for a password, so it needs a key login. Set up an SSH key for the account, then run sftp -b again.

Sources

More in SFTP and file transfer

All guides