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

share|improve this question
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
up vote 0 down vote accepted

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

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

You must log in to answer this question.

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