brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sftp -b runs a list of SFTP commands from a file over SSH

Network and access
~/tips/tip-sftp-b-batch-file

cd /var/www/upload

Script uploads and downloads with sftp and stop at the first failed command.

When a host only gives you SFTP, there is no shell to run a script on. sftp -b lets you script the transfer from your side instead.

Put the commands in a file, the same ones you would type at the sftp> prompt:

cd /var/www/upload
put -r dist/*
-rm old.zip
ls -l

Then run it:

sftp -b upload.txt [email protected]

The man page says sftp aborts when one of the important commands fails, like put, get, rm or cd. Start a line with - to make a failure there not count, which is why -rm old.zip above does not stop the run when that file is not there. A @ at the start hides the command from the output.

Use -b - to read the commands from standard input, handy with a here document in a shell script.

Because there is nobody to type a password, batch mode needs key authentication. I’d add -o BatchMode=yes too, so a missing key gives a clear error instead of a hanging script. And the exit code is not zero when it aborts, so your cron job or CI step can actually notice.

Further reading: sftp man page

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link