2

I am trying to link files in a while loop for my script but just the simple linking code itself creates a broken link.

The directory structure is this:

main/working/script.sh main/working/dir main/shared/default/some_files 

My script has this code:

ln -s ../shared/default/* dir 

This creates broken link. I can make the link not broken if I go inside the directory of main/working/dir and use ln -s ../../shared/default/* .

0

1 Answer 1

1

That is because you link to a relative path; Inside your script go to main/working/:

cd main/working/ ln -s ../shared/default/* dir 

either use the absolute path:

ln -s /absolute/path/to/shared/default/* dir 

you might even deduce the path where your script is located and use that path:

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) ln -s $DIR/../../shared/default/* dir 

edit: bash cannot expand the * if you are not at the right directory, so you can work around that to temporarily change directories:

# go to dir to make correct relative links cd dir ln -s $DIR/../../shared/default/* ./ cd .. 
Sign up to request clarification or add additional context in comments.

7 Comments

is there another way on linking it without using the absolute path?
I gave you three possibilities; the first is relative, second absolute, third is a mix of both: relative to the place where your script is located...
The first option you gave was the code I used and it gives broken links. The third option though is too long.
your link is broken because you are not located in the correct directory; that's why you need to go to your working directory
remark the line just before the one you used: cd main/working (note that you could use an absolute directory too here: cd /path/to/main/working
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.