Create soft links from multiple specific files in various subdirectories

I want to create soft links (ln -s) to folder2 of all the files that contain *foo* in its name, and can be found in some or all the subdirectories of folder1.

I’ve tried it with for, find, and find -exec ln, and a combination of them, but all I get is a broken link named *foo* or a link to everything inside folder1.


You can use this little snippet


find "$folder1" -type f -name '*foo*' -exec  
sh -c 'for f; do ln -s "$folder1" "/path/to/folder2/${f##*/}"; done' _ {} +

This can run from anywhere since I’m using absolute paths here.

Leave a Reply

Your email address will not be published. Required fields are marked *