I'm wondering how I'd be able to symlink all the files in a dir structure and then also copy files of a certain extension in that dir. I'm basically symlinking all files within /foo/a,b,c to /bar/a,b,c and then copying over certain files with a certain extensions.

  • I am assuming this is linux?
    – soandos
    Jul 27 '11 at 18:52
  • Yes, sorry, in linux.
    – Mike
    Jul 27 '11 at 20:23
  • Were /foo/a /foo/b foo/c meant to be directories? In that you need the entire directory structure copied under /foo, symlink all files not of a certain ext (in tree /foo), and finally copy all files of a certain ext (in tree /foo)?
    – Nicholi
    Jul 27 '11 at 21:44

From the /bar directory, to create all symlinks for everything but these extensions.

find ../foo/ -type f ! -name '*.txt' ! -name '*.baz' -exec ln -s '{}' \;

And then to copy all the extensions, same command for the most part.

find ../foo/ -type f \( -name "*.txt" -o -name "*.baz" \) -exec cp '{}' ./ \;

Edit: Copy entire directory structure, symlink some files, copy others. Wasn't sure if this was possible at first with a single command, but just learned some other handy tricks with find.

find foo/ -type d -printf "mkdir -vp 'bar/%p'\n" -o -type f ! -name "*.txt" ! -name "*.baz" -printf "ln -vs '../%p' 'bar/%p'\n" -o -type f \( -name "*.txt" -o -name "*.baz" \) -printf "cp -v %p bar/%p\n" | sh
mv bar/foo/* bar/ && rm -R bar/foo/

Only important thing to note is when making the symbolic link you give the actual path the links will take to relate to foo/. My example shows relative links when they are side by side (could also make absolute symbolic links as well).

  • Great job, that works wonders! Thanks for sharing the knowledge on find!! Good stuff..
    – Mike
    Jul 28 '11 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.