HelloWorld
HelloWorld

Reputation: 297

UNIX, how to maintain directory structure?

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

Upvotes: 1

Views: 1211

Answers (3)

altblue
altblue

Reputation: 948

You might want to use rsync instead:

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

which is, more verbose:

rsync --verbose --recursive --include '*/' --exclude '*' bar1 bar2

Upvotes: 2

Michael Mrozek
Michael Mrozek

Reputation: 175395

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

Upvotes: 2

spacedentist
spacedentist

Reputation: 390

cd bar1
find . -type d -exec mkdir -p '../bar2/{}' \;

Upvotes: 6

Related Questions