Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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 --
share|improve this answer

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.

share|improve this answer

What you do is first list the top level folders, using ls, then filtering out files by using egrep. Use awk to list just the folder name (without the file permissions, owner, etc). This list is then passed into xargs, which will run the command mkdir for each item in your list.

Make sure your current directory is the destination directory before running the command.

ls -l /path/to/source | egrep '^d' | awk '{print $9}' | xargs -L 1 mkdir

share|improve this answer
    
This is not the recommended way of getting only directories. It'll break if a directory contains whitespace, and you're basing everything off parsing ls, which should be avoided at all times for several reasons. In fact, for your specific example, the code would not work because it would only call mkdir folder for every top level folder in your list. In general, ls should really only be used to list files for viewing them in a shell, not working with file/directory names. find is better suited for that, as David's answer illustrates. –  slhck Sep 26 '13 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.