16

Is it possible to do a simple inline style SSH command, for example:

ssh [email protected] { cd foo/bar && rm *.foobar }

3 Answers 3

28

Should you want to execute cd foo/bar && rm *.foobar on the remote machine, simply do

ssh [email protected]  'cd foo/bar && rm *.foobar'

and see man ssh...

ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D
[bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L   [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R
[bind_address:]port:host:hostport] [-S ctl_path] [-w tunnel:tunnel
[user@]hostname [command]

The parts you want:

ssh [user@]hostname [command]
1
  • 2
    Worth noting that you can pipe STDIN and STDOUT using this method as well- just the other day I made a HD image of a remote machine using DD over ssh.
    – crazy2be
    Aug 5, 2011 at 2:54
9

style 1: ssh user@host 'embedded command'

style 2: ssh user@host " $(cat cmd.txt) "
cmd.txt is file that contain your remote command which is locate at your local machine

7

yes it is :

ssh [email protected] 'cd foo/bar && rm *.foobar '

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .