Home > front end >  How to link a selected set of files from one directory to another in Linux?
How to link a selected set of files from one directory to another in Linux?

Time:11-02

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.

CodePudding user response:

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.

  • Related