Reputation: 581
I want to create an input argument for mkdir -p, I have one directory tree structure in one server and I want to copy this same structure to another machine using a mkdir -p command. The two machines don't "talk" to each other, no telnet, no ssh. The objective is to insert a simple command (mkdir -p master/{one/{a,b,c},two/{a,b,c},three/{a,b,c}}) in a install script in server2, I don't want an extra file and I can't access server1 from server2.
Something like master/{one/{a,b,c},two/{a,b,c},three/{a,b,c}}. I'm using ( find . -type d ) to find the directories and now I need the awk part.
What is the best way to create an awk command to find the tree like structure ?
Directory tree:
-- master
----one
--------a
--------b
--------c
----two
--------a
--------b
--------c
----three
--------a
--------b
--------c
Mkdir command:
mkdir -p master/{one/{a,b,c},two/{a,b,c},three/{a,b,c}}
Upvotes: 2
Views: 1608
Reputation: 58578
This find/sort/GNU sed oneliner makes a start:
mkdir -p master/{one,two,three}/{a,b,c}
find master -type d | sort |
sed -r 'H;${x;s/\n//;y/\n/ /;:a;s/([^ ]*) (\1\/[^ ]* )/\2/;ta;:b;s#(([^ /]*/)+)([^ /]+) \1([^ /]+)#\1\3,\4#;tb;s#(([^ /]*/)*)([^ ]*)#\1{\3}#g;:c;s#(([^ /]*/)*)([^/ ]+)/(\{[^}]*\}) \1([^ /]+)/\4#\1\3,\5/\4#;tc;s#/([^/ ]*)/\{#/{\1}/{#g;s/^/mkdir -p /p};d'
mkdir -p master/{one,three,two}/{a,b,c}
It's a bit longwinded to explain but in essence it makes the sorted directory listing into a string. Then uses loops and matching to condense it back to the expression. Finally it prepends the mkdir -p
command.
It doesn't produce the most efficient expression, but this might be improved if a different sort was used.
Upvotes: 0
Reputation: 195269
if you want to cp an existing dir structure, you could consider to use tar and untar. tar can work with your find
too.
well some example, just show what I meant.
on your existing serverA:
find .... -print |xargs tar -cf - |ssh user@SERVER_B "cd someDir; tar -xf - "
updated
if you prefer copy a file to serverB and fire it, here is another dirty solution:
on serverA:
kent$ mkdir a/b/{1..5}/c
now we have a tree, then :
kent$ find -type d|sed -r '/^.$/{s:.:#!/bin/bash:};{s/^\./mkdir -p &/}'
#!/bin/bash
mkdir -p ./a
mkdir -p ./a/b
mkdir -p ./a/b/4
mkdir -p ./a/b/4/c
mkdir -p ./a/b/3
mkdir -p ./a/b/3/c
mkdir -p ./a/b/1
mkdir -p ./a/b/1/c
mkdir -p ./a/b/5
mkdir -p ./a/b/5/c
mkdir -p ./a/b/2
mkdir -p ./a/b/2/c
now you can save the output to a script file, on B go to the target DIr, then run it on B
Upvotes: 1
Reputation: 185831
I hope I understand your needs : copy the directorys tree from server-A to server-B, so
On server-A :
cd /path/to/dir
find -type d | ssh server-B 'xargs -I% mkdir -p "/path/to/dir/%"'
Upvotes: 0