Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 have got a directory of huge files (total ~ 1TB) and I don't want copy them around. However, I'd like to work around them, so it would be convenient to have them linked in a directory hierarchy I have access to (aka one I created). So


Should be copied to

  foo -> /path/to/dirs/foo
  bar -> /path/to/dirs/bar
    tri -> /path/to/dirs/tri
share|improve this question
It's unclear what you want. What is the problem, the number of directories? Finding the lowest-level directories by script? Do foo and bar not have subdirectories? – Hauke Laging May 4 '13 at 23:42
I put slashes where there should be. The question is how to copy directories and symlink files instead of copy both. – Reactormonk May 4 '13 at 23:44
You want help. Obviously the people who shall help you decide "where slashes should be". Don't try to be more clever than those who shall get done what you can't. That foo is to be a file just because there is no slash is a strange assumption considering that its parent path is path/to/dirs/. – Hauke Laging May 4 '13 at 23:51
up vote 0 down vote accepted

With GNU:

cd ~/path/to/dirs || exit 1
find /path/to/dirs -type d -printf %P\\0 | xargs -0 mkdir -p 
find /path/to/dirs -type f -print0 | 
  xargs -0 cp --symbolic-link --parents --target-directory=.
share|improve this answer

With GNU coreutils (i.e. on non-embedded Linux or Cygwin):

cp -al /path/to/dirs ~/path/to/dirs

If your cp doesn't have the -l option, make a first pass to create the directory hierarchy, then a second pass for the symbolic links:

find /path/to/dirs -type d -exec sh -c 'for x do mkdir "$HOME$x"; done' _ {} +
find /path/to/dirs ! -type d -exec sh -c 'for x do ln -s "$x" "$HOME$x"; done' _ {} +

You can combine the two passes into a single command if you prefer:

find /path/to/dirs -exec sh -c '
  for x do
    if [ -d "$x" ]; then
      mkdir "$HOME$x"
      ln -s "$x" "$HOME$x"
' _ {} +
share|improve this answer

Your Answer


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.