Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to create symbolic links (ln -s) to all files (or a class of files, e.g. ending with .bar) in a certain directory. Say I'm in the cwd and type ls ../source/*.bar gives me

foo.bar
baz.bar

how can I pass the parameter list to ln -s that it finally resolves to

ln -s ../source/foo.bar
ln -s ../source/baz.bar

Of course I know I can write a bash script, but there should be something simpler involving xargs or so since it seems to be a common task - at least for me.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

ln does take multiple arguments, but don't forget to give a target directory in that case.

So, in your example . is the target directory, so it should be as easy as

ln -s ../source/*.bar .

From man ln; the command above uses the 3rd form:

ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
ln [OPTION]... TARGET                  (2nd form)
ln [OPTION]... TARGET... DIRECTORY     (3rd form)
ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
  • In the 1st form, create a link to TARGET with the name LINK_NAME.
  • In the 2nd form, create a link to TARGET in the current directory.
  • In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
share|improve this answer

Use find

certainDir="/path/to/dir"
find -name "*.bar" -exec ln -s {} "$certainDir" \;

Also, remember to use full paths (where possible) with symlinks.

share|improve this answer
1  
Thanks, but isn't it find $certainDir -name "*.bar" -exec ln -s {} \;? And why should I use full paths? I find it quite convenient to know that links stay intact when I move a directory tree around wihch has some internal links. –  dastrobu Aug 19 '13 at 8:56
    
That depends where you want to symlink the directories. And I can't remember why full paths are a good idea, it might have been for hard links but I burned that into my mind for a reason. Hmmm. –  justbrowsing Aug 19 '13 at 8:59
    
Thanks, your answer is great. Since mpy's answer is the simpler solution to the question, the point goes to him/her. I would accept both answers, if I could. –  dastrobu Aug 19 '13 at 9:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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