Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir -p over SSH bash

Tags:

ssh

mkdir

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?

like image 344
eekfonky Avatar asked Nov 02 '16 15:11

eekfonky


People also ask

How do I use mkdir in bash?

Create a New Directory ( mkdir ) The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd . Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).

How do I create a folder using SSH?

Create a new directory on your server using the mkdir command. mkdir foldername – create a new directory called foldername. mkdir -p foldername – create a new directory called foldername, even if the directory already exists.


2 Answers

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

TESTDIR="$HOSTNAME"
ssh [email protected] "mkdir -p ~/$TESTDIR/test"
like image 143
Jakuje Avatar answered Oct 22 '22 07:10

Jakuje


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"
like image 20
Pranoy Gn Avatar answered Oct 22 '22 07:10

Pranoy Gn