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.
| Server | Your machine | What it does |
|---|---|---|
pwd | lpwd | Show the current folder |
cd /tmp | lcd dl | Change folder |
ls -la | lls | List files |
mkdir upload | lmkdir upload | Create a folder |
Moving files
| Command | What it does |
|---|---|
get report.pdf | Download to the local folder |
get report.pdf copy.pdf | Download with another name |
get -r site dl/site2 | Download a folder |
put report.pdf | Upload to the remote folder |
put -r site | Upload a folder |
reget big.bin | Continue a partial download |
reput big.bin | Continue a partial upload |
Managing files on the server
| Command | What it does |
|---|---|
rename old.pdf new.pdf | Rename or move |
cp report.pdf copy.pdf | Copy a file on the server, if it runs OpenSSH 9.0 or later |
rm old.pdf | Delete a file |
rmdir site | Delete an empty folder |
chmod 640 file | Change permissions |
ln -s target name | Create a symbolic link |
df -h | Show free space |
!ls | Run a command on your machine |
help | List commands |
bye | Leave |
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.