1

I want to create an empty directory structure bar2 from a nonempty directory tree bar1. Both bar1 and bar2 are at the same hierarchical level. How can I use mkdir in an efficient manner so that intermediate directories are automatically created?

  1. To create a directory list from bar1 with find and order it if necessary.
  2. Using awk, remove all branches from the list so that I can run `mkdir only on the leaves.
  3. Run mkdir with the list to replicate the directory structure of bar1
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
HelloWorld
  • 297
  • 4
  • 12

3 Answers3

6
cd bar1
find . -type d -exec mkdir -p '../bar2/{}' \;
spacedentist
  • 390
  • 1
  • 4
2

The hard part of your question (how to list only leaf directories) has been asked before on SO. You can use the find/awk combo there and run mkdir -p on each result:

[bar1] $ find . -type d | sort | awk '$0 !~ last {print last} {last=$0} END {print last}' | xargs -Ix mkdir -p ../bar2/x
Community
  • 1
  • 1
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
2

You might want to use rsync instead:

rsync -r -f '+ */' -f '- *' bar1 bar2

which is, more verbose:

rsync --verbose --recursive --include '*/' --exclude '*' bar1 bar2
altblue
  • 937
  • 12
  • 19