Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

/path/to/dirs/
  foo
  bar
  baz/
    tri

Should be copied to

~/path/to/dirs/
  foo -> /path/to/dirs/foo
  bar -> /path/to/dirs/bar
  baz/
    tri -> /path/to/dirs/tri
share|improve this question
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? –  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. –  Tass 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
add comment

2 Answers

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
add comment

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"
    else
      ln -s "$x" "$HOME$x"
    fi
  done
' _ {} +
share|improve this answer
add comment

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.