Skip to content
Advertisement

LINUX: How to softlink specific files in all 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

Advertisement

Answer

With globstar at one stroke.

shopt -s globstar
cd folder2
ln -s ../folder1/**/*foo* .

cd is needed for relative links (this applies for below answers that utilizes find too). If you want absolute links, do

shopt -s globstar
ln -s /where/is/it/folder1/**/*foo* folder2/
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement