Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using a shell script, how can I create a directory remotely from serverA -> serverB using ssh? Can I pass the create directory command with the ssh command? How?

I'm basically looking to create a directory remotely based on date (ex. 20110304), if it doesn't already exist.

share|improve this question

migrated from stackoverflow.com Mar 4 '11 at 23:59

This question came from our site for professional and enthusiast programmers.

ssh remote-host-ip '. ~/your_profile; mkdir your_directory'

don't know what you mean exactly by "by date", but you could test before mkdir as below

ssh remote-host-ip '. ~/your_profile; test -d your_directory || mkdir your_directory'

if you want a directory name as you mention, try

ssh remote-host-ip '. ~/your_profile; test -d your_parent_directory/$(date +%Y%M%d) || mkdir your_parent_directory/$(date +%Y%M%d)'
share|improve this answer
Select your code and press CTRL+K to turn on code highlighting or optionally surround them in backticks `` if you want to highlight inline like so – SiegeX Mar 4 '11 at 23:52
@SiegeX: Thank you for your helpful tip! – Xichen Li Mar 4 '11 at 23:53

you can accomplish it like this:

  ssh you@serverB "mkdir /path/to/dir"

or If you have a script on serverA....

  ssh you@serverB 'bash -s' < local_script.sh
share|improve this answer
also if you need bash to load your environment, you can use bash --login – Andrea Z Mar 4 '11 at 23:54

If you want to generate a date programmatically, take a look at date +format:

jinx:774 Z$ date +%m%d%H%M
share|improve this answer

ssh user@server mkdir path/$(date +%Y%m%d)

You need to setup password less authentication between the servers for this to work. Here are the steps to do that.

share|improve this answer

Your Answer


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.