Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a Shell Script where I have to delete a file on a remote machine via a Shell Script.

Flow manually:

ssh username@domain.com

.. then at domain:

cd ./some/where
rm some_file.war

How to accomplish that task?

share|improve this question
add comment

4 Answers

up vote 20 down vote accepted

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@domain.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 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.

share|improve this answer
3  
I thought -c was cipher_spec - no? I didn't think -c was required for command passing. –  Scott 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
add comment

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

share|improve this answer
add comment

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/

share|improve this answer
add comment

Even better, you could use sftp instead of plain ssh shell.

share|improve this answer
    
Care to explain why and show how? –  Caleb Aug 1 '11 at 17:39
    
'sftp' meant for file transfers and does it better than plain ssh. It is somewhat like FTP server in an SSH tunnel. –  Tomer Cohen Aug 16 '11 at 20:06
    
I know exactly how SFTP works. The point is the OP was asking for a way to do this in a script. Can you provide an example of how you would do so with SFTP? –  Caleb Aug 16 '11 at 21:52
    
-b batchfile Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-’ character` –  Tomer Cohen Aug 17 '11 at 20:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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