I'm under linux and use bash as my login shell . I need to make softlink to a bunch of files. Thoes files are listed in a text file , let's say file_list.txt . Every line in file_list.txt is just a full path of one target file need to be linked . Although I don't know exactly how to, I do believe there is way to combine "cat " "| " and "ln -s " together to make the job done whithin one shell command . So need your help .


If you want the names in the current directory (or any one directory), and if the names in the file are all absolute names, or are relative names correct relative to the target directory, then:

ln -s $(<file_list.txt) .

If you prefer to use 'cat', then:

ln -s $(cat file_list.txt) .

Clearly, I'm using the current directory as the target.


You don't say what you want to link to.

Use a while loop without cat. If there are spaces in the filenames this method will handle that.

while read -r line; do ln -s "$line" target; done < file_list.txt

You will have to supply the way to derive the name of the target since you didn't specify it in your question.


Consider something like (untested):

for targ in $(cat file_list);
   tfile=$(basename $targ)
   ln -s $targ $tfile

Note that the basename will strip any file extention like naming (i.e. ".exe" or whatever). This can be fixed but it is a pain. You can have base name strip any single given filename extension if you so desire (use the -s flag).

You can compress this into a single line like:

$ for targ in $(cat file_list); do tfile=$(basename $targ); ln -s $targ $tfile; done
  • basename doesn't strip extensions unless you tell it to. – Dennis Williamson Feb 7 '10 at 23:03
  • @Dennis: For some reason I had that behavior reverse in my head. Thanks. – dmckee Feb 8 '10 at 0:29
  • BSD and OS X (BSD based) basename has -s, others (Linux, HPUX, AIX, etc.) don't. For the ones that do, you can still do the extension stripping without -s for portability. – Dennis Williamson Feb 8 '10 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.