mkdir -p over SSH bash

23,202

Solution 1

The last argument of ssh is command you want to execute on the remote host:

TESTDIR="$HOSTNAME"
ssh [email protected] "mkdir -p ~/$TESTDIR/test"

Solution 2

If you have a pem file to ssh as authentication use the following

ssh -i your-key.pem user@ip_addr "mkdir -p /your_dir_name/test"
Share:
23,202
eekfonky
Author by

eekfonky

New job at a small start up as junior sysadmin

Updated on July 09, 2022

Comments

  • eekfonky
    eekfonky almost 2 years

    I have a small test script as follows;

    TESTDIR="$HOSTNAME"
    ssh [email protected] "\$TESTDIR"
    mkdir -p ~/$TESTDIR/test
    exit
    

    the output with bash -x is;

    + TESTDIR=ndx
    + ssh [email protected] '$TESTDIR'
    + mkdir -p /home/user/ndx/test
    + exit
    

    Yet on the remote server, no directory exists?