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
  • 1
    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? May 4, 2013 at 23:42
  • I put slashes where there should be. The question is how to copy directories and symlink files instead of copy both. May 4, 2013 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/. May 4, 2013 at 23:51

3 Answers 3


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=.

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"
' _ {} +
cp --no-dereference orig dest

This will preserve symlinks when copying. Here is the documentation:

 -P, --no-dereference         never follow symbolic links in SOURCE
 -p                           same as --preserve=mode,ownership,timestamps

You must log in to answer this question.

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