Soft links

How to create a soft link on another directory?
Example

In my current directory, I have got a file named "file1" and a directory named "dir1". I would like to create a soft link pointing to file1 in dir1.

I use this
    ln -s ./file1 ./dir1/file1

Then in my dir1 directory, I do a ls -l but then i saw it is pointing to ./file1 and not ../file1
How come?
LVL 33
hongjunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yuzhCommented:
You do:

ln -s /dir1/file1 /dir2/file1

man ln
to learn more details
0
Matt_AveryCommented:
The fist argument to "ln" (sourcefile) should be an absolute path or a path relative to the second argument.

You could do:

$ ln -s ../file1 ./dir1/file1

But I find it easier on the brain to always create links in the current directory, so I would do this:

$ cd ./dir1
$ ln -s ../file1 file1



0
yuzhCommented:
PS: you should use FULL path for the sourcd file,

eg:
    file1 and dir1 are sitting in /abc, you can do:
    cd /abc
    ln -s /abc/file1  dir1/file1
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

hongjunAuthor Commented:
@yuzh,

I have got this piece of shellscript
I expect 2 arguments.
The 1st argument can be in any directory.
If I do a cd before I do a ln -s, then how would I know how many ../ should I put?


#!/bin/bash

# incorrect usage
if [ ! $# -eq 2 ]
then
    echo "Usage: $0 <search directory> <search string>"

    # unsuccessful exit
    exit 1
# correct usage
else
    # store the .search directory
    # if the last character is /, we need not add the / else we append
    if [ ${1:$((${#1}-1))} != "/" ]
    then
        DOT_SEARCH_DIR="$1/.search"
    else
        DOT_SEARCH_DIR="$1.search"
    fi

    # directory for .search does not exist so we mkdir
    if [ ! -d "$DOT_SEARCH_DIR" ]
    then
        mkdir "$DOT_SEARCH_DIR"
    else
        echo "$DOT_SEARCH_DIR already existed so new symbolic links to files will be added on and existing ones will be replaced."
    fi

    # create symbolic links to files to be searched for
    ln -sf "$PWD/$2"* $DOT_SEARCH_DIR

    # successful exit
    exit 0
fi




hongjun
0
Matt_AveryCommented:
yuzh is correct - you can use an absolute path ("/abc/file1") instead of a relative path ("../file1").

There are times when one alternative is better than the other. For instance, absloute paths will break if you move the entire directory structure, and a relative path would break if you moved the "dir1" directory.
0
yuzhCommented:
Hi hongjun,

    It is a lot easier to use absolute path  to do "ln", and you don't
have to worry about "cd" and in which dir the user startup your
script.

  You can always do:
    if [ ! -d "$DOT_SEARCH_DIR" ]

    you check if the dir exists or not.
   
0
cutie2000Commented:
Ya. I also believe so..
I emailed to my lecturer on this issue and asked if we are allowed to use absolute pathname.
I believe the use of relative pathnames will make the whole thing "redundant" and waste of time.

hongjun
0
hongjunAuthor Commented:
sorry..
above is my friend.
using my laptop.
0
hongjunAuthor Commented:
I have got this problem..

ln -s /home/myusername/a* ./mydir



If my currently home directory does not have files starting with a*, it will still create a softlink with a* pointing to a*. Why is that so? I do not want it to create the link since it does not have files matching it.
0
hongjunAuthor Commented:
And also!
when I do this

ln -s /home/myusername/a* ./mydir


How to exclude creating soft links for directories? I only want to create soft links for files. Try to use ln command only.
0
yuzhCommented:
>>How to exclude creating soft links for directories?

ln -s /path-to/dir1 /path-to/dir2

eg:

ln -s ln -s /path-to/dir1 mydir


0
hongjunAuthor Commented:
Summary of my problem
1. How to exclude creating soft links for directories? I only want to create soft links for files. Try to use ln command only.

2. ln -s /home/myusername/a* ./mydir
If my currently home directory does not have files starting with a*, it will still create a softlink with a* pointing to a*. Why is that so? I do not want it to create the link since it does not have files matching it.


Actually I have solved my problem using other tricks. I will post them here after Monday. Still hoping for alternative solution.
0
yuzhCommented:
Sorry, I didn't read "exclude" in my last comment.

1>>. How to exclude creating soft links for directories? I only want to create soft links for files. Try to use ln command only.

Why you want to do it?  the way " ln -s" work is like a pointer (or short cut in M$ Windows)

let say you do:
ln -s  /home/myusername/project/data /mydata

create a symbolic /mydata for dir "/home/myusername/project/data "

when cd /mydata, you can access all the files under  /home/myusername/project/data  
with only ONE link (pointer)!

If you want to like all the files under /home/myusername/project/data  to dir1,

you need to do:

for f in `find /home/myusername/project/data  -type f -print`
do
     linkf=`basename $f`
     ln -s $f /dir1/${linkf}
done

>> 2. ln -s /home/myusername/a* ./mydir
     You need to a real source filename/dirname, otherwise it will not work!

     PS: It is safe to use "rm" to delete the link (target file)!

     I hope that I can make you understand it this time.

Good luck!



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hongjunAuthor Commented:
thanks a lot..
i used anothermethod instead but urs shld oso work.
0
hongjunAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.