5

I would like to copy a folder structure (without the files), but only the top level folders. That is, with my source:

folder a
 * folder 1
 * folder 2
   * folder X
folder b
 * folder 3
folder c

I want my destination to simply be:

folder a
folder b
folder c

How can I do this in Linux?

3 Answers 3

5

With GNU find, which supports -printf, and GNU xargs, which supports -r:

find /source/path -mindepth 1 -maxdepth 1 -type d -printf '/target/path/%f\0' | xargs -r -0 -- mkdir --
2

You could simply do this:

for dir in *; do mkdir /path/to/"$dir"; done

This assumes that you want to copy everything in the current directory and that all you have in that directory are the target folders, no files. It will collect all names in the current directory (*) and run mkdir to create empty folders of that name in the target path.

1

If you want to preserve ownership, etc. (at least it works on CentOS 7):

find /source/path -maxdepth 1 -type d | cpio -pdvm /destination/path

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .