36

I am writing a shell script where I have to delete a file on a remote machine via a shell script.

Manual workflow:

  1. Log on to remote machine:
    ssh username@domain.example
    
  2. At the remote machine (domain), type the following commands
    cd ./some/where
    rm some_file.war
    

How should I accomplish that task in a script?

AdminBee
  • 22,803
mico
  • 701

4 Answers4

61

You can pass the SSH client a command to execute in place of starting a shell by appending it to the SSH command.

ssh username@example.com 'rm /some/where/some_file.war'

You don't have to cd to a location to remove something as long as you specify the full path, so that's another step you can skip.

The next question is authentication. If you just run that, you will get prompted for a password. If you don't want to enter this interactively you should set up public key authentication.

Stephen Kitt
  • 434,908
Caleb
  • 70,105
  • 3
    I thought -c was cipher_spec - no? I didn't think -c was required for command passing. – Scott C Wilson Jul 27 '11 at 12:31
  • Thank you all you guys for quick answers. This answer was the one containing the most of the parts to the solution so @Caleb got the tick this time. – mico Jul 27 '11 at 12:38
  • Should we add -f to rm executing remote call ? – Fedir RYKHTIK May 07 '14 at 15:43
  • 1
    @Fedir NO! The question specifically asks about files not directories and if there are any warnings or errors thrown by rm they should certainly be passed on to the caller unless the user knows what to expect and why they are overriding something. Lots of mistakes and frustrating debuging later can be avoided by only using the options you need in a given senario. – Caleb May 07 '14 at 20:20
  • @Caleb Ok, but if I would like to remove the file without interactive prompt ? "Do You really would like to delete .... ?" (Note : I'm asking not about -r, but -f) – Fedir RYKHTIK May 09 '14 at 13:53
  • 1
    @Fedir The -f option to rm is short for --force and has an effect an whether errors are thown for non-existing files or bogus arguments. Usually commands run over non interactive shells default to being less interactive anyway, but it yous is not or you are getting an interactive prompt the thing to do would be to fix the settings directly related to that. If you are scripting this you shouldn't have that issue anyway, and if you are in an interactive shell (where you have the possibility of catastrophic typos) you should use -I on --interactive=never to set your desired behavior. – Caleb May 09 '14 at 14:06
  • @Caleb Thanks for the explanaition about non interactive shells. I've found the way to test it. If ssh user@host echo "\$-" has "i", it's an interactive shell. – Fedir RYKHTIK May 09 '14 at 14:21
  • what about "Host key verification"? @Caleb – TheNone Jun 04 '17 at 16:55
  • To force it to remove a read-only file your command would be ssh somesystem 'rm -f somefile' – SDsolar Oct 24 '17 at 01:07
4

If you wish to delete remote file with the use of sudo, you need to execute something like this:

ssh -tt user@host 'stty raw -echo; sudo rm /path/to/file' < <(cat)

Details.

2

The ssh command has a command parameter (last parameter in the command) that you can use to run remote commands.

1

Setup passwordless keys then add the command as part of the ssh command. See: http://www.dotkam.com/2009/03/10/run-commands-remotely-via-ssh-with-no-password/

laebshade
  • 2,176