Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I selectively create symbolic links to specific files in another directory in LINUX?

I'm not exactly sure how to go about doing this, but I need to create symbolic links for certain files in one directory and place the symbolic links in another directory.

For instance, I want to link all files with the word "foo" in its name in the current directory bar1 that does not have the extension ".cc" and place the symbolic links in a directory bar2.

I was wondering if there was single line command that could accomplish this in LINUX bash.

like image 602
Justin Avatar asked Oct 19 '25 12:10

Justin


2 Answers

Assuming you are in a directory that contains directories bar1 and bar2:

find bar1 -name '*foo*' -not -type d -not -name '*.cc' -exec ln -s $PWD/'{}' bar2/ \;
like image 92
Greg Inozemtsev Avatar answered Oct 21 '25 01:10

Greg Inozemtsev


Try this:

cd bar1
find . -maxdepth 1 -name '*foo*' -not -name '*.cc'  -exec echo ln -s $PWD/{} ../bar2 \;

Once you are satisfied with the dry run, remove echo from the command and run it for real.

like image 26
Hai Vu Avatar answered Oct 21 '25 03:10

Hai Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!