I can see some of you have arrived to my Unix file types post looking for an example of using symlinks in Unix. Today I would like to give you a quick introduction into Unix symlinks.
What is symlink?
Symlink is a short name for symbolic link (sometimes also referred as soft link) is a special type of file in Unix, which references another file or directory. Symlink contains the name for another file and contains no actual data. To most commands, symlinks look like a regular file, but all the operations (like reading from a file) are referred to the file the symlink points to.
How to create a Unix symlink
Just to give you an example, here's how a typical symlink can be created and verified.
First, we create a new text file called /tmp/file.1:
greys@ubuntu:~$ echo "Hello from file 1" > /tmp/file.1 greys@ubuntu:~$ cat /tmp/file.1 Hello from file 1
Now, we create a symlink called /tmp/file.2, which points to the original file of ours. We use the standard Unix ln command, first specifying the target file (the real file we want our symlink to point to), then specify the name of our symbolic link:
greys@ubuntu:~$ ln -s /tmp/file.1 /tmp/file.2
If we look at both files, here's what we see:
greys@ubuntu:~$ ls -al /tmp/file* -rw-r--r-- 1 greys greys 18 2008-02-07 22:22 /tmp/file.1 lrwxrwxrwx 1 greys greys 11 2008-02-07 22:23 /tmp/file.2 -> /tmp/file.1
If you notice, the /tmp/file.2 has an "l" in the long-format output of the ls command, which confirms it's a symbolic link. You also can see right away where this symlink points to.
Just to confirm the typical behaviour of a symlink, here's what happens when we try to show the contents of the /tmp/file.2: we see the contents of the file it points to, /tmp/file.1:
greys@ubuntu:~$ cat /tmp/file.2 Hello from file 1
How to remove a symlink
Guess what happens when you remove a symlink? Actually, not much. You only remove the symlink file itself, not the data file it refers to. Here's what I mean:
greys@ubuntu:~$ rm /tmp/file.2 greys@ubuntu:~$ ls -al /tmp/file* -rw-r--r-- 1 greys greys 18 2008-02-07 22:22 /tmp/file.1
While we're at it, I would also like to explain what an orphan symlink is: it's a symbolic link which points nowhere, because the original target file it used to point to doesn't exist anymore.
Here is how an orphan symlink looks. First off, we recreate the symlink and verify it points to /tmp/file.1 once again:
greys@ubuntu:~$ ln -s /tmp/file.1 /tmp/file.2 greys@ubuntu:~$ ls -al /tmp/file* -rw-r--r-- 1 greys greys 18 2008-02-07 22:22 /tmp/file.1 lrwxrwxrwx 1 greys greys 11 2008-02-07 22:38 /tmp/file.2 -> /tmp/file.1
Now, we simply rename the /tmp/file.1 file to /tmp/file.3:
greys@ubuntu:~$ mv /tmp/file.1 /tmp/file.3
This, naturally, makes /tmp/file.2 an orphan symlink which points to the old /tmp/file.1, but there isn't a file like this anymore. Attempt to show the contents of /tmp/file.2 will thus fail:
greys@ubuntu:~$ ls -al /tmp/file* lrwxrwxrwx 1 greys greys 11 2008-02-07 22:38 /tmp/file.2 -> /tmp/file.1 -rw-r--r-- 1 greys greys 18 2008-02-07 22:22 /tmp/file.3 greys@ubuntu:~$ cat /tmp/file.2 cat: /tmp/file.2: No such file or directory
I want to create symbolic link for my ctl file I am using command as
ln -s $FND_TOP/bin/fndcpesr $XBOL_TOP/bin/my file name
It gets created ..
But now I want to create that link with
ln -s $CE_TOP/bin/fndcpesr $XBOL_TOP/bin/my file name..
But now it gives error as 'FILE EXISTS' ..
I also removed previous link..
Can anyone please help me out..
Thanks
Ganesh
Hi Ganesh,
The first parameter to ln command is the SOURCE file – the one which already exists and you're trying to link to it. The second parameter is the DESTINATION file, the new one which will be created and point to the SOURCE file.
So from your examples it looks like the $XBOL_TOP/bin/my_file_name is the SOURCE link, and the $CE_TOP/bin/fndcpesr is the DESTINATION, just swap their order in the command line to get it all working.
Good luck!
I have a shell script where get the install directory path. Sometimes this install directory path is a sybolic link. The script resolves the path to the real directory path and not the symbolic link path. How do get the symbolic link path itstead of the real directory path?
This is how I get the path:
INST_HOME=`echo $CWD | sed 's|/ims/IMS1/bin||g'`
echo "INST_HOME: \"$INST_HOME\" "
Nicely explained. Thanks
Best thing ever, now I can start to manage my staging and production server 😉 thanks a lot dude!
You're always welcome, Kay