![如何在 Ubuntu 服务器上删除使用 SFTP 获取的文件?](https://linux22.com/image/1160372/%E5%A6%82%E4%BD%95%E5%9C%A8%20Ubuntu%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E5%88%A0%E9%99%A4%E4%BD%BF%E7%94%A8%20SFTP%20%E8%8E%B7%E5%8F%96%E7%9A%84%E6%96%87%E4%BB%B6%EF%BC%9F.png)
我将 Putty 连接到两个不同的 Ubuntu 服务器(A 和 B)。我想通过 SFTP 将 A 传输到 B,并将get
B 目录中的所有文件传输到 A,然后在下载后立即删除 B 中的文件 - 这样只有 A 拥有这些文件。
如何get
在传输/下载成功后立即获取文件并删除文件?
答案1
您可以使用以下命令删除远程文件:
rm [path]
对于远程目录:
rmdir [path]
用实际路径、文件名或目录名替换[path]
。这也可以是您在普通终端中习惯使用的通配符。要递归获取目录,您可能需要标志-r
。
遗憾的是,没有命令可以将文件从 B 移动到 A,因此您需要将文件从 B 获取到 A,然后删除 B 上的文件。这意味着没有命令将获取和删除连接在一起。
要批量获取然后删除文件,您可以使用通配符。
示例(假设您的文件位于服务器 B 上的 XY 文件夹中):
# 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
logout
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