Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to duplicate the directory structure in some directory to another, without copying any actual files. In place of the files, I'd like either a blank file with the same name, or a symlink to the original location.

How could I do this, on OS X? Command-line tools (rsync/find/tar/whatever), shell scripts, etc., are all acceptable, software I have to buy probably isn't.

share|improve this question
up vote 8 down vote accepted
user@osx:~/from$ find . -type d -exec mkdir -p ~/to/{} \;
user@osx:~/from$ find . -type f -exec ln -s ~/from/{} ~/to/{} \;


user@osx:~/from$ find . -type f -exec touch ~/to/{} \;
share|improve this answer
This works, thanks. – ShreevatsaR Aug 3 '10 at 22:27
Both this and the lndir solution worked. I've "accepted" this because lndir comes with X and may go away or not be present on a system. If you have lndir, though, it's easiest to use. – ShreevatsaR Aug 4 '10 at 2:48
5 years on this still works. I've not been a serious Unix command line user for over 15 yrs so this brings back great memories (pain :-) – Mark Levison Oct 30 '15 at 19:08

Try lndir. From its man page:

The lndir program makes  a  shadow  copy  todir  of  a  directory  tree
fromdir,  except  that  the shadow is not populated with real files but
instead with symbolic links pointing at the real files in  the  fromdir
directory tree.
share|improve this answer
That's OS X magic! – whitequark Aug 3 '10 at 22:15
Wow, this would be exactly what I want. It's in /usr/X11 for some reason (the manpage is also listed under X). For some reason, when I run it, like lndir dir1 dir2, I get dir1: No such file or directory. – ShreevatsaR Aug 3 '10 at 22:26
Oh, fixed it. The manpage says The fromdir argument […] is relative to todir (not the current directory). So if you have dir1 and dir2 both in the current directory, you have to type lndir ../dir1 dir2. Of course, typing the full path for dir1 also works. Thanks again. – ShreevatsaR Aug 3 '10 at 22:31

Here's one more variation of the rsync method, which will keep folder icons and finder labels:

rsync -Ea /path/from/ /path/to/ --include="*/"  --include="Icon*" --exclude="*"

This doesn't copy file aliases, so you might want to combine it with whitequark's answer above.

share|improve this answer

Another solution that works for me is to use rsync and exclude all files, e.g.

$ rsync -a /path/from/ /path/to/ --include \*/ --exclude \*

The --include \*/ specifies that all directories should be copied, and --exclude \* specifies that all files should be excluded from the copy.

The beauty of this is that the new directory hierarchy has the same attributes, timestamps, permissions, etc as the original.

share|improve this answer
I agree, having the right attributes is great. Do you know if you can do something about the files, so that they exist (either as empty files or symlinks)? – ShreevatsaR Aug 4 '10 at 18:57
Sorry - I missed that part of the question first time around - it may well be possible with rsync but I'll need to give it some more thought. – Paul R Aug 5 '10 at 8:28

This will find the directories inside temp, remove the leading /home/bryan/ from them and create the new directories

  for i in `find /home/bryan/temp -type d| sed 's/\/home\/bryan\///g'`
  do mkdir $i

I suggest typing in the code one line at a time (this way semicolons (not shown) will be placed correctly)

share|improve this answer

You must log in to answer this question.

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