Solved

Soft links

Posted on 2004-10-08
15
402 Views
Last Modified: 2013-12-27
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?
0
Comment
Question by:hongjun
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 12256918
You do:

ln -s /dir1/file1 /dir2/file1

man ln
to learn more details
0
 
LVL 2

Expert Comment

by:Matt_Avery
ID: 12256947
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
 
LVL 38

Expert Comment

by:yuzh
ID: 12256963
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Author Comment

by:hongjun
ID: 12257049
@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
 
LVL 2

Expert Comment

by:Matt_Avery
ID: 12257053
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
 
LVL 38

Expert Comment

by:yuzh
ID: 12257924
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
 

Expert Comment

by:cutie2000
ID: 12257965
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
 
LVL 33

Author Comment

by:hongjun
ID: 12257998
sorry..
above is my friend.
using my laptop.
0
 
LVL 33

Author Comment

by:hongjun
ID: 12265025
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
 
LVL 33

Author Comment

by:hongjun
ID: 12265140
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
 
LVL 38

Expert Comment

by:yuzh
ID: 12266147
>>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
 
LVL 33

Author Comment

by:hongjun
ID: 12269009
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
 
LVL 38

Accepted Solution

by:
yuzh earned 500 total points
ID: 12273566
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
 
LVL 33

Author Comment

by:hongjun
ID: 12273884
thanks a lot..
i used anothermethod instead but urs shld oso work.
0
 
LVL 33

Author Comment

by:hongjun
ID: 12273887
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question