Solved

Soft links

Posted on 2004-10-08
15
397 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now