Send files with sftp then delete afterwards one line?
Sophia Terry
How can I send files using sftp then delete them afterwards?
I have managed to get this far with sending all files with a set extension from a directory,
sftp user@remote <<< $'put -r /path/to/file/test_file/*.json'but how can I delete them once I have sent them and only if they have been successfully sent on the one line command?
Also, the server I am using is very locked down, so I won't be able to install additional software from the internet and could this be kept as a one line send. This will also be used with cron, hence using one line.
1 Answer
If you need it in one line, use && operator:
sftp user@remote <<< $'put -r /path/to/file/test_file/*.json' && rm /path/to/file/test_file/*.jsonIt will execute rm only if sftp has succeeded. Adding -b - to sftp is probably a good idea (if not necessary).