I am using scp to copy stuff to a remote location. But sometimes scp does not work as expected, I have found that sometimes the copy does not complete properly (possibly when I have added new files to the folder I am copying).

So I want to remove the remote folder first and then do the copy to ensure that I have exactly what I need.

Are there other commands like scp (e.g. srm or smv)? Or is there a way to remove remote folders and files?


The ssh command will allow you to execute pretty much any command on the remote host, e.g.,

ssh yourlogin@remotehost rmdir somedir

where in this example somedir is relative to the home directory of yourlogin.

| improve this answer | |
  • Thanks for that. The only issue I have with the method is that each time you call it you have to "login" again :(. Since my target is embedded I can't use ssh keys because it all gets over-written, so I am trying to do this in one command. rsync looks promising. But, +1 since this does answer the question :)) – code_fodder Oct 1 '13 at 14:18
  • 5
    @code_fodder you will have to login with rsync as well. In fact, you will have to log in again with all methods. In fact, with ssh you can at least execute multiple commands in one go: ssh you@host bash -c "command1;command2;command3; commandN". – terdon Oct 1 '13 at 14:38

Rsync will copy an entire path/tree and check files already at the destination and not bother copying them if they're unchanged. If you use the --delete option, it will delete any files that are at the destination that are no longer at the source. It works over ssh

| improve this answer | |
  • I was messing around with rsync, as someone mentioned this in another post of mine regarding scp. I have rsync installed on my local pc but not on the remote pc (an embedded unit). I was using the command rsync -avz -e shh root@location:/path/to/dest /path/from/host But it fails after I enter the password saying sh: rsync: not found. Does that mean rsync has to exist on the remote pc? Thanks! – code_fodder Oct 1 '13 at 14:15
  • 1
    @code_fodder yes, it needs a program running on the remote system to tell it what to do. It's not a straight copy program – Canadian Luke Oct 1 '13 at 14:27
  • @CanadianLuke ... damn :( – code_fodder Oct 1 '13 at 14:33
  • @code_fodder just install it! More useful then not – Canadian Luke Oct 1 '13 at 14:34
  • Depending on the embedded solution rsync may or may not be available with any degree of easy... though it's a very common untility so as i said, depending on the embedded solution. Note: Rsync doesn't have to be running all the time, but it needs to be present to be run on demand. – jerm Oct 1 '13 at 15:25

If your remote SSH server supports the SFTP subsystem (most do, unless it's been explicitly disabled), you can use the sftp shell (or any other SFTP client) to manipulate files and directories on the remote server.

Unfortunately, one thing the basic sftp shell apparently does not handle is recursive directory removal; in order to delete a directory, you have to delete all its contents first. Most other clients (especially graphical ones) typically do support it, though.

(You don't mention what your local OS is, but if it's Linux, you can use your native file manager as GUI SFTP client by pointing it at a URL like sftp://user@host/path/. For Windows, WinSCP is a decent client, and FileZilla is a good cross-platform solution.)

| improve this answer | |

To empty a remote (/local) directory, use rsync like this:

rsync -r --delete-excluded --exclude=* / server:/tmp/empty_this_dir/

Be careful - it removes everything below the specified destination!

| improve this answer | |
  • I've tried this, but it returns an error and doesn't remove the remote directory. exec request failed on channel 0 rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.2] – xarlymg89 Oct 25 '19 at 12:26
  • It seems like there is a problem starting rsync on the remote side or even connecting via ssh. You can try ssh server rsync --version or ssh -v server rsync --version to find the reason. – bro Oct 26 '19 at 9:56
  • Thanks for answering, however I found a way to solve the issue, not as elegant but with similar results. Will try again since I believe I'll have to face it again. – xarlymg89 Oct 28 '19 at 10:30

As an alternative, mounting the remote directory in a local one, then removing it with rm -r /path_to_directory_to_remove and finally umounting the directory does the trick.

Original answer here: https://stackoverflow.com/a/24650511/973919

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.