Create the same subfolders in another folder

Using rsync:

rsync -a --include='*/' --exclude='*' /some/path/dir/ dir

This would recreate the directory structure of /some/path/dir as dir in the current directory, without copying any files.

Any directory encountered in the source path would be created at the target due to the inclusion pattern, but anything else would be excluded. As a side effect of using -a (--archive), you'll get the same timestamps on all subdirectories in the target as in the source. This also works for creating local directory structures from remote directories (and vice versa).


Try this,

cd /source/dir/path
find . -type d -exec mkdir -p -- /destination/directory/{} \;
  • . -type d To list directories in the current path recursively.
  • mkdir -p -- /destination/directory/{} create directory at destination.

This relies on a find that supports expanding {} in the middle of an argument word.


You can use find to traverse the source structure and call mkdir for each directory it meets.

This example, using find, will copy your directory structure from foo to /tmp/another/

( cd foo && find -type d -exec sh -c 'for d do mkdir -p "/tmp/another/$d"; done' sh {} + )

The exec loop builds up the set of directories underneath foo, which is then passed to the mkdir. If you don't have a version of find that understands + you can use \; at the cost of efficiency. Substitute mkdir with echo mkdir to see what would happen without actually doing it.

Tags:

Mkdir