How to delete get'ed files using SFTP on an Ubuntu server?
Matthew Martinez
I putty across to two different Ubuntu servers (A and B). I would like to SFTP from A to B and get all the files in a directory in B to A and delete the files in B straight after the download - so that only A has the files.
How can achieve get files and delete the file straight after a successful transfer/download?
1 Answer
You can use the following to remove remote files:
rm [path]and for remote directories:
rmdir [path]Where you replace [path] with your actual path or file name or directory name. This as well can be wildcards as you are used to in normal terminal. for recursively getting directories you might need the -r flag.
Sadly there is no command for moving the files from B to A so you need to get the files from B to A and then remove the files on B. means there is no command to concatenate the get and the remove together.
For getting and then deleting the files in bulk you can use wildcards.
Example (let's assume you have your files inside a folder XY on your server B):
# log into server A via putty/ssh
ssh user@A
# then sftp to B from A
sftp user@B
# now you will get a prompt for sftp which I will not
# repeat in the commands to follow
sftp>
# navigate to your local target directory on server A
lcd pathtotarget/
# navigate to your source directory on server B
cd XY
# get all the files from B to A
get *
# after the transfer is done you can go on and remove the files on B
cd ..
rmdir XY
# or if you want only the files being deleted
rm *
# quit your sftp session
quit
# log off from server A
logout2The command set available on SFTP:
bye Quit sftp cd path Change remote directory to 'path' chgrp grp path Change group of file 'path' to 'grp' chmod mode path Change permissions of file 'path' to 'mode' chown own path Change owner of file 'path' to 'own' df [-hi] [path] Display statistics for current directory or filesystem containing 'path' exit Quit sftp get [-afPpRr] remote [local] Download file reget [-fPpRr] remote [local] Resume download file reput [-fPpRr] [local] remote Resume upload file help Display this help text lcd path Change local directory to 'path' lls [ls-options [path]] Display local directory listing lmkdir path Create local directory ln [-s] oldpath newpath Link remote file (-s for symlink) lpwd Print local working directory ls [-1afhlnrSt] [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory progress Toggle display of progress meter put [-afPpRr] local [remote] Upload file pwd Display remote working directory quit Quit sftp rename oldpath newpath Rename remote file rm path Delete remote file rmdir path Remove remote directory symlink oldpath newpath Symlink remote file version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell ? Synonym for help