5

I have a large directory tree with lots of files.

I would like to re-create the directory structure in a new location, but only the directories, not the files.

I've checked cp's man page, but don't see an argument that will help.

4 Answers 4

8
rsync -av -f"+ */" -f"- *" /.../SOURCE /.../DESTINATION
2
  • 2
    can you explain?
    – chovy
    Aug 21, 2013 at 6:08
  • 2
    This is explained in man rsync (see section "INCLUDE/EXCLUDE PATTERN RULES"). The option -f adds a filter. "+ */" includes all directories, the following "- *" excludes everything else. Aug 21, 2013 at 10:46
5
find . -type d -exec mkdir -p ~/to/{} \;

Taken from @whitequark's answer to to this question: Copy directory structure without copying files, on Mac OS X

1
  • Don't suppose there's a way to do this whilst maintaining attributes?
    – Sobrique
    Dec 11, 2014 at 16:40
5

Another approach is use the tree which is pretty handy and navigating directory trees based on its strong options. There are options for directory only, exclude empty directories, exclude names with pattern, include only names with pattern, etc. Check out man tree

Advantage: you can edit or review the list, or if you do a lot of scripting and create a batch of empty directories frequently

Approach: create a list of directories using tree, use that list as an arguments input to mkdir

tree -dfi --noreport > some_dir_file.txt

-dfi lists only directories, prints full path for each name, makes tree not print the indentation lines,

--noreport Omits printing of the file and directory report at the end of the tree listing, just to make the output file not contain any fluff

Then go to the destination where you want the empty directories and execute

xargs mkdir < some_dir_file.txt
0

Found this as have hit a similar need. With a new enough version of rsync, that's your best bet.

If you don't have a recent version of rsync, then this does the trick:

find path/to/source -type -d -exec mkdir path/to/dest/'{}' \; -exec chown --reference='{}' path/to/dest/'{}' \; -exec chmod --reference='{}' path/to/dest/'{}' \;

(A variant of this can be used for permission mirroring)

You must log in to answer this question.

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