| « nameserver DDOS | udev not renaming interfaces properly » |
duplicating *nix directory structures
I recently had a need to duplicate some directory structures - not the files within the directories - just the directory tree itself. Rather than sifting through and spending hours trying to recurse through the directories by hand, I went to work finding a better way. Find does a good job of listing out the tree:
find /var/opt/ -type d
Combine that with sed and you can create a script to create the directories.
find /var/opt -type d | sed 's/old_dir/new_dir/' | sed 's/.*/mkdir -p "&"/'
It's even possible to use ssh to retrieve the directory structure from a different machine with:
ssh user@system "command"
Put it all together and you get
ssh user@system "command" | find /var/opt -type d | sed 's/old_dir/new_dir/' | sed 's/.*/mkdir -p "&"/'
Finally, you can pipe that output to sh and run it on the fly rather than creating scripts to do it.
ssh user@system "command" | find /var/opt -type d | sed 's/old_dir/new_dir/' | sed 's/.*/mkdir -p "&"/' | sh
Very handy - particularly in my new role on the Tech Services team!