Symlink all files but copy certain extensions

2

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.

Mike

Posted 2011-07-27T18:44:28.230

Reputation: 23

I am assuming this is linux? – soandos – 2011-07-27T18:52:09.447

Yes, sorry, in linux. – Mike – 2011-07-27T20:23:17.280

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 – 2011-07-27T21:44:27.617

Answers

0

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

Nicholi

Posted 2011-07-27T18:44:28.230

Reputation: 628

Great job, that works wonders! Thanks for sharing the knowledge on find!! Good stuff.. – Mike – 2011-07-28T14:16:32.820