Create soft links from multiple specific files in various subdirectories Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Create soft links from multiple specific files in various subdirectories without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

You can use this little snippet

#!/bin/bash

folder1="/path/to/folder1"
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.

We are here to answer your question about Create soft links from multiple specific files in various subdirectories - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji