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.

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.

add comment

4 Answers 4

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
add comment

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
add comment

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

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

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