1

I am trying to use write a script that uses SSH to create a new directory and write to a text file in it. I've got 1 master on a network, and then 3 nodes that I want to create the directory on. These 4 machines are hosted on VMware.

#node1
ssh node1@192.168.1.102 'sudo touch /temp_dirname/host.txt'
ssh node1@192.168.1.102 'echo "node1" | sudo tee /temp_dirname/host.txt'

#node2
ssh node1@192.168.1.103 'sudo touch /temp_dirname/host.txt'
ssh node1@192.168.1.103 'echo "node1" | sudo tee /temp_dirname/host.txt'

#node3
ssh node3@192.168.1.104 'sudo touch /temp_dirname/host.txt'
ssh node3@192.168.1.104 'echo "node1" | sudo tee /temp_dirname/host.txt'

When I run this I get different errors for each node... for nodes 1 and 2 I get

touch: cannot touch '/temp_dirname/host.txt': no such file or directory

and

tee: temp_dirname/host.txt: no such file or directory

and node 3 I get:

touch: setting times of '/temp_dirname/ no such file or directory

and

tee: temp_dirname/host.txt: no such file or directory

I am absolutely confused with this as I thought touch created files - so why is it no such file or directory.

dessert
  • 39,392
  • 12
  • 115
  • 163
Amy
  • 23
  • 6
  • 1
    Possibly related: [Create file and its parent directory](https://askubuntu.com/questions/800845/create-file-and-its-parent-directory) – steeldriver Apr 15 '19 at 18:06

1 Answers1

3

If /temp_dirname doesn't exist, you can create it with mkdir -p /temp_dirname.

-p works with any arbitrary depth, creating directories as required.

vintnes
  • 146
  • 2
  • 1
    This works! I could have sworn I tried this earlier with no avail... One question, I do have @vintnes is that `/temp_dirname` doesn't show up when I do either `ls` or `ls -a` – Amy Apr 15 '19 at 18:13
  • `ls` defaults to your current working directory -- usually `/home/$USER` by default. Specify path, as in `ls -a /` or `ls -a /temp_dir` or change your working directory with `cd`. Get comfortable with `man`! – vintnes Apr 15 '19 at 22:12