How to delete a file on remote machine via SSH by using a Shell Script?

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

ssh [email protected] '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 thats 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 publickey authentication.


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.


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