How to link a selected set of files from one directory to another in Linux?

Issue

Say for example, in the source directory I have the following files:

  1. abc.r
  2. xyz.sh
  3. pqr.fam
  4. lmn.bim
  5. uvw.r
  6. ttt.sh

Now I need to link only the items 1,2 and 5 only (listed above). Most importantly I need to link all the 3 files together (i.e. link all the 3 files at the same time).

I know how to link 1 file at a time (ln -s sourceDirectory/fileName targetDirectory/), but not multiple files at once.
I found ways to do this when the file name prefixes has some pattern (for example, link all the files where the names start with letter "f"), but in my case, I do not have any such pattern. My file names are different.

Solution

Try this:

#!/bin/bash

for file in a.txt b.txt c.txt
do
    ln -s /sourcedir/"${file}" /targetdir/
done

Since you only have a list, you have to iterate through the list.

Answered By – Nic3500

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published